<?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('product_upgrade_paths', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('source_product_id');
            $table->unsignedBigInteger('target_product_id');
            $table->enum('direction', ['upgrade', 'downgrade']);
            $table->json('allowed_periods');
            $table->timestamps();

            $table->foreign('source_product_id')->references('id')->on('products')->onDelete('cascade');
            $table->foreign('target_product_id')->references('id')->on('products')->onDelete('cascade');
            $table->unique(['source_product_id', 'target_product_id', 'direction'], 'product_upgrade_paths_unique');
            $table->index(['source_product_id', 'direction'], 'product_upgrade_paths_source_dir_idx');
        });
    }

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