<?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('venta_detalles', function (Blueprint $table) {
            $table->id();
            $table->foreignId('venta_id')->constrained('ventas')->cascadeOnDelete();
            $table->enum('tipo', ['repuesto', 'servicio']);
            $table->unsignedBigInteger('item_id')->nullable(); // id de Repuesto o Servicio (referencia informativa)
            $table->string('codigo')->nullable();
            $table->string('descripcion');
            $table->decimal('cantidad', 12, 3)->default(1);
            $table->decimal('precio_unitario', 12, 4)->default(0);
            $table->decimal('descuento', 12, 2)->default(0);
            $table->decimal('porcentaje_iva', 5, 2)->default(15); // 0 o la tarifa vigente (ej. 15.00)
            $table->decimal('subtotal', 12, 2)->default(0); // cantidad*precio - descuento (base imponible)
            $table->decimal('iva', 12, 2)->default(0);
            $table->decimal('total', 12, 2)->default(0);
            $table->timestamps();
        });
    }

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