<?php

namespace App\Console\Commands;

use App\Services\Customer\CustomerQualityService;
use Illuminate\Console\Command;

class CustomerQualityRefreshMetricsCommand extends Command
{
    protected $signature = 'customer-quality:refresh-metrics';

    protected $description = 'Refresh cached customer quality metrics, including guest orders grouped by customer phone.';

    public function __construct(
        private readonly CustomerQualityService $customerQualityService
    ) {
        parent::__construct();
    }

    public function handle(): int
    {
        try {
            $result = $this->customerQualityService->refreshMetrics();
            $this->info(sprintf(
                'Customer quality metrics refreshed. Metrics: %d, High quality: %d, Risk: %d, Checksum: %s.',
                $result['metric_count'],
                $result['high_quality_count'],
                $result['risk_count'],
                $result['checksum']
            ));

            return self::SUCCESS;
        } catch (\Throwable $throwable) {
            report($throwable);
            $this->error('Customer quality metric refresh failed: ' . $throwable->getMessage());

            return self::FAILURE;
        }
    }
}
