<?php

declare(strict_types=1);

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('subscription_config_selections', function (Blueprint $table) {
            $table->id();
            $table->foreignId('subscription_id')->constrained()->cascadeOnDelete();
            $table->unsignedBigInteger('option_id');
            $table->unsignedBigInteger('value_id')->nullable();
            $table->integer('quantity')->nullable();
            $table->string('text_value', 500)->nullable();
            $table->decimal('locked_price', 10, 4);
            $table->decimal('locked_hourly_price', 10, 4)->nullable();
            $table->string('billing_cycle', 50);
            $table->boolean('is_custom_build')->default(false);
            $table->timestamps();

            $table->foreign('option_id')
                ->references('id')
                ->on('plan_config_options')
                ->restrictOnDelete();

            $table->foreign('value_id')
                ->references('id')
                ->on('plan_config_values')
                ->nullOnDelete();

            $table->unique(['subscription_id', 'option_id']);
        });
    }

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