<?php

namespace App\Console\Commands;

use App\Models\Theme;
use App\Services\NewsCrawlerService;
use Illuminate\Console\Command;

class CrawlNewsCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:crawl-news {keyword?} {--limit=10} {--all-themes}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Crawl news from Google News based on keywords or themes';

    /**
     * Execute the console command.
     */
    public function handle(NewsCrawlerService $service)
    {
        $limit = (int) $this->option('limit');
        $allThemes = $this->option('all-themes');
        $keyword = $this->argument('keyword');

        if ($allThemes) {
            $themes = Theme::active()->get();
            if ($themes->isEmpty()) {
                $this->error('No active themes found.');

                return;
            }

            foreach ($themes as $theme) {
                $this->info("Crawling for theme: {$theme->name}");
                $count = $service->crawlFromGoogleNews($theme->name, $limit, [$theme->id]);
                $this->info("Saved {$count} news items for theme: {$theme->name}");
            }

            return;
        }

        if (! $keyword) {
            $this->error('Please provide a keyword or use --all-themes');

            return;
        }

        $this->info("Crawling for keyword: {$keyword}");
        $count = $service->crawlFromGoogleNews($keyword, $limit);
        $this->info("Saved {$count} news items for keyword: {$keyword}");
    }
}
