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

            // 기본 정보
            $table->string('briefing_code', 50)->nullable()->comment('현설번호 (자동생성)');
            $table->string('title', 200)->comment('현장설명회명/현장명');
            $table->text('description')->nullable()->comment('설명/업무보고');

            // 거래처/현장 연결
            $table->foreignId('partner_id')->nullable()->comment('거래처 ID');
            $table->foreignId('site_id')->nullable()->comment('현장 ID');

            // 일정 정보
            $table->date('briefing_date')->comment('현장설명회 일자');
            $table->string('briefing_time', 10)->nullable()->comment('현장설명회 시간 (HH:MM)');
            $table->enum('briefing_type', ['online', 'offline'])->default('offline')->comment('구분: online|offline');
            $table->string('location', 200)->nullable()->comment('현장설명회 장소');
            $table->string('address', 255)->nullable()->comment('주소');

            // 상태 정보
            $table->enum('status', ['scheduled', 'ongoing', 'completed', 'cancelled', 'postponed'])
                ->default('scheduled')->comment('상태: scheduled|ongoing|completed|cancelled|postponed');
            $table->enum('bid_status', ['pending', 'bidding', 'closed', 'failed', 'awarded'])
                ->default('pending')->comment('입찰상태: pending|bidding|closed|failed|awarded');
            $table->date('bid_date')->nullable()->comment('입찰일자');

            // 참석자 정보
            $table->string('attendee', 100)->nullable()->comment('담당 참석자');
            $table->enum('attendance_status', ['scheduled', 'attended', 'absent'])
                ->default('scheduled')->comment('참석상태: scheduled|attended|absent');
            $table->integer('attendee_count')->default(0)->comment('총 참석자 수');

            // 공사 정보
            $table->integer('site_count')->default(0)->comment('개소 수');
            $table->date('construction_start_date')->nullable()->comment('공사기간 시작');
            $table->date('construction_end_date')->nullable()->comment('공사기간 종료');
            $table->enum('vat_type', ['excluded', 'included'])->default('excluded')->comment('부가세: excluded|included');

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

            // 인덱스
            $table->index('tenant_id');
            $table->index('partner_id');
            $table->index('site_id');
            $table->index('briefing_date');
            $table->index('status');
            $table->index('bid_status');
            $table->unique(['tenant_id', 'briefing_code']);
        });
    }

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