<?php

namespace Modules\Superadmin\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class NewSubscriptionNotification extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($subscription)
    {
        $this->subscription = $subscription;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param mixed $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        $subscription = $this->subscription;
        $business = optional($subscription)->business;
        $package = optional($subscription)->package;
        $owner = optional($business)->owner;

        $paidVia = !empty($subscription->paid_via) ? $subscription->paid_via : 'Gratuito';
        $transactionId = !empty($subscription->payment_transaction_id) ? $subscription->payment_transaction_id : '-';

        $price = '-';
        if (!empty($package->price)) {
            $price = 'R$ ' . number_format($package->price, 2, ',', '.');
        }

        $status = !empty($subscription->status) ? ucfirst($subscription->status) : 'Registrada';

        $subscriptionDate = !empty($subscription->created_at)
            ? $subscription->created_at->format('d/m/Y H:i')
            : now()->format('d/m/Y H:i');

        return (new MailMessage)
            ->subject('Nova assinatura registrada no ERP Sousait')
            ->replyTo('contatos@sousait.net', 'ERP Sousait')
            ->view('superadmin::emails.new_subscription_notification', [
                'businessName' => !empty($business->name) ? $business->name : 'Empresa não informada',
                'ownerName' => !empty($owner->user_full_name) ? $owner->user_full_name : 'Responsável não informado',
                'ownerEmail' => !empty($owner->email) ? $owner->email : 'contatos@sousait.net',
                'packageName' => !empty($package->name) ? $package->name : 'Pacote não informado',
                'price' => $price,
                'status' => $status,
                'subscriptionDate' => $subscriptionDate,
                'paidVia' => $paidVia,
                'transactionId' => $transactionId,
            ]);
    }

    /**
     * Get the array representation of the notification.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}
