<?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_repairs', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
            $table->unsignedBigInteger('equipment_id')->comment('설비 ID');
            $table->date('repair_date')->comment('수리일');
            $table->string('repair_type', 20)->comment('보전구분: internal/external');
            $table->decimal('repair_hours', 5, 1)->nullable()->comment('수리시간');
            $table->text('description')->nullable()->comment('수리내용');
            $table->decimal('cost', 15, 2)->nullable()->comment('수리비용');
            $table->string('vendor', 100)->nullable()->comment('외주업체');
            $table->foreignId('repaired_by')->nullable()->comment('수리자 ID (users.id)');
            $table->text('memo')->nullable()->comment('비고');
            $table->foreignId('created_by')->nullable()->comment('생성자 ID');
            $table->foreignId('updated_by')->nullable()->comment('수정자 ID');
            $table->timestamps();
            $table->softDeletes();

            $table->index(['tenant_id', 'repair_date'], 'idx_repair_date');
            $table->index(['tenant_id', 'equipment_id'], 'idx_repair_equipment');

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

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