<?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('handover_reports', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');

            // 보고서 기본 정보
            $table->string('report_number', 50)->comment('보고서번호');
            $table->unsignedBigInteger('contract_id')->nullable()->comment('연결 계약 ID');
            $table->string('site_name')->comment('현장명');

            // 거래처 정보
            $table->unsignedBigInteger('partner_id')->nullable()->comment('거래처 ID');
            $table->string('partner_name')->nullable()->comment('거래처명');

            // 담당자 정보
            $table->unsignedBigInteger('contract_manager_id')->nullable()->comment('계약담당자 ID');
            $table->string('contract_manager_name')->nullable()->comment('계약담당자명');
            $table->unsignedBigInteger('construction_pm_id')->nullable()->comment('공사PM ID');
            $table->string('construction_pm_name')->nullable()->comment('공사PM명');

            // 계약 상세
            $table->integer('total_sites')->default(0)->comment('총 개소');
            $table->decimal('contract_amount', 15, 2)->default(0)->comment('계약금액');
            $table->date('contract_date')->nullable()->comment('계약일자');
            $table->date('contract_start_date')->nullable()->comment('계약시작일');
            $table->date('contract_end_date')->nullable()->comment('계약종료일');
            $table->date('completion_date')->nullable()->comment('준공일');

            // 상태 정보
            $table->string('status', 20)->default('pending')->comment('인수인계상태: pending, completed');

            // 2차 배관
            $table->boolean('has_secondary_piping')->default(false)->comment('2차 배관 유무');
            $table->decimal('secondary_piping_amount', 15, 2)->default(0)->comment('2차 배관 금액');
            $table->text('secondary_piping_note')->nullable()->comment('2차 배관 비고');

            // 도장 & 코킹
            $table->boolean('has_coating')->default(false)->comment('도장 & 코킹 유무');
            $table->decimal('coating_amount', 15, 2)->default(0)->comment('도장 & 코킹 금액');
            $table->text('coating_note')->nullable()->comment('도장 & 코킹 비고');

            // 장비 외 실행금액 (JSON)
            $table->json('external_equipment_cost')->nullable()->comment('장비 외 실행금액: shipping_cost, high_altitude_work, public_expense');

            // 특이사항
            $table->text('special_notes')->nullable()->comment('특이사항');

            // 활성화 상태
            $table->boolean('is_active')->default(true)->comment('활성화 여부');

            // 감사 컬럼
            $table->unsignedBigInteger('created_by')->nullable()->comment('생성자 ID');
            $table->unsignedBigInteger('updated_by')->nullable()->comment('수정자 ID');
            $table->unsignedBigInteger('deleted_by')->nullable()->comment('삭제자 ID');

            // 타임스탬프
            $table->timestamps();
            $table->softDeletes();

            // 인덱스
            $table->index('tenant_id');
            $table->index('contract_id');
            $table->index('partner_id');
            $table->index('status');
            $table->unique(['tenant_id', 'report_number']);
        });
    }

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