<?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('crawl_logs', function (Blueprint $table) {
            $table->id();
            $table->unsignedInteger('source_id');
            $table->enum('status', ['success', 'empty', 'error', 'blocked']);
            $table->unsignedSmallInteger('articles_found')->default(0);
            $table->unsignedSmallInteger('articles_saved')->default(0);
            $table->unsignedSmallInteger('articles_skipped')->default(0);   // duplicates
            $table->text('error_message')->nullable();
            $table->timestamp('started_at');
            $table->timestamp('finished_at')->nullable();
            $table->unsignedSmallInteger('duration_seconds')->nullable();

            $table->foreign('source_id')->references('id')->on('sources')->cascadeOnDelete();
            $table->index(['source_id', 'started_at']);
        });
    }

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