<?php

namespace App\Notifications;

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

class RetirementReminderNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public $employee;
    public $daysLeft;
    public $type;

    public function __construct(Employee $employee, int $daysLeft, string $type = 'retirement')
    {
        $this->employee = $employee;
        $this->daysLeft = $daysLeft;
        $this->type = $type;
    }

    public function via($notifiable): array
    {
        return ['mail', 'database'];
    }

    public function toMail($notifiable): MailMessage
    {
        $subject = $this->getSubject();
        $urgency = $this->getUrgencyText();

        $mail = (new MailMessage)
            ->subject($subject)
            ->greeting('Halo ' . $notifiable->name . ',')
            ->line($urgency)
            ->line('')
            ->line('**Nama:** ' . $this->employee->name)
            ->line('**NIP:** ' . $this->employee->nip)
            ->line('**Departemen:** ' . $this->employee->department->name)
            ->line('**Jabatan:** ' . $this->employee->position)
            ->line('**Tanggal Pensiun:** ' . $this->employee->retirement_date->format('d M Y'));

        if ($this->daysLeft <= 7) {
            $mail->line('')
                 ->line('⚠️ **PERHATIAN:** Segera proses pencabutan semua akses!')
                 ->line('Pastikan tidak ada akses yang tertinggal.');
        }

        $mail->action('Lihat Detail Pegawai', url('/employees/' . $this->employee->id))
             ->line('Terima kasih.');

        return $mail;
    }

    public function toDatabase($notifiable): array
    {
        return [
            'type' => $this->type . '_reminder',
            'employee_id' => $this->employee->id,
            'days_left' => $this->daysLeft,
            'message' => $this->employee->name . ' pensiun dalam ' . $this->daysLeft . ' hari',
        ];
    }

    protected function getSubject(): string
    {
        if ($this->daysLeft <= 0) {
            return '[URGENT] ' . $this->employee->name . ' HARI INI PENSIUN!';
        } elseif ($this->daysLeft == 1) {
            return '[URGENT] ' . $this->employee->name . ' BESOK PENSIUN!';
        } elseif ($this->daysLeft <= 7) {
            return '[REMINDER] ' . $this->employee->name . ' pensiun dalam ' . $this->daysLeft . ' hari';
        } else {
            return '[INFO] ' . $this->employee->name . ' akan pensiun';
        }
    }

    protected function getUrgencyText(): string
    {
        if ($this->daysLeft <= 0) {
            return '🔴 **' . $this->employee->name . ' HARI INI adalah hari pensiun!**';
        } elseif ($this->daysLeft == 1) {
            return '🟡 **' . $this->employee->name . ' BESOK akan pensiun!**';
        } elseif ($this->daysLeft <= 7) {
            return '🟡 **' . $this->employee->name . ' akan pensiun dalam ' . $this->daysLeft . ' hari.**';
        } else {
            return '🔵 ' . $this->employee->name . ' akan pensiun dalam ' . $this->daysLeft . ' hari.';
        }
    }
}