<?php

namespace App\Jobs;

use App\Models\GeneratedReport;
use App\Services\Ai\ReportAiInsightGenerator;
use App\Services\Reports\ReportGenerator;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class GenerateReportAiInsightsJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function __construct(
        public int $reportId,
        public bool $forceLocal = false,
        public bool $regenerateFiles = false,
    ) {
    }

    public function handle(ReportAiInsightGenerator $insightGenerator, ReportGenerator $reportGenerator): void
    {
        $report = GeneratedReport::findOrFail($this->reportId);
        $report = $insightGenerator->generate($report, $this->forceLocal);

        if ($report->ai_status === 'completed' && $this->regenerateFiles) {
            $reportGenerator->generate($report);
        }
    }
}
