<?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
    {
        if (!Schema::hasTable('forum_tags')) {
            Schema::create('forum_tags', function (Blueprint $table): void {
                $table->id();
                $table->string('name', 80);
                $table->string('slug', 80)->unique();
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('forum_topic_tags')) {
            Schema::create('forum_topic_tags', function (Blueprint $table): void {
                $table->id();
                $table->foreignId('topic_id')->constrained('forum_topics')->cascadeOnDelete();
                $table->foreignId('tag_id')->constrained('forum_tags')->cascadeOnDelete();
                $table->timestamps();

                $table->unique(['topic_id', 'tag_id']);
            });
        }
    }

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