<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('equipment_inspections', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
            $table->unsignedBigInteger('equipment_id')->comment('설비 ID');
            $table->string('year_month', 7)->comment('점검년월 (2026-02)');
            $table->string('overall_judgment', 10)->nullable()->comment('종합판정: OK/NG');
            $table->foreignId('inspector_id')->nullable()->comment('점검자 ID (users.id)');
            $table->text('repair_note')->nullable()->comment('수리내역');
            $table->text('issue_note')->nullable()->comment('이상내용');
            $table->foreignId('created_by')->nullable()->comment('생성자 ID');
            $table->foreignId('updated_by')->nullable()->comment('수정자 ID');
            $table->timestamps();

            $table->unique(['tenant_id', 'equipment_id', 'year_month'], 'uq_inspection_month');
            $table->index(['tenant_id', 'year_month'], 'idx_inspection_ym');

            $table->foreign('equipment_id')
                ->references('id')
                ->on('equipments')
                ->onDelete('cascade');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('equipment_inspections');
    }
};
