<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('support_tickets', function (Blueprint $table) {
            $table->id();

            // scope：用于多层级隔离（platform/site/merchant）
            $table->string('scope', 20)->default('platform');
            $table->unsignedBigInteger('scope_id')->nullable();

            // 关联对象（用于“收费闭环/治理串联”）
            $table->unsignedBigInteger('merchant_id')->nullable();
            $table->unsignedBigInteger('buyer_id')->nullable();
            $table->unsignedBigInteger('order_id')->nullable();
            $table->unsignedBigInteger('platform_order_id')->nullable();
            $table->unsignedBigInteger('site_subscription_id')->nullable();
            $table->unsignedBigInteger('platform_lead_id')->nullable();

            // 工单基础字段
            $table->string('ticket_no', 50)->unique();
            $table->string('category', 50)->default('general');
            $table->string('priority', 20)->default('normal');
            $table->string('status', 20)->default('open');
            $table->string('subject', 200)->default('');
            $table->text('content')->nullable();

            // 指派与时间
            $table->unsignedBigInteger('assigned_admin_id')->nullable();
            $table->timestamp('closed_at')->nullable();

            $table->timestamps();

            $table->index(['scope', 'scope_id']);
            $table->index(['merchant_id']);
            $table->index(['platform_order_id']);
            $table->index(['site_subscription_id']);
            $table->index(['platform_lead_id']);
            $table->index(['status', 'priority']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('support_tickets');
    }
};
