<?php

namespace App\Console\Commands;

use App\Services\RanchCostAlertNotificationService;
use Illuminate\Console\Command;

class RanchNotifyCostAlertsCommand extends Command
{
    protected $signature = 'ranch:notify-cost-alerts
                            {--force : Enviar aunque no haya alertas}
                            {--max-cost= : Umbral máximo $/kg ganado}
                            {--min-gain= : Kg mínimos ganados para evaluar}';

    protected $description = 'Envía correo si hay animales activos con costo/kg por encima del umbral';

    public function handle(RanchCostAlertNotificationService $service): int
    {
        $maxCost = $this->option('max-cost') !== null ? (float) $this->option('max-cost') : null;
        $minGain = $this->option('min-gain') !== null ? (float) $this->option('min-gain') : null;
        $force = (bool) $this->option('force');

        $result = $service->sendIfNeeded($maxCost, $minGain, $force);

        $this->info($result['message']);
        if ($result['recipients'] !== []) {
            $this->line('Destinatarios: '.implode(', ', $result['recipients']));
        }
        $this->line('Alertas: '.$result['alert_count']);

        return $result['sent'] || $result['alert_count'] === 0 ? self::SUCCESS : self::FAILURE;
    }
}
