<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('productos', function (Blueprint $table) {
            $table->id();
            $table->string('nombre', 100);
            $table->text('descripcion');
            $table->string('codigo_barras', 50)->unique();
            $table->string('precio_compra');
            $table->string('precio_venta');
            $table->integer('stock')->default(0);
            $table->unsignedBigInteger('categoria_id'); // Cambio a unsignedBigInteger
            $table->boolean('estado')->default(true);
            $table->string('imagen');
            $table->timestamps();

            // Definir correctamente la clave foránea
            $table->foreign('categoria_id')->references('id')->on('categorias')->onDelete('cascade')->onUpdate('cascade');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Verificar si las tablas existen antes de eliminar las claves foráneas
        if (Schema::hasTable('detalle_ventas')) {
            Schema::table('detalle_ventas', function (Blueprint $table) {
                $table->dropForeign(['producto_id']);
            });
        }
    
        if (Schema::hasTable('detalle_compras')) {
            Schema::table('detalle_compras', function (Blueprint $table) {
                $table->dropForeign(['producto_id']);
            });
        }
    
        Schema::dropIfExists('productos');
    }
    

    
};
