<?php

namespace App\Notifications;

use App\Models\Milestone;
use App\Models\NotificationMessage;
use App\Models\Proposal;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class MilestoneDueDate extends Notification
{
    use Queueable;

    private Milestone $milestone;

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

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

    /**
     * Get the mail representation of the notification.
     *
     * @param mixed $notifiable
     * @return MailMessage
     */
    public function toMail($notifiable)
    {
        $url= $this->milestone->milestonable()->getModel() instanceof Proposal ?
            "/projects/" . $this->milestone->milestonable->project->slug
            : "/services/" . $this->milestone->milestonable->service->slug;
        $notification = NotificationMessage::where('slug','MilestoneEndsSoon')->first();
        return (new MailMessage)
            ->subject($notification->subject)
            ->greeting($notification->greeting .' ' . $notifiable->name . "!")
            ->line($notification->message)
            ->action($notification->action_button_text, url($url))
            ->line($notification->end_text);
    }

    /**
     * Get the array representation of the notification.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        $notification = NotificationMessage::where('slug','MilestoneEndsSoon')->first();
        $url= $this->milestone->milestonable()->getModel() instanceof Proposal ?
            "/projects/" . $this->milestone->milestonable->project->slug
            : "/services/" . $this->milestone->milestonable->service->slug;
        return [
            'title' => $notification->subject,
            'description' => $notification->message,'title_ar'=> $notification->subject_ar,
            'description_ar'=>$notification->message_ar,
            'milestone_id' => $this->milestone->milestonable->id,
            'relative_url' => $url,
        ];
    }
}
