<?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::dropIfExists('ticket_validity_group_times');
        Schema::dropIfExists('ticket_validity_groups');
    }

    public function down(): void
    {
        Schema::create('ticket_validity_groups', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('ticket_id')
                ->constrained('tickets')
                ->cascadeOnUpdate()
                ->cascadeOnDelete();
        });

        Schema::create('ticket_validity_group_times', function (Blueprint $table): void {
            $table->foreignId('ticket_validity_group_id')
                ->constrained('ticket_validity_groups')
                ->cascadeOnUpdate()
                ->cascadeOnDelete();
            $table->foreignId('validity_time_id')
                ->constrained('validity_times')
                ->cascadeOnUpdate()
                ->restrictOnDelete();
            $table->primary(['ticket_validity_group_id', 'validity_time_id']);
        });
    }
};
