<?php

namespace App\Console\Commands;

use App\Support\InvestmentLedger;
use Carbon\Carbon;
use Illuminate\Console\Command;

class AccrueInvestmentsDaily extends Command
{
    protected $signature = 'investments:accrue-daily {--date= : Fecha Y-m-d (por defecto hoy)}';

    protected $description = 'Acredita intereses según parámetros (diario cada día | mensual en el día pactado).';

    public function handle(): int
    {
        $dateOpt = $this->option('date');
        $on = $dateOpt !== null && $dateOpt !== ''
            ? Carbon::parse((string) $dateOpt)->startOfDay()
            : Carbon::today();

        $count = InvestmentLedger::runAccrualForDate($on);
        $finalized = InvestmentLedger::finalizeEndedActiveInvestments();

        $this->info("Acreditaciones aplicadas: {$count} (fecha {$on->toDateString()}).");
        if ($finalized > 0) {
            $this->info("Inversiones marcadas como finalizadas: {$finalized}.");
        }

        return self::SUCCESS;
    }
}
