<?php

namespace App\Console\Commands;

use App\Services\Forecasting\LlmOverlayService;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;

#[Signature('forecast:llm-overlay {--event-driven : Honor the 4h cooldown (default: false; daily 07:00 cron always runs)}')]
#[Description('Run the daily Anthropic web-search overlay on the current weekly forecast.')]
class RunLlmOverlay extends Command
{
    public function handle(LlmOverlayService $service): int
    {
        $row = $service->run(eventDriven: (bool) $this->option('event-driven'));

        if ($row === null) {
            $this->warn('LLM overlay skipped (no API key, on cooldown, or rejected for empty citations).');

            return self::SUCCESS;
        }

        $this->info(sprintf(
            'Stored llm_overlays #%d — direction=%s confidence=%d major_impact=%s.',
            $row->id,
            $row->direction,
            $row->confidence,
            $row->major_impact_event ? 'YES' : 'no',
        ));

        return self::SUCCESS;
    }
}
