<?php

namespace App\Notifications\Ops;

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

class PurchaseCreated extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct(private readonly PackagePurchase $purchase) {}

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

    public function toMail(object $notifiable): MailMessage
    {
        $tenant = $this->purchase->tenant;
        $package = $this->purchase->package;
        $amount = number_format((float) $this->purchase->price, 2);
        $consents = $this->purchase->metadata['consents'] ?? [];
        $subject = __('emails.ops.purchase.subject', ['package' => $package?->name ?? __('emails.package_limits.package_fallback')]);
        $greeting = __('emails.ops.purchase.greeting');

        return (new MailMessage)
            ->subject($subject)
            ->view('emails.notifications.basic', [
                'title' => $subject,
                'preheader' => $greeting,
                'heroTitle' => $greeting,
                'heroSubtitle' => $subject,
                'lines' => [
                    __('emails.ops.purchase.tenant', ['tenant' => $tenant?->name ?? __('emails.tenant_feedback.unknown_tenant')]),
                    __('emails.ops.purchase.package', ['package' => $package?->name ?? __('emails.package_limits.package_fallback')]),
                    __('emails.ops.purchase.amount', ['amount' => $amount, 'currency' => '€']),
                    __('emails.ops.purchase.provider', ['provider' => $this->purchase->provider, 'id' => $this->purchase->provider_id ?? '—']),
                    __('emails.ops.purchase.consents', [
                        'legal' => $consents['legal_version'] ?? 'n/a',
                        'terms' => $consents['accepted_terms_at'] ?? 'n/a',
                        'waiver' => $consents['digital_content_waiver_at'] ?? 'n/a',
                    ]),
                ],
                'footer' => __('emails.ops.purchase.footer'),
            ]);
    }
}
