<?php

namespace App\Notifications;

use App\Models\Leave;

class LeaveCancelledNotification extends HrmsNotification
{
    public function __construct(
        public Leave $leave,
        public bool $wasApproved = false
    ) {}

    public function notificationCategory(): string
    {
        return 'leave';
    }

    public function notificationSubject(): string
    {
        $name = $this->leave->user?->name ?? 'An employee';

        return $this->wasApproved
            ? 'Leave withdrawal accepted — '.$name
            : 'Leave request cancelled — '.$name;
    }

    public function notificationLines(): array
    {
        $user = $this->leave->user;
        $lines = [
            $this->wasApproved
                ? (('Leave withdrawal for '.($user?->name ?? 'an employee').' was accepted.'))
                : (($user?->name ?? 'An employee').' cancelled a pending leave request.'),
            'Type: '.($this->leave->leaveType?->name ?? 'Leave'),
            'Dates: '.$this->leave->start_date?->format('d M Y').' to '.$this->leave->end_date?->format('d M Y'),
        ];

        $withdrawalReason = trim((string) ($this->leave->withdrawal_reason ?? ''));
        if ($withdrawalReason !== '') {
            $lines[] = 'Reason: '.$withdrawalReason;
        }

        return $lines;
    }

    public function actionUrl(): ?string
    {
        return route('leaves.index', ['status' => $this->wasApproved ? 'cancelled' : 'pending']);
    }

    public function actionLabel(): string
    {
        return 'View leave requests';
    }

    public function toArray(object $notifiable): array
    {
        $user = $this->leave->user;
        $withdrawalReason = trim((string) ($this->leave->withdrawal_reason ?? ''));

        return [
            'type' => 'leave_cancelled',
            'message' => $this->wasApproved
                ? ('Leave withdrawal for '.($user?->name ?? 'an employee').' was accepted for '
                    .$this->leave->start_date?->format('d M Y').' to '.$this->leave->end_date?->format('d M Y').'.')
                : (($user?->name ?? 'An employee').' cancelled a leave request for '
                    .$this->leave->start_date?->format('d M Y').' to '.$this->leave->end_date?->format('d M Y').'.'),
            'leave_id' => $this->leave->id,
            'employee_name' => $user?->name,
            'withdrawal_reason' => $withdrawalReason !== '' ? $withdrawalReason : null,
            'url' => route('leaves.index', ['status' => $this->wasApproved ? 'cancelled' : 'pending']),
        ];
    }

    protected function fcmType(): string
    {
        return 'leave_cancelled';
    }

    protected function fcmRoute(): string
    {
        return '/leave';
    }

    protected function fcmData(): array
    {
        return [
            'leave_id' => (string) $this->leave->id,
            'outcome' => $this->wasApproved ? 'withdrawal_accepted' : 'cancelled',
        ];
    }
}
