<?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('product_categories', function (Blueprint $table) {
            $table->id();

            // Agency relationship
            $table->unsignedBigInteger('agency_id')->nullable();
            $table->unsignedBigInteger('product_type_id')->nullable();

            // Hierarchy support
            $table->unsignedBigInteger('parent_id')->nullable();
            $table->integer('level')->default(0);
            $table->string('path')->nullable(); // For nested set model

            // Basic Information
            $table->string('code', 20)->unique();
            $table->string('name');
            $table->string('slug')->unique();
            $table->text('description')->nullable();
            $table->text('short_description')->nullable();

            // Media
            $table->string('image')->nullable();
            $table->string('icon')->nullable();
            $table->json('gallery')->nullable();

            // Display Settings
            $table->integer('sort_order')->default(0);
            $table->string('color_code', 7)->nullable(); // Hex color
            $table->boolean('is_featured')->default(false);
            $table->boolean('show_on_homepage')->default(false);

            // SEO
            $table->string('meta_title')->nullable();
            $table->text('meta_description')->nullable();
            $table->string('meta_keywords')->nullable();
            $table->json('meta_data')->nullable(); // Additional meta tags

            // Configuration
            $table->json('settings')->nullable(); // Category specific settings
            $table->json('filters')->nullable(); // Available filters for this category

            // Status
            $table->boolean('is_active')->default(true);
            $table->boolean('is_public')->default(true);

            // Analytics
            $table->unsignedInteger('view_count')->default(0);
            $table->unsignedInteger('tour_count')->default(0);

            // Audit Fields
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->timestamps();
            $table->softDeletes();

            // Foreign Keys
            $table->foreign('agency_id')->references('id')->on('agencies')->onDelete('cascade');
            $table->foreign('product_type_id')->references('id')->on('product_types')->onDelete('cascade');
            $table->foreign('parent_id')->references('id')->on('product_categories')->onDelete('cascade');
            $table->foreign('created_by')->references('id')->on('users')->onDelete('set null');
            $table->foreign('updated_by')->references('id')->on('users')->onDelete('set null');

            // Indexes
            $table->index(['agency_id', 'is_active']);
            $table->index(['product_type_id', 'agency_id']);
            $table->index(['parent_id', 'sort_order']);
            $table->index(['slug', 'is_active']);
            $table->index(['is_featured', 'is_active']);
            $table->index(['level', 'path']);
            $table->index(['created_at']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('product_categories');
    }
};
