<?php

namespace App\Notifications;

use App\Models\Leave;
use Illuminate\Support\Str;

class LeaveSubmittedNotification extends HrmsNotification
{
    public function __construct(
        public Leave $leave
    ) {}

    public function notificationCategory(): string
    {
        return 'leave';
    }

    public function notificationSubject(): string
    {
        $name = $this->leave->user?->name ?? 'An employee';

        return 'Leave request pending approval — ' . $name;
    }

    public function notificationLines(): array
    {
        $user = $this->leave->user;
        $type = $this->leave->leaveType?->name ?? 'Leave';

        return [
            ($user?->name ?? 'An employee') . ' submitted a leave request.',
            'Type: ' . $type,
            'Dates: ' . $this->leave->start_date?->format('d M Y') . ' to ' . $this->leave->end_date?->format('d M Y'),
            'Days: ' . rtrim(rtrim(number_format((float) $this->leave->total_days, 1, '.', ''), '0'), '.'),
            'Reason: ' . Str::limit((string) $this->leave->reason, 120),
        ];
    }

    public function actionUrl(): ?string
    {
        return route('leaves.index', ['status' => 'pending']);
    }

    public function actionLabel(): string
    {
        return 'Review leave requests';
    }

    public function toArray(object $notifiable): array
    {
        $user = $this->leave->user;

        return [
            'type' => 'leave_submitted',
            'message' => ($user?->name ?? 'An employee') . ' submitted 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,
            'leave_type' => $this->leave->leaveType?->name,
            'start_date' => $this->leave->start_date?->format('Y-m-d'),
            'end_date' => $this->leave->end_date?->format('Y-m-d'),
            'total_days' => $this->leave->total_days,
            'url' => route('leaves.index', ['status' => 'pending']),
        ];
    }

    protected function fcmType(): string
    {
        return 'leave_submitted';
    }

    protected function fcmRoute(): string
    {
        return '/leave';
    }

    protected function fcmData(): array
    {
        return [
            'leave_id' => (string) $this->leave->id,
            'outcome' => 'pending',
        ];
    }
}
