<?php

namespace App\Notifications;

use App\Models\Subscription;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class SubscriptionExpiringNotification extends Notification
{
    use Queueable;

    public function __construct(public Subscription $subscription, public int $daysLeft)
    {
    }

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

    public function toMail(object $notifiable): MailMessage
    {
        $tenant = $this->subscription->tenant;
        $isTrial = $this->subscription->status === 'trialing';

        return (new MailMessage())
            ->subject(($isTrial ? __('Your trial ends soon') : __('Your subscription renews soon'))
                .' — '.$tenant->name)
            ->greeting(__('Hello :name', ['name' => $notifiable->name]))
            ->line($isTrial
                ? __('Your :days-day free trial for :clinic ends in :left day(s).', [
                    'days' => $this->subscription->plan->trial_days,
                    'clinic' => $tenant->name, 'left' => $this->daysLeft,
                ])
                : __('Your :clinic subscription renews in :left day(s).', [
                    'clinic' => $tenant->name, 'left' => $this->daysLeft,
                ]))
            ->action(__('Manage billing'), $tenant->url('/billing'))
            ->line(__('After expiry there is a :grace-day grace period before access is suspended.', [
                'grace' => config('tenancy.grace_days'),
            ]));
    }

    public function toArray(object $notifiable): array
    {
        return [
            'type' => 'subscription_expiring',
            'days_left' => $this->daysLeft,
            'status' => $this->subscription->status,
            'tenant' => $this->subscription->tenant->name,
            'billing_url' => $this->subscription->tenant->url('/billing'),
        ];
    }
}
