<?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('planes_mantenimiento', function (Blueprint $table) {
            $table->id();
            $table->string('codigo', 30)->unique();
            $table->string('nombre', 200);
            $table->string('clase', 20)->default('preventivo')->comment('preventivo, correctivo');
            $table->foreignId('area_id')->nullable()->constrained('areas')->nullOnDelete();
            $table->foreignId('sistema_id')->nullable()->constrained('sistemas')->nullOnDelete();
            $table->foreignId('equipo_id')->nullable()->constrained('equipos')->nullOnDelete();
            $table->date('fecha_inicio')->nullable();
            $table->date('fecha_fin')->nullable();
            $table->text('descripcion')->nullable();
            $table->text('observaciones')->nullable();
            $table->boolean('activo')->default(true);
            $table->foreignId('creado_por')->nullable()->constrained('usuarios')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('planes_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('plan_id')->constrained('planes_mantenimiento')->cascadeOnDelete();
            $table->foreignId('componente_id')->nullable()->constrained('componentes')->nullOnDelete();
            $table->integer('numero_item')->default(0);
            $table->text('descripcion');
            $table->string('cambio_lubricacion', 100)->nullable()->comment('Texto: cambio o lubricacion');
            $table->string('codigo_componente', 40)->nullable();
            $table->foreignId('unidad_id')->nullable()->constrained('unidades')->nullOnDelete();
            $table->decimal('cantidad', 12, 3)->default(0);
            $table->foreignId('frecuencia_id')->nullable()->constrained('frecuencias')->nullOnDelete();
            // Indicadores LILA + cambio (boolean por celda como en Excel)
            $table->boolean('lila_l_limpieza')->default(false);
            $table->boolean('lila_i_inspeccion')->default(false);
            $table->boolean('lila_l_lubricacion')->default(false);
            $table->boolean('lila_a_ajuste')->default(false);
            // Marcas de PM (frecuencias en horas)
            $table->boolean('pm_360')->default(false)->comment('PM 01 - Quincenal 360h');
            $table->boolean('pm_720')->default(false)->comment('PM 02 - Mensual 720h');
            $table->boolean('pm_4320')->default(false)->comment('PM 03 - Semestral 4320h');
            $table->boolean('pm_8640')->default(false)->comment('PM 04 - Anual 8640h');
            $table->foreignId('tipo_mantenimiento_id')->nullable()->constrained('tipos_mantenimiento')->nullOnDelete();
            $table->foreignId('responsable_id')->nullable()->constrained('usuarios')->nullOnDelete();
            $table->text('observaciones')->nullable();
            $table->decimal('costo_estimado', 14, 2)->default(0);
            $table->decimal('costo_promedio', 14, 2)->default(0);
            $table->decimal('costo_actual', 14, 2)->default(0);
            $table->boolean('activo')->default(true);
            $table->timestamps();
            $table->index(['plan_id', 'numero_item']);
        });
    }

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