<?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_warehouses', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->string('code', 64);
            $table->string('name');
            $table->json('address')->nullable();
            $table->boolean('is_active')->default(true);
            $table->string('lifecycle_state', 32)->default('draft');
            $table->timestamp('deactivated_at')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->unique(['tenant_id', 'code'], 'inv_wh_tenant_code_uq');
            $table->index(['tenant_id', 'is_active', 'lifecycle_state'], 'inv_wh_tenant_active_lc');
        });

        Schema::create('inventory_warehouse_sections', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('warehouse_id')->constrained('inventory_warehouses')->cascadeOnDelete();
            $table->string('code', 64);
            $table->string('name');
            $table->boolean('is_active')->default(true);
            $table->unsignedInteger('sort_order')->default(0);
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->unique(['warehouse_id', 'code'], 'inv_wh_sec_wh_code_uq');
            $table->index(['tenant_id', 'warehouse_id', 'is_active'], 'inv_wh_sec_tnt_wh_act');
        });

        Schema::create('inventory_units', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->string('code', 32);
            $table->string('name');
            $table->string('symbol', 16)->nullable();
            $table->boolean('is_base')->default(false);
            $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_unit_tenant_code_uq');
            $table->index(['tenant_id', 'is_active'], 'inv_unit_tenant_active');
        });

        Schema::create('inventory_unit_conversions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('from_unit_id')->constrained('inventory_units')->cascadeOnDelete();
            $table->foreignId('to_unit_id')->constrained('inventory_units')->cascadeOnDelete();
            $table->decimal('factor', 24, 12);
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->unique(['tenant_id', 'from_unit_id', 'to_unit_id'], 'inv_unit_conv_uq');
            $table->index(['tenant_id', 'is_active'], 'inv_unit_conv_tnt_act');
        });

        Schema::create('inventory_price_types', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->string('code', 64);
            $table->string('name');
            $table->string('direction', 16);
            $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_price_type_tnt_code_uq');
            $table->index(['tenant_id', 'direction', 'is_active'], 'inv_price_type_tnt_dir');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('inventory_price_types');
        Schema::dropIfExists('inventory_unit_conversions');
        Schema::dropIfExists('inventory_units');
        Schema::dropIfExists('inventory_warehouse_sections');
        Schema::dropIfExists('inventory_warehouses');
    }
};
