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

            // 기본 정보
            $table->string('bidding_code', 50)->comment('입찰번호 (예: BID-2025-001)');
            $table->foreignId('quote_id')->nullable()->comment('연결된 견적 ID (quotes.id)');

            // 거래처/현장
            $table->unsignedBigInteger('client_id')->nullable()->comment('거래처 ID');
            $table->string('client_name', 100)->nullable()->comment('거래처명 (스냅샷)');
            $table->string('project_name', 200)->nullable()->comment('현장명');

            // 입찰 정보
            $table->date('bidding_date')->nullable()->comment('입찰일');
            $table->date('bid_date')->nullable()->comment('입찰일 (레거시 호환)');
            $table->date('submission_date')->nullable()->comment('투찰일');
            $table->date('confirm_date')->nullable()->comment('확정일');
            $table->unsignedInteger('total_count')->default(0)->comment('총 개소');
            $table->decimal('bidding_amount', 15, 2)->default(0)->comment('입찰금액');

            // 상태 (waiting/submitted/failed/invalid/awarded/hold)
            $table->string('status', 20)->default('waiting')->comment('상태');

            // 입찰자
            $table->unsignedBigInteger('bidder_id')->nullable()->comment('입찰자 ID');
            $table->string('bidder_name', 50)->nullable()->comment('입찰자명 (스냅샷)');

            // 공사기간
            $table->date('construction_start_date')->nullable()->comment('공사 시작일');
            $table->date('construction_end_date')->nullable()->comment('공사 종료일');
            $table->string('vat_type', 20)->default('excluded')->comment('부가세 (included/excluded)');

            // 비고
            $table->text('remarks')->nullable()->comment('비고');

            // 견적 데이터 스냅샷 (JSON)
            $table->json('expense_items')->nullable()->comment('공과 항목 스냅샷');
            $table->json('estimate_detail_items')->nullable()->comment('견적 상세 항목 스냅샷');

            // 감사
            $table->foreignId('created_by')->nullable()->comment('생성자');
            $table->foreignId('updated_by')->nullable()->comment('수정자');
            $table->foreignId('deleted_by')->nullable()->comment('삭제자');
            $table->timestamps();
            $table->softDeletes();

            // 인덱스
            $table->index('tenant_id', 'idx_biddings_tenant_id');
            $table->index('status', 'idx_biddings_status');
            $table->index('bidding_date', 'idx_biddings_bidding_date');
            $table->index('quote_id', 'idx_biddings_quote_id');
            $table->unique(['tenant_id', 'bidding_code', 'deleted_at'], 'uq_tenant_bidding_code');
        });
    }

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