<?php

namespace App\Console\Commands;

use App\Models\Client;
use App\Services\Reports\ReportGenerator;
use Carbon\Carbon;
use Illuminate\Console\Command;

class GenerateReportCommand extends Command
{
    protected $signature = 'dw:generate-report {client_id} {--type=monthly} {--from=} {--to=} {--channels=*}';

    protected $description = 'Müşteri için manuel DW Ads Reporter raporu üretir.';

    public function handle(ReportGenerator $generator): int
    {
        $client = Client::findOrFail((int) $this->argument('client_id'));
        $type = (string) $this->option('type');
        $from = $this->option('from') ? Carbon::parse((string) $this->option('from')) : now()->startOfMonth();
        $to = $this->option('to') ? Carbon::parse((string) $this->option('to')) : now()->endOfDay();
        $channels = $this->option('channels') ?: ['google_ads', 'meta_ads', 'search_console', 'ga4'];

        $report = $generator->createManualReport($client, $type, $from, $to, $channels);

        $this->info('Rapor oluşturuldu: #' . $report->id . ' / ' . $report->status);

        if ($report->error_message) {
            $this->error($report->error_message);
        }

        return $report->status === 'completed' ? self::SUCCESS : self::FAILURE;
    }
}
