<?php

namespace App\Console\Commands;

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

class RanchNotifyWeightAlertsCommand extends Command
{
    protected $signature = 'ranch:notify-weight-alerts
                            {--force : Enviar aunque no haya alertas}
                            {--min-gdp= : Umbral GDP kg/día}
                            {--days= : Días sin pesaje para alerta}';

    protected $description = 'Envía correo si hay alertas de peso (GDP bajo o sin pesaje reciente)';

    public function handle(RanchWeightAlertNotificationService $service): int
    {
        $minGdp = $this->option('min-gdp') !== null ? (float) $this->option('min-gdp') : null;
        $days = $this->option('days') !== null ? (int) $this->option('days') : null;
        $force = (bool) $this->option('force');

        $result = $service->sendIfNeeded($minGdp, $days, $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;
    }
}
