<?php

namespace App\Mail;

use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class RanchCalvingReportMail extends Mailable
{
    use Queueable;
    use SerializesModels;

    /**
     * @param  array<string, mixed>  $report
     */
    public function __construct(
        public readonly array $report,
    ) {}

    public function build(): self
    {
        $count = (int) ($this->report['totals']['calvings_count'] ?? 0);
        $empresa = config('app.name', 'Control Ganadero');
        $from = $this->report['from'] ?? null;
        $to = $this->report['to'] ?? null;
        $period = $from && $to
            ? Carbon::parse($from)->format('d/m/Y').' – '.Carbon::parse($to)->format('d/m/Y')
            : 'periodo completo';

        return $this->subject("{$empresa}: resumen de partos ({$count}) — {$period}")
            ->view('emails.ranch.calving_report', [
                'report' => $this->report,
            ]);
    }
}
