<?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('service_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->string('name', 100);
            $table->tinyInteger('category')->nullable()->comment('1产康 2零售 3加餐 4其他');
            $table->decimal('price', 10, 2);
            $table->integer('duration')->nullable()->comment('服务时长(分钟)');
            $table->text('description')->nullable();
            $table->string('image', 255)->nullable();
            $table->tinyInteger('status')->default(1);
            $table->timestamps();
            $table->index(['store_id', 'category']);
        });

        // 服务包
        Schema::create('service_packages', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->string('name', 100);
            $table->json('items')->nullable()->comment('包含项目与次数');
            $table->decimal('price', 10, 2);
            $table->integer('validity_days')->nullable()->comment('有效天数');
            $table->tinyInteger('status')->default(1);
            $table->timestamps();
        });

        // 服务订单
        Schema::create('service_orders', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->foreignId('customer_id')->constrained();
            $table->string('order_no', 50)->unique();
            $table->tinyInteger('type')->nullable()->comment('1单项 2服务包 3零售 4加餐');
            $table->json('items')->nullable()->comment('订单项明细');
            $table->decimal('total_amount', 12, 2)->nullable();
            $table->decimal('discount_amount', 12, 2)->default(0);
            $table->decimal('actual_amount', 12, 2)->nullable();
            $table->tinyInteger('status')->default(0)->comment('0待付 1已付 2已完成 3已取消 4已退');
            $table->tinyInteger('pay_method')->nullable()->comment('1微信 2支付宝 3现金 4挂账');
            $table->timestamp('paid_at')->nullable();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->timestamps();
            $table->index('store_id');
            $table->index('customer_id');
        });

        // 服务执行记录
        Schema::create('service_executions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('service_order_id')->constrained()->cascadeOnDelete();
            $table->foreignId('service_item_id')->constrained();
            $table->foreignId('customer_id')->constrained();
            $table->unsignedBigInteger('technician_id')->nullable()->comment('技师/产康师');
            $table->timestamp('scheduled_at')->nullable()->comment('预约时间');
            $table->timestamp('started_at')->nullable();
            $table->timestamp('ended_at')->nullable();
            $table->tinyInteger('status')->default(0)->comment('0待执行 1执行中 2已完成 3已取消');
            $table->text('remark')->nullable();
            $table->timestamps();
            $table->index('service_order_id');
            $table->index('technician_id');
        });

        // 服务评价
        Schema::create('service_reviews', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('service_order_id')->nullable();
            $table->unsignedBigInteger('service_execution_id')->nullable();
            $table->foreignId('customer_id')->constrained();
            $table->tinyInteger('score')->nullable()->comment('1-5星');
            $table->text('content')->nullable();
            $table->timestamp('created_at')->nullable();
            $table->index('service_order_id');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('service_reviews');
        Schema::dropIfExists('service_executions');
        Schema::dropIfExists('service_orders');
        Schema::dropIfExists('service_packages');
        Schema::dropIfExists('service_items');
    }
};
