<?php

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

class CreateProductsTable20250428051003 extends Migration
{
    public function up(): void
    {
        Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('section_id');
            $table->unsignedBigInteger('category_id');
            $table->unsignedBigInteger('brand_id');
            $table->unsignedBigInteger('vendor_id'); // Foreign key to be added later
            $table->unsignedBigInteger('admin_id'); // Foreign key to be added later
            $table->enum('admin_type', ['superadmin', 'vendor']);
            $table->string('name');
            $table->string('product_code', 50)->unique();
            $table->string('product_color', 50)->nullable();
            $table->decimal('price', 10, 2);
            $table->decimal('discount', 5, 2)->default(0.00);
            $table->decimal('weight', 10, 2);
            $table->enum('weight_unit', ['grams', 'kg']);
            $table->decimal('length', 10, 2)->nullable();
            $table->decimal('width', 10, 2)->nullable();
            $table->decimal('height', 10, 2)->nullable();
            $table->integer('moq')->default(1);
            $table->enum('sell_as', ['single_item', 'bulk_pack', 'slab', 'carton', 'custom'])->default('single_item');
            $table->integer('items_per_pack')->nullable();
            $table->string('pack_label')->nullable();
            $table->string('product_image')->nullable();
            $table->string('product_video')->nullable();
            $table->text('description');
            $table->string('occasion', 50)->nullable();
            $table->string('fit', 50)->nullable();
            $table->string('pattern', 50)->nullable();
            $table->string('sleeve', 50)->nullable();
            $table->string('fabric', 50)->nullable();
            $table->enum('status', ['pending', 'approved', 'disapproved', 'draft'])->default('pending');
            $table->boolean('is_active')->default(true);
            $table->enum('is_featured', ['No', 'Yes'])->default('No');
            $table->enum('is_bestseller', ['No', 'Yes'])->default('No');
            $table->enum('is_trending', ['No', 'Yes'])->default('No');
            $table->integer('pallet_size');
            $table->integer('stock')->nullable();
            $table->integer('low_stock_threshold')->default(10);
            $table->decimal('product_commission', 5, 2)->nullable();
            $table->decimal('category_commission', 5, 2)->nullable();
            $table->decimal('final_commission', 5, 2)->nullable();
            $table->timestamp('submitted_on')->useCurrent();
            $table->string('seo_title', 70)->nullable();
            $table->string('seo_description', 160)->nullable();
            $table->string('seo_keywords')->nullable();
            $table->string('seo_canonical_url')->nullable();
            $table->string('seo_robots', 50)->default('index, follow');
            $table->string('seo_og_title', 70)->nullable();
            $table->string('seo_og_description', 160)->nullable();
            $table->string('seo_og_image')->nullable();
            $table->timestamps();

            $table->foreign('section_id')->references('id')->on('sections')->onDelete('restrict');
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('restrict');
            $table->foreign('brand_id')->references('id')->on('brands')->onDelete('restrict');
            // $table->foreign('vendor_id')->references('id')->on('vendors')->onDelete('cascade');
            // $table->foreign('admin_id')->references('id')->on('users')->onDelete('restrict');

            $table->index('status');
            $table->index('submitted_on');
            $table->index('vendor_id');
            $table->index('category_id');
            $table->index('section_id');
            $table->index('brand_id');
            $table->index('seo_title');
            $table->index('is_bestseller');
            $table->index('is_trending');
            $table->index('created_at');
            $table->fullText(['name', 'description']);
        });
    }

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