<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('inventory_attributes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->string('code', 120);
            $table->string('name');
            $table->string('type', 32);
            $table->boolean('is_active')->default(true);
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->unique(['tenant_id', 'code'], 'inv_attr_tenant_code_uq');
            $table->index(['tenant_id', 'type', 'is_active'], 'inv_attr_tnt_type_act');
        });

        Schema::create('inventory_attribute_options', function (Blueprint $table) {
            $table->id();
            $table->foreignId('attribute_id')->constrained('inventory_attributes')->cascadeOnDelete();
            $table->string('value_key', 120);
            $table->string('label');
            $table->unsignedInteger('sort_order')->default(0);
            $table->timestamps();

            $table->unique(['attribute_id', 'value_key'], 'inv_attr_opt_attr_key_uq');
            $table->index(['attribute_id', 'sort_order'], 'inv_attr_opt_attr_sort');
        });

        Schema::create('inventory_category_attributes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('category_id')->constrained('inventory_categories')->cascadeOnDelete();
            $table->foreignId('attribute_id')->constrained('inventory_attributes')->cascadeOnDelete();
            $table->boolean('is_required')->default(false);
            $table->unsignedInteger('sort_order')->default(0);
            $table->json('default_payload')->nullable();
            $table->timestamps();

            $table->unique(['category_id', 'attribute_id'], 'inv_cat_attr_cat_attr_uq');
        });

        Schema::create('inventory_item_attribute_values', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('item_id')->constrained('inventory_items')->cascadeOnDelete();
            $table->foreignId('attribute_id')->constrained('inventory_attributes')->cascadeOnDelete();
            $table->string('value_text', 500)->nullable();
            $table->text('value_textarea')->nullable();
            $table->decimal('value_number', 24, 8)->nullable();
            $table->boolean('value_boolean')->nullable();
            $table->date('value_date')->nullable();
            $table->json('value_json')->nullable();
            $table->timestamps();

            $table->unique(['item_id', 'attribute_id'], 'inv_item_attr_val_uq');
            $table->index(['tenant_id', 'item_id'], 'inv_item_attr_val_tnt_item');
        });

        Schema::create('inventory_item_prices', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('item_id')->constrained('inventory_items')->cascadeOnDelete();
            $table->foreignId('price_type_id')->constrained('inventory_price_types')->cascadeOnDelete();
            $table->char('currency', 3);
            $table->decimal('amount', 18, 4);
            $table->decimal('min_quantity', 18, 6)->nullable();
            $table->date('effective_from')->nullable();
            $table->date('effective_to')->nullable();
            $table->boolean('is_active')->default(true);
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index(
                ['tenant_id', 'item_id', 'price_type_id', 'currency', 'is_active'],
                'inv_item_price_lookup'
            );
            $table->index(['item_id', 'effective_from', 'effective_to'], 'inv_item_price_eff');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('inventory_item_prices');
        Schema::dropIfExists('inventory_item_attribute_values');
        Schema::dropIfExists('inventory_category_attributes');
        Schema::dropIfExists('inventory_attribute_options');
        Schema::dropIfExists('inventory_attributes');
    }
};
