<?php

namespace App\Notifications\Subscription;

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

class SubscriptionTrialStatusNotification extends Notification
{
    use Queueable;

    public Subscription $subscription;
    protected $status;
    public $type;
    protected array $templateMap = [];

    /**
     * Create a new notification instance.
     */
    public function __construct($subscription, $type)
    {
        $this->subscription = $subscription;
        $this->type = $type;

        if ($this->subscription->status === getConstant('SUBSCRIPTION_STATUS_TRIAL')) {
            $this->status = 'Activated';
        } else {
            $this->status = getConstantLabel('subscription_status', $this->subscription->status);
        }
    }

    /**
     * Get the notification's delivery channels.
     *
     * @return array<int, string>
     */
    public function via(object $notifiable): array
    {
        return ['mail', 'database'];
    }

    /**
     * Get the mail representation of the notification.
     */
    public function toMail(object $notifiable): MailMessage
    {
        $this->templateMap = [
            getConstant('SUBSCRIPTION_STATUS_TRIAL') => 'activated',
            getConstant('SUBSCRIPTION_STATUS_INACTIVE') => 'inactive',
            getConstant('SUBSCRIPTION_STATUS_CANCELLED') => 'cancelled',
            getConstant('SUBSCRIPTION_STATUS_EXPIRED') => 'expired',
            getConstant('SUBSCRIPTION_STATUS_SUSPENDED') => 'suspended',
        ];

        if (isset($this->templateMap[$this->type])) {
            return (new MailMessage)
                ->subject(trans('email.subscription_trial_'.$this->templateMap[$this->type].'.subject', [
                    'reference_id' => $this->subscription->reference_id,
                ]))
                ->markdown('emails.subscription.trial.'.$this->templateMap[$this->type], ['subscription' => $this->subscription]);
        }

        return new MailMessage();
    }

    /**
     * Get the array representation of the notification.
     *
     * @return array<string, mixed>
     */
    public function toArray(object $notifiable): array
    {
        return [
            'title' => 'Trial Subscription #' . $this->subscription->reference_id . ' is ' . $this->status,
            'reference_id' => $this->subscription->reference_id,
        ];
    }
}
