<?php

declare(strict_types=1);

namespace App\Mail;

use App\Models\AssetWarranty;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Support\Collection;

/**
 * The daily "these warranties need attention soon" email.
 *
 * Deliberately not `ShouldQueue` — there is no queue in this app (see the
 * architecture's non-goals); `ams:send-warranty-digest` sends synchronously,
 * once a day, which is a fine place for a mail send to block for the second
 * or two it takes.
 */
class WarrantyExpiryDigest extends Mailable
{
    use Queueable;

    /**
     * @param  array<int, Collection<int, AssetWarranty>>  $windows  alert-day window => warranties expiring exactly then
     */
    public function __construct(
        public readonly array $windows,
    ) {}

    public function envelope(): Envelope
    {
        $total = collect($this->windows)->sum(fn (Collection $warranties): int => $warranties->count());

        return new Envelope(
            subject: trans_choice(
                ':count warranty expiring soon — Aumni AMS|:count warranties expiring soon — Aumni AMS',
                $total,
                ['count' => $total],
            ),
        );
    }

    public function content(): Content
    {
        return new Content(
            view: 'emails.warranty-expiry-digest',
            with: ['windows' => $this->windows],
        );
    }
}
