<?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
    {
        if (Schema::hasTable('moraqebat_checklists')) {
            return;
        }

        Schema::create('moraqebat_checklists', function (Blueprint $table) {
            $table->id();
            $table->foreignId('moraqebat_machine_id')->constrained('moraqebat_machines')->cascadeOnDelete();
            $table->string('staff_type', 32);
            $table->unsignedBigInteger('staff_id');
            $table->date('inspection_date');
            $table->string('machine_state', 32);
            $table->text('machine_not_fine_reason')->nullable();
            $table->boolean('is_oiling_checked')->default(false);
            $table->boolean('is_cleanliness_checked')->default(false);
            $table->boolean('is_parts_required')->default(false);
            $table->boolean('is_machine_downtime')->default(false);
            $table->boolean('is_repair_required')->default(false);
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index(['staff_type', 'staff_id']);
            $table->index('inspection_date');
        });
    }

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