<?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('areas', function (Blueprint $table) {
            $table->id();
            $table->string('codigo', 20)->unique();
            $table->string('nombre', 150);
            $table->text('descripcion')->nullable();
            $table->string('color', 10)->nullable();
            $table->boolean('activo')->default(true);
            $table->timestamps();
        });

        Schema::create('sistemas', function (Blueprint $table) {
            $table->id();
            $table->foreignId('area_id')->constrained('areas')->cascadeOnUpdate()->restrictOnDelete();
            $table->string('codigo', 30);
            $table->string('nombre', 150);
            $table->text('descripcion')->nullable();
            $table->boolean('activo')->default(true);
            $table->timestamps();
            $table->unique(['area_id', 'codigo']);
        });

        Schema::create('equipos', function (Blueprint $table) {
            $table->id();
            $table->foreignId('area_id')->constrained('areas')->cascadeOnUpdate()->restrictOnDelete();
            $table->foreignId('sistema_id')->nullable()->constrained('sistemas')->nullOnDelete();
            $table->string('codigo', 40)->unique();
            $table->string('nombre', 200);
            $table->text('descripcion')->nullable();
            $table->string('marca', 100)->nullable();
            $table->string('modelo', 100)->nullable();
            $table->string('serie', 100)->nullable();
            $table->string('ubicacion', 200)->nullable();
            $table->date('fecha_instalacion')->nullable();
            $table->decimal('horometro_actual', 12, 2)->default(0);
            $table->string('imagen', 255)->nullable();
            $table->boolean('activo')->default(true);
            $table->timestamps();
        });

        Schema::create('componentes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('equipo_id')->constrained('equipos')->cascadeOnDelete();
            $table->string('codigo', 40);
            $table->string('nombre', 200);
            $table->text('descripcion')->nullable();
            $table->string('unidad', 30)->nullable();
            $table->boolean('activo')->default(true);
            $table->timestamps();
            $table->unique(['equipo_id', 'codigo']);
        });

        Schema::create('tipos_mantenimiento', function (Blueprint $table) {
            $table->id();
            $table->string('codigo', 20)->unique();
            $table->string('nombre', 100);
            $table->string('clase', 20)->default('preventivo')->comment('preventivo, correctivo, predictivo, otro');
            $table->text('descripcion')->nullable();
            $table->boolean('activo')->default(true);
            $table->timestamps();
        });

        Schema::create('frecuencias', function (Blueprint $table) {
            $table->id();
            $table->string('codigo', 20)->unique();
            $table->string('nombre', 80);
            $table->integer('dias')->nullable()->comment('Equivalente en dias calendario');
            $table->integer('horas_pm')->nullable()->comment('Equivalente en horas de operacion (PM)');
            $table->boolean('activo')->default(true);
            $table->timestamps();
        });

        Schema::create('unidades', function (Blueprint $table) {
            $table->id();
            $table->string('codigo', 20)->unique();
            $table->string('nombre', 80);
            $table->boolean('activo')->default(true);
            $table->timestamps();
        });

        Schema::create('estados_orden', function (Blueprint $table) {
            $table->id();
            $table->string('codigo', 20)->unique();
            $table->string('nombre', 60);
            $table->string('color', 10)->default('#6c757d');
            $table->integer('orden')->default(0);
            $table->boolean('cierra_orden')->default(false);
            $table->boolean('activo')->default(true);
            $table->timestamps();
        });

        Schema::create('prioridades', function (Blueprint $table) {
            $table->id();
            $table->string('codigo', 20)->unique();
            $table->string('nombre', 60);
            $table->string('color', 10)->default('#6c757d');
            $table->integer('nivel')->default(1);
            $table->boolean('activo')->default(true);
            $table->timestamps();
        });

        Schema::create('actividades_lila', function (Blueprint $table) {
            $table->id();
            $table->string('codigo', 10)->unique()->comment('L, I, L2, A, etc.');
            $table->string('nombre', 80);
            $table->string('icono', 30)->nullable();
            $table->boolean('activo')->default(true);
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('actividades_lila');
        Schema::dropIfExists('prioridades');
        Schema::dropIfExists('estados_orden');
        Schema::dropIfExists('unidades');
        Schema::dropIfExists('frecuencias');
        Schema::dropIfExists('tipos_mantenimiento');
        Schema::dropIfExists('componentes');
        Schema::dropIfExists('equipos');
        Schema::dropIfExists('sistemas');
        Schema::dropIfExists('areas');
    }
};
