<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Services\Rss\RssIngestionService;
use Illuminate\Console\Command;

final class SyncRssFeedsCommand extends Command
{
    protected $signature = 'rss:sync {source? : Optional rss_feed_sources.id}';

    protected $description = 'Fetch configured RSS/Atom feeds and store headline + link rows (no full article republishing).';

    public function handle(RssIngestionService $ingestion): int
    {
        $arg = $this->argument('source');
        $onlyId = $arg !== null && $arg !== '' ? (int) $arg : null;

        if ($onlyId !== null && $onlyId < 1) {
            $this->error('Source id must be a positive integer.');

            return self::FAILURE;
        }

        $this->info('Syncing RSS/Atom sources…');
        $n = $ingestion->syncAll($onlyId);
        $this->info("Done. New entries stored this run: {$n}.");

        return self::SUCCESS;
    }
}
