<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        // 月嫂档案
        Schema::create('nannies', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->string('name', 50);
            $table->string('phone', 20)->nullable();
            $table->string('id_card', 20)->nullable();
            $table->string('avatar', 255)->nullable();
            $table->tinyInteger('level')->nullable()->comment('1初级 2中级 3高级 4金牌');
            $table->json('skills')->nullable()->comment('技能标签');
            $table->integer('experience_years')->nullable();
            $table->string('health_cert', 255)->nullable()->comment('健康证');
            $table->tinyInteger('cooperation_type')->nullable()->comment('1自有 2合作 3兼职');
            $table->decimal('base_salary', 10, 2)->nullable();
            $table->text('introduction')->nullable();
            $table->tinyInteger('status')->default(1)->comment('1空闲 2服务中 3休假 4停用');
            $table->timestamps();
            $table->index(['store_id', 'status']);
        });

        // 月嫂订单
        Schema::create('nanny_orders', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->foreignId('customer_id')->constrained();
            $table->unsignedBigInteger('nanny_id')->nullable();
            $table->string('order_no', 50)->unique();
            $table->tinyInteger('service_type')->nullable()->comment('1月嫂 2育儿嫂');
            $table->date('start_date')->nullable();
            $table->date('end_date')->nullable();
            $table->integer('days')->nullable();
            $table->decimal('price', 12, 2)->nullable();
            $table->tinyInteger('status')->default(0)->comment('0待匹配 1待确认 2已签约 3服务中 4已完成 5已取消');
            $table->json('match_candidates')->nullable()->comment('匹配候选月嫂');
            $table->text('remark')->nullable();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->timestamps();
            $table->index('store_id');
            $table->index('nanny_id');
        });

        // 月嫂排班
        Schema::create('nanny_schedules', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('nanny_id');
            $table->unsignedBigInteger('nanny_order_id')->nullable();
            $table->date('schedule_date');
            $table->tinyInteger('type')->nullable()->comment('1服务 2休息 3培训');
            $table->string('remark', 255)->nullable();
            $table->timestamp('created_at')->nullable();
            $table->index(['nanny_id', 'schedule_date']);
        });

        // 月嫂评价
        Schema::create('nanny_reviews', function (Blueprint $table) {
            $table->id();
            $table->foreignId('nanny_order_id')->constrained()->cascadeOnDelete();
            $table->foreignId('customer_id')->constrained();
            $table->unsignedBigInteger('nanny_id');
            $table->tinyInteger('score')->nullable()->comment('1-5');
            $table->text('content')->nullable();
            $table->timestamp('created_at')->nullable();
            $table->index('nanny_id');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('nanny_reviews');
        Schema::dropIfExists('nanny_schedules');
        Schema::dropIfExists('nanny_orders');
        Schema::dropIfExists('nannies');
    }
};
