<?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('diferencias', function (Blueprint $table) {
            $table->id();
            $table->text('observacion')->nullable();
            $table->unsignedInteger('id_turno');
            $table->unsignedInteger('id_tipo_combustible');
            $table->unsignedInteger('id_usuario');
            $table->decimal('cantidad_galones', 12, 3)->default(0);
            $table->decimal('precio', 12, 2)->default(0);
            $table->decimal('total', 14, 2)->default(0);
            $table->tinyInteger('estado')->default(1);

            $table->foreign('id_turno')
                ->references('id_turno')
                ->on('turnos')
                ->onUpdate('cascade')
                ->onDelete('restrict');

            $table->foreign('id_tipo_combustible')
                ->references('id_tipo_combustible')
                ->on('tipos_combustible')
                ->onUpdate('cascade')
                ->onDelete('restrict');

            $table->foreign('id_usuario')
                ->references('id_usuario')
                ->on('usuarios')
                ->onUpdate('cascade')
                ->onDelete('restrict');

            $table->index(['id_turno', 'id_tipo_combustible']);
            $table->index('estado');
        });
    }

    public function down(): void
    {
        Schema::table('diferencias', function (Blueprint $table) {
            $table->dropForeign(['id_turno']);
            $table->dropForeign(['id_tipo_combustible']);
            $table->dropForeign(['id_usuario']);
        });

        Schema::dropIfExists('diferencias');
    }
};
