<?php

use App\Domains\Catalog\Enums\CatalogItemType;
use App\Domains\Catalog\Enums\InventoryPolicy;
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::table('catalog_items', function (Blueprint $table): void {
            $table->enum('type', CatalogItemType::values())
                ->default(CatalogItemType::Standard->value)
                ->after('inventory_id');
            $table->enum('inventory_policy', InventoryPolicy::values())
                ->nullable()
                ->default(InventoryPolicy::Tracked->value)
                ->change();
        });

        Schema::create('bundle_components', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('bundle_catalog_item_id')
                ->constrained('catalog_items')
                ->cascadeOnDelete();
            $table->foreignId('component_catalog_item_id')
                ->constrained('catalog_items')
                ->restrictOnDelete();
            $table->foreignId('component_variant_id')
                ->nullable()
                ->constrained('variantes')
                ->restrictOnDelete();
            $table->unsignedInteger('quantity');
        });
    }

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

        Schema::table('catalog_items', function (Blueprint $table): void {
            $table->dropColumn('type');
            $table->enum('inventory_policy', InventoryPolicy::values())
                ->nullable(false)
                ->default(InventoryPolicy::Tracked->value)
                ->change();
        });
    }
};
