<?php

namespace App\Notifications;

use App\Models\Leave;
use Illuminate\Support\Str;

class LeaveReviewedNotification extends HrmsNotification
{
    public function __construct(
        public Leave $leave,
        public string $outcome
    ) {}

    public function notificationCategory(): string
    {
        return 'leave';
    }

    public function notificationSubject(): string
    {
        return 'Your leave request was ' . $this->outcome;
    }

    public function notificationLines(): array
    {
        $lines = [
            'Your leave request (' . ($this->leave->leaveType?->name ?? 'Leave') . ') was ' . $this->outcome . '.',
            'Dates: ' . $this->leave->start_date?->format('d M Y') . ' to ' . $this->leave->end_date?->format('d M Y'),
        ];

        if ($this->leave->remark) {
            $lines[] = 'Remark: ' . Str::limit((string) $this->leave->remark, 120);
        }

        return $lines;
    }

    public function actionUrl(): ?string
    {
        return route('leaves.dashboard');
    }

    public function actionLabel(): string
    {
        return 'View leave balances';
    }

    public function toArray(object $notifiable): array
    {
        return [
            'type' => 'leave_reviewed',
            'message' => 'Your leave request for '
                . $this->leave->start_date?->format('d M Y') . ' to ' . $this->leave->end_date?->format('d M Y')
                . ' was ' . $this->outcome . '.',
            'outcome' => $this->outcome,
            'leave_id' => $this->leave->id,
            'remark' => Str::limit((string) ($this->leave->remark ?? ''), 80),
            'url' => route('leaves.dashboard'),
        ];
    }

    protected function fcmType(): string
    {
        return 'leave_reviewed';
    }

    protected function fcmRoute(): string
    {
        return '/leave';
    }

    protected function fcmData(): array
    {
        return [
            'leave_id' => (string) $this->leave->id,
            'outcome' => $this->outcome,
        ];
    }
}
