<?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::create('sources', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedSmallInteger('country_id');
            $table->unsignedTinyInteger('category_id');
            $table->string('name', 150);
            $table->string('rss_url', 500)->nullable();
            $table->string('website_url', 500)->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamp('last_crawled_at')->nullable();
            $table->enum('last_status', ['ok', 'empty', 'error', 'blocked'])->nullable();
            $table->unsignedInteger('articles_fetched')->default(0);     // total ever fetched
            $table->timestamps();

            $table->foreign('country_id')
                  ->references('id')->on('countries')
                  ->cascadeOnDelete();

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

            $table->index(['country_id', 'category_id']);
            $table->index('is_active');
        });
    }

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