<?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::table('sites', function (Blueprint $table) {
            // 현장코드
            $table->string('site_code', 50)->nullable()->after('tenant_id')->comment('현장코드');

            // 거래처 연결 (clients 테이블)
            $table->foreignId('client_id')->nullable()->after('site_code')
                ->constrained('clients')->nullOnDelete()->comment('거래처 ID');

            // 상태 (is_active와 별개)
            $table->enum('status', ['unregistered', 'suspended', 'active', 'pending'])
                ->default('unregistered')->after('is_active')->comment('상태: 미등록/중지/사용/보류');

            // 인덱스
            $table->index(['tenant_id', 'site_code']);
            $table->index(['tenant_id', 'status']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('sites', function (Blueprint $table) {
            $table->dropIndex(['tenant_id', 'site_code']);
            $table->dropIndex(['tenant_id', 'status']);
            $table->dropForeign(['client_id']);
            $table->dropColumn(['site_code', 'client_id', 'status']);
        });
    }
};
