<?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
    {
        if (!Schema::hasTable('product_variants')) {
            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');
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('product_variants');
    }
};
