<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('sub_plan_options', function (Blueprint $table) {
            $table->bigIncrements('id');

            $table->unsignedBigInteger('sub_plan_id');
            $table->unsignedBigInteger('subscription_plan_option_id');
            $table->string('seats')->nullable();
            $table->string('price');
            $table->unsignedBigInteger('backend_user_id')->nullable();

            $table->nullableTimestamps();

            $table->foreign('sub_plan_id')->references('id')
                ->on('sub_plans')->onDelete('CASCADE')->onUpdate('CASCADE');

            $table->foreign('subscription_plan_option_id')->references('id')
                ->on('subscription_plan_options')->onDelete('RESTRICT')->onUpdate('CASCADE');

            $table->foreign('backend_user_id')->references('id')
                ->on('backend_users')->onDelete('SET NULL')->onUpdate('CASCADE');
        });

        Schema::create('sub_plan_option_languages', function (Blueprint $table) {
            $table->bigIncrements('translation_id');
            $table->unsignedBigInteger('sub_plan_option_id');
            $table->string('language_id', 10);

            $table->string('title', 255);
            $table->string('periodicity', 45)->nullable();

            // Languages
            $table->unique(['sub_plan_option_id', 'language_id'], 'idx_sub_plan_option_language');
            $table->foreign('sub_plan_option_id')
                ->references('id')
                ->on('sub_plan_options')
                ->onDelete('CASCADE')
                ->onUpdate('CASCADE');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('sub_plan_option_languages');
        Schema::dropIfExists('sub_plan_options');
    }
};
