<?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('repuestos', function (Blueprint $table) {
            $table->id();
            $table->string('codigo', 40)->unique();
            $table->string('nombre', 200);
            $table->text('descripcion')->nullable();
            $table->foreignId('unidad_id')->nullable()->constrained('unidades')->nullOnDelete();
            $table->decimal('stock', 12, 3)->default(0);
            $table->decimal('stock_minimo', 12, 3)->default(0);
            $table->decimal('costo_unitario', 14, 2)->default(0);
            $table->string('ubicacion', 100)->nullable();
            $table->boolean('activo')->default(true);
            $table->timestamps();
        });

        Schema::create('movimientos_inventario', function (Blueprint $table) {
            $table->id();
            $table->foreignId('repuesto_id')->constrained('repuestos')->cascadeOnDelete();
            $table->foreignId('orden_id')->nullable()->constrained('ordenes_trabajo')->nullOnDelete();
            $table->string('tipo', 20)->default('salida')->comment('entrada, salida, ajuste');
            $table->decimal('cantidad', 12, 3);
            $table->decimal('costo_unitario', 14, 2)->default(0);
            $table->decimal('costo_total', 14, 2)->default(0);
            $table->string('motivo', 200)->nullable();
            $table->foreignId('usuario_id')->nullable()->constrained('usuarios')->nullOnDelete();
            $table->timestamps();
            $table->index(['repuesto_id', 'created_at']);
        });

        Schema::create('ordenes_costos', function (Blueprint $table) {
            $table->id();
            $table->foreignId('orden_id')->constrained('ordenes_trabajo')->cascadeOnDelete();
            $table->string('concepto', 200);
            $table->string('tipo', 20)->default('repuesto')->comment('mano_obra, repuesto, otro');
            $table->decimal('cantidad', 12, 3)->default(1);
            $table->decimal('costo_unitario', 14, 2)->default(0);
            $table->decimal('costo_total', 14, 2)->default(0);
            $table->foreignId('repuesto_id')->nullable()->constrained('repuestos')->nullOnDelete();
            $table->timestamps();
        });

        Schema::create('ordenes_archivos', function (Blueprint $table) {
            $table->id();
            $table->foreignId('orden_id')->constrained('ordenes_trabajo')->cascadeOnDelete();
            $table->string('nombre_original', 255);
            $table->string('ruta', 255);
            $table->string('mime', 100)->nullable();
            $table->unsignedBigInteger('tamano')->default(0);
            $table->string('descripcion', 255)->nullable();
            $table->foreignId('subido_por')->nullable()->constrained('usuarios')->nullOnDelete();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('ordenes_archivos');
        Schema::dropIfExists('ordenes_costos');
        Schema::dropIfExists('movimientos_inventario');
        Schema::dropIfExists('repuestos');
    }
};
