<?php

namespace App\Notifications\Packages;

use App\Models\TenantPackage;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class TenantPackageExpiringNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct(
        private readonly TenantPackage $tenantPackage,
        private readonly int $daysRemaining,
    ) {}

    public function via(object $notifiable): array
    {
        return ['mail'];
    }

    public function toMail(object $notifiable): MailMessage
    {
        $tenant = $this->tenantPackage->tenant;
        $package = $this->tenantPackage->package;

        $url = url('/tenant/billing');
        $subject = trans_choice('emails.package_limits.package_expiring.subject', $this->daysRemaining, [
            'package' => $package?->getNameForLocale() ?? $package?->name ?? __('emails.package_limits.package_fallback'),
            'days' => $this->daysRemaining,
        ]);
        $greeting = __('emails.package_limits.package_expiring.greeting', [
            'name' => $tenant?->name ?? __('emails.package_limits.team_fallback'),
        ]);

        return (new MailMessage)
            ->subject($subject)
            ->view('emails.notifications.basic', [
                'title' => $subject,
                'preheader' => $greeting,
                'heroTitle' => $greeting,
                'heroSubtitle' => $subject,
                'lines' => [
                    trans_choice('emails.package_limits.package_expiring.body', $this->daysRemaining, [
                        'package' => $package?->getNameForLocale() ?? $package?->name ?? __('emails.package_limits.package_fallback'),
                        'days' => $this->daysRemaining,
                        'date' => optional($this->tenantPackage->expires_at)->toFormattedDateString(),
                    ]),
                ],
                'cta' => [
                    [
                        'label' => __('emails.package_limits.package_expiring.action'),
                        'url' => $url,
                    ],
                ],
                'footer' => __('emails.package_limits.footer'),
            ]);
    }
}
