<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('rss_posts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('category_id')->nullable();
            $table->string('source');
            $table->string('source_url');
            $table->string('post_url');
            $table->string('title');
            $table->string('slug');
            $table->text('body')->nullable();
            $table->jsonb('keywords')->nullable();
            $table->jsonb('entities')->nullable();
            $table->json('metadata')->nullable();
            $table->mediumText('keyword_list')->nullable();
            $table->mediumText('bites')->nullable();
            $table->mediumText('impact')->nullable();
            $table->enum('impact_level', ['low', 'medium', 'high'])->default('low');
            $table->datetime('published_at');
            $table->enum('status', ['draft', 'published', 'blocked', 'trashed'])->default('draft');
            $table->timestamps();

            $table->index('title');
            $table->index('slug');
            $table->index('published_at');

            $table->foreign('category_id')->references('id')->on('categories');
        });

        DB::statement('CREATE INDEX idx_rss_posts_entities ON rss_posts USING gin (entities jsonb_path_ops)');

        DB::statement('CREATE INDEX idx_rss_posts_keywords ON rss_posts USING gin (keywords jsonb_path_ops)');

    }

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