<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     * 
     * Note: This should be run AFTER all data has been migrated and
     * all foreign key references have been removed.
     */
    public function up(): void
    {
        if (Schema::hasTable('product_variants')) {
            Schema::dropIfExists('product_variants');
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Recreate the product_variants table structure
        // Note: This is a simplified recreation - actual structure may vary
        Schema::create('product_variants', function (Blueprint $table) {
            $table->id();
            $table->foreignId('product_id')->constrained()->onDelete('cascade');
            $table->string('sku', 191)->unique();
            $table->string('item_code', 191)->nullable()->unique();
            $table->decimal('price', 10, 2)->comment('Sales price');
            $table->decimal('purchase_price', 12, 2)->nullable();
            $table->decimal('wholesale_price', 10, 2)->nullable();
            $table->decimal('mrp', 10, 2)->nullable();
            $table->integer('current_stock')->default(0);
            $table->integer('opening_stock')->default(0);
            $table->string('barcode', 191)->nullable()->unique();
            $table->string('image_path')->nullable();
            $table->string('hsn_code')->nullable()->comment('For tax classification');
            $table->integer('warranty_months')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->index('product_id');
            $table->index('sku');
            $table->index('item_code');
            $table->index('barcode');
        });
    }
};
