<?php

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

return new class extends Migration
{
    /**
     * 작업지시 하위 테이블에 tenant_id 컬럼 추가
     * - work_order_items
     * - work_order_bending_details
     * - work_order_issues
     *
     * 기존 데이터는 work_orders 테이블의 tenant_id를 참조하여 업데이트
     */
    public function up(): void
    {
        // 1. work_order_items (이미 존재하면 건너뜀)
        if (! Schema::hasColumn('work_order_items', 'tenant_id')) {
            Schema::table('work_order_items', function (Blueprint $table) {
                $table->unsignedBigInteger('tenant_id')->nullable()->after('id')->comment('테넌트ID');
                $table->index('tenant_id', 'idx_work_order_items_tenant');
            });

            DB::statement('
                UPDATE work_order_items wi
                JOIN work_orders wo ON wi.work_order_id = wo.id
                SET wi.tenant_id = wo.tenant_id
            ');

            Schema::table('work_order_items', function (Blueprint $table) {
                $table->unsignedBigInteger('tenant_id')->nullable(false)->change();
            });
        }

        // 2. work_order_bending_details (이미 존재하면 건너뜀)
        if (! Schema::hasColumn('work_order_bending_details', 'tenant_id')) {
            Schema::table('work_order_bending_details', function (Blueprint $table) {
                $table->unsignedBigInteger('tenant_id')->nullable()->after('id')->comment('테넌트ID');
                $table->index('tenant_id', 'idx_work_order_bending_details_tenant');
            });

            DB::statement('
                UPDATE work_order_bending_details wbd
                JOIN work_orders wo ON wbd.work_order_id = wo.id
                SET wbd.tenant_id = wo.tenant_id
            ');

            Schema::table('work_order_bending_details', function (Blueprint $table) {
                $table->unsignedBigInteger('tenant_id')->nullable(false)->change();
            });
        }

        // 3. work_order_issues (이미 존재하면 건너뜀)
        if (! Schema::hasColumn('work_order_issues', 'tenant_id')) {
            Schema::table('work_order_issues', function (Blueprint $table) {
                $table->unsignedBigInteger('tenant_id')->nullable()->after('id')->comment('테넌트ID');
                $table->index('tenant_id', 'idx_work_order_issues_tenant');
            });

            DB::statement('
                UPDATE work_order_issues woi
                JOIN work_orders wo ON woi.work_order_id = wo.id
                SET woi.tenant_id = wo.tenant_id
            ');

            Schema::table('work_order_issues', function (Blueprint $table) {
                $table->unsignedBigInteger('tenant_id')->nullable(false)->change();
            });
        }
    }

    public function down(): void
    {
        Schema::table('work_order_items', function (Blueprint $table) {
            $table->dropIndex('idx_work_order_items_tenant');
            $table->dropColumn('tenant_id');
        });

        Schema::table('work_order_bending_details', function (Blueprint $table) {
            $table->dropIndex('idx_work_order_bending_details_tenant');
            $table->dropColumn('tenant_id');
        });

        Schema::table('work_order_issues', function (Blueprint $table) {
            $table->dropIndex('idx_work_order_issues_tenant');
            $table->dropColumn('tenant_id');
        });
    }
};
