<?php

namespace App\Notifications;

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

class ChangeSchedule extends Notification
{
    use Queueable;

    private string $message;

    /**
     * Create a new notification instance.
     */
    public function __construct( private EmployeeChangeSchedule $change_schedule)
    {
        $this->message = $this->generateMessage();
    }

    /**
     * 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
    {
        return (new MailMessage)
        ->view('emails.change_schedule', [
            'headerImageUrl' => asset('images/AUTOHUB25_BANNER.png'), // URL of the image
            'msg' => $this->message,
            'notifiable' => $notifiable,
        ])
        ->subject('Change Schedule Notification');
    }

    /**
     * Get the array representation of the notification.
     *
     * @return array<string, mixed>
     */
    public function toArray(object $notifiable): array
    {
        $title = null;
        $isApprover = false;

        if($this->change_schedule->validated == 1){
            $title = "Your change schedule application has been received and validated by HR";
        }else if($this->change_schedule->validated == 99){
            $title = "Your change schedule application has been denied by HR";
        }else if($this->change_schedule->status == 2){
           $title =  "Your change schedule application has been approved";
        }else if($this->change_schedule->status == 99){
            $title = "Your change schedule application has been denied ";
        }else{
            $title = $this->change_schedule->employee->firstname . ' ' . $this->change_schedule->employee->lastname." Submitted Leave Application";
            $isApprover = true;
        }

        return [
            'change_schedule_id' => $this->change_schedule->id,
            'status' => $this->change_schedule->status,
            'approved_at' => $this->change_schedule->approved_at,
            'message' => $this->message,
            'title' => $title,
            'url' => $isApprover ? 'attendance/change-schedule-approval' : 'attendance/change-schedule',
            'created_by' => $this->change_schedule->employee_id
        ];
    }

    private function generateMessage(): string
    {
        $fullname = $this->change_schedule->employee->firstname.' '.$this->change_schedule->employee->lastname;
        $date = $this->change_schedule->date;
        $from_time = $this->change_schedule->from_time;
        $to_time = $this->change_schedule->to_time;
        $created_at = Carbon::parse($this->change_schedule->created_at)->setTimezone('Asia/Shanghai');

        switch ($this->change_schedule->status) {
            case 1:
            case 0:
                return $fullname.' filed change schedule application dated "'.$date. '" from "'.$from_time.'" to "'.$to_time.'" for your validation.';
            case 2:
                if($this->change_schedule->validated == 1){
                    return 'Your change schedule application dated "'.$date. '" from "'.$from_time.'" to "'.$to_time.'" has been received and validated by HR.';
                }else if($this->change_schedule->validated == 99){
                    return 'Your change schedule application dated "'.$date. '" from "'.$from_time.'" to "'.$to_time.'" has been denied by HR.';
                }else{
                    return 'Your change schedule application dated "'.$date. '" from "'.$from_time.'" to "'.$to_time.'" has been approved.';
                }
               
            case 99:
                return 'Your change schedule application dated "'.$date. '" from "'.$from_time.'" to "'.$to_time.'" has been denied.';
            default:
                return 'Unknown status.';
        }
    }
}
