<?php

namespace App\Notifications\Backend\User;

use App\Models\OrderManagement\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class OrderCreatedNotification extends Notification implements ShouldQueue{
    use Queueable;

    public $order;

    /**
     * Create a new notification instance.
     *
     * @param Order $order
     */
    public function __construct(Order $order){
        $this->order = $order;
    }

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

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toDatabase($notifiable): array{
        return $this->data();
    }

    public function toBroadcast($notifiable): BroadcastMessage{
        return new BroadcastMessage([
            'data' => $this->data()
        ]);
    }

    public function toMail($notifiable): MailMessage{
        $order = $this->order;
        return (new MailMessage())
                ->subject(__("New order submited [:id]",['id' => $order->ref_id]))
                ->greeting(__("Dear :name",['name' => $notifiable->full_name]))
                ->line('A New order created with Id ":id"', ['id' => $order->ref_id])
            ;
    }

    protected function data(): array{
        return [
            "customer_id" => $this->order->customer_id,
            'user_name' => $this->order->customer->full_name,
            'order_id' => $this->order->id,
            'amount' => $this->order->total,
            'order_url' => route('admin.orders.show', $this->order->id),
            'created_date' => $this->order->created_at->format(__("d-m-Y")),
        ];
    }
}
