<?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
    {
        // Articles table (without feed_id foreign key initially)
        Schema::create('articles', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('description')->nullable();
            $table->longText('content')->nullable();
            $table->string('url')->nullable();
            $table->string('image_url')->nullable();
            $table->timestamp('published_at')->nullable();
            $table->string('author')->nullable();
            $table->unsignedBigInteger('feed_id')->nullable();
            $table->enum('approval_status', ['pending', 'approved', 'rejected'])->default('pending');
            $table->timestamp('validated_at')->nullable();
            $table->enum('publish_status', ['unpublished', 'publishing', 'published', 'error'])->default('unpublished');
            $table->timestamps();

            $table->index(['published_at', 'approval_status']);
            $table->index('feed_id');
            $table->unique('url');
        });

        // Article publications table
        Schema::create('article_publications', function (Blueprint $table) {
            $table->id();
            $table->foreignId('article_id')->constrained()->onDelete('cascade');
            $table->string('post_id');
            $table->unsignedBigInteger('platform_channel_id');
            $table->string('platform')->default('lemmy');
            $table->json('publication_data')->nullable();
            $table->timestamp('published_at');
            $table->string('published_by');
            $table->timestamps();

            $table->unique(['article_id', 'platform', 'platform_channel_id'], 'article_pub_unique');
        });

        // Logs table
        Schema::create('logs', function (Blueprint $table) {
            $table->id();
            $table->string('level'); // info, warning, error, etc.
            $table->string('message');
            $table->json('context')->nullable(); // Additional context data
            $table->timestamp('logged_at')->useCurrent();
            $table->timestamps();

            $table->index(['level', 'logged_at']);
        });

        // Settings table
        Schema::create('settings', function (Blueprint $table) {
            $table->id();
            $table->string('key')->unique();
            $table->text('value')->nullable();
            $table->string('type')->default('string'); // string, integer, boolean, json
            $table->text('description')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('article_publications');
        Schema::dropIfExists('articles');
        Schema::dropIfExists('logs');
        Schema::dropIfExists('settings');
    }
};
