<?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
    {
        // Platform instances table
        Schema::create('platform_instances', function (Blueprint $table) {
            $table->id();
            $table->enum('platform', ['lemmy']);
            $table->string('url');
            $table->string('name');
            $table->text('description')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->unique(['platform', 'url']);
        });

        // Platform accounts table
        Schema::create('platform_accounts', function (Blueprint $table) {
            $table->id();
            $table->enum('platform', ['lemmy']);
            $table->string('instance_url');
            $table->string('username');
            $table->text('password');
            $table->json('settings')->nullable();
            $table->boolean('is_active')->default(false);
            $table->timestamp('last_tested_at')->nullable();
            $table->string('status')->default('untested');
            $table->timestamps();

            $table->unique(['username', 'platform', 'is_active']);
        });

        // Platform channels table
        Schema::create('platform_channels', function (Blueprint $table) {
            $table->id();
            $table->foreignId('platform_instance_id')->constrained()->onDelete('cascade');
            $table->string('name'); // "technology"
            $table->string('display_name'); // "Technology"
            $table->string('channel_id'); // API ID from platform
            $table->text('description')->nullable();
            $table->foreignId('language_id')->nullable()->constrained();
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->unique(['platform_instance_id', 'name']);
        });

        // Platform account channels pivot table
        Schema::create('platform_account_channels', function (Blueprint $table) {
            $table->id();
            $table->foreignId('platform_account_id')->constrained()->onDelete('cascade');
            $table->foreignId('platform_channel_id')->constrained()->onDelete('cascade');
            $table->boolean('is_active')->default(true);
            $table->integer('priority')->default(0);
            $table->timestamps();

            $table->unique(['platform_account_id', 'platform_channel_id'], 'account_channel_unique');
        });

        // Platform channel posts table (synced from platform APIs for duplicate detection)
        Schema::create('platform_channel_posts', function (Blueprint $table) {
            $table->id();
            $table->string('platform');
            $table->string('channel_id');
            $table->string('channel_name')->nullable();
            $table->string('post_id');
            $table->string('title')->nullable();
            $table->string('url')->nullable();
            $table->timestamp('posted_at')->nullable();
            $table->timestamps();

            $table->unique(['platform', 'channel_id', 'post_id'], 'channel_post_unique');
            $table->index(['platform', 'channel_id', 'url']);
            $table->index(['platform', 'channel_id', 'title']);
        });

        // Language platform instance pivot table
        Schema::create('language_platform_instance', function (Blueprint $table) {
            $table->id();
            $table->foreignId('language_id')->constrained()->onDelete('cascade');
            $table->foreignId('platform_instance_id')->constrained()->onDelete('cascade');
            $table->integer('platform_language_id'); // The platform-specific ID (e.g., Lemmy's language ID) - NOT NULL
            $table->boolean('is_default')->default(false); // Whether this is the default language for this instance
            $table->timestamps();

            $table->unique(['language_id', 'platform_instance_id'], 'lang_platform_instance_unique');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('language_platform_instance');
        Schema::dropIfExists('platform_channel_posts');
        Schema::dropIfExists('platform_account_channels');
        Schema::dropIfExists('platform_channels');
        Schema::dropIfExists('platform_accounts');
        Schema::dropIfExists('platform_instances');
    }
};
