<?php

namespace App\Notifications;

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

class OffsetEarning extends Notification
{
    use Queueable;

    private string $message;

    /**
     * Create a new notification instance.
     */
    public function __construct( private EmployeeOffsetEarning $offsetEarning)
    {
        $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.offset_earning', [
            'headerImageUrl' => asset('images/AUTOHUB25_BANNER.png'), // URL of the image
            'msg' => $this->message,
            'notifiable' => $notifiable,
        ])
        ->subject('Offset Earning Notification');
    }

    /**
     * Get the array representation of the notification.
     *
     * @return array<string, mixed>
     */
    public function toArray(object $notifiable): array
    {
        return [
            'offset_availment_id' => $this->offsetEarning->id,
            'status' => $this->offsetEarning->status,
            'approved_at' => $this->offsetEarning->approved_at,
            'message' => $this->message,
            'title' => ($this->offsetEarning->status == 2) ? 
            "Your offset earning application has been approved" : 
            (($this->offsetEarning->status == 99) ? 
                "Your offset earning application has been denied" :
                $this->offsetEarning->employee->firstname . ' ' . $this->offsetEarning->employee->lastname." Submitted Offset Earning Application"
            ),
            'url' => 'attendance/offset-approval',
            'created_by' => $this->offsetEarning->employee_id
        ];
    }

    private function generateMessage(): string
    {
        $fullname = $this->offsetEarning->employee->firstname.' '.$this->offsetEarning->employee->lastname;
        $date = $this->offsetEarning->date;
        $hours = $this->offsetEarning->hours;

        switch ($this->offsetEarning->status) {
            case 1:
            case 0:
                return $fullname.' filed offset earning application dated "'.$date. '" for "'.$hours.'". hours';
            case 2:
                return 'Your offset earning dated "'.$date. '" for "'.$hours.'" hours has been approved';
            case 99:
                return 'Your offset earning application dated "'.$date. '" for "'.$hours.'" hours has been deniedd';
            default:
                return 'Unknown status.';
        }
    }
}
