<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('tipos_impuesto', function (Blueprint $table) {
            $table->id();
            $table->string('nombre')->unique();
            $table->decimal('porcentaje', 5, 2);
            $table->boolean('activo')->default(true);
            $table->boolean('predeterminado')->default(false);
            $table->timestamps();
        });

        $ivaAnterior = (float) (DB::table('configuracion_iva')->value('valor') ?? 12);

        DB::table('tipos_impuesto')->insert([
            ['nombre' => 'IVA', 'porcentaje' => $ivaAnterior, 'activo' => true, 'predeterminado' => true, 'created_at' => now(), 'updated_at' => now()],
            ['nombre' => 'Exento', 'porcentaje' => 0, 'activo' => true, 'predeterminado' => false, 'created_at' => now(), 'updated_at' => now()],
        ]);

        Schema::dropIfExists('configuracion_iva');
    }

    public function down(): void
    {
        Schema::create('configuracion_iva', function (Blueprint $table) {
            $table->id();
            $table->decimal('valor', 5, 2);
            $table->timestamps();
        });

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