<?php

namespace App\Notifications;

use App\Models\Applicant;
use App\Services\Recruitment\ReferralProgressService;

class ReferralStageChangedNotification extends HrmsNotification
{
    public function __construct(
        public Applicant $applicant,
        public string $fromStage,
        public string $toStage,
        public ?string $referrerNote = null
    ) {}

    public function notificationCategory(): string
    {
        return 'recruitment';
    }

    public function notificationSubject(): string
    {
        return 'Referral update — '.$this->applicant->name;
    }

    public function notificationLines(): array
    {
        $role = $this->applicant->jobPosting?->title ?? 'an open role';
        $label = app(ReferralProgressService::class)->staffLabel($this->toStage);

        $lines = [
            'Your referral '.$this->applicant->name.' for '.$role.' has been updated.',
            'Status: '.$label,
        ];

        if ($this->referrerNote) {
            $lines[] = $this->referrerNote;
        }

        return $lines;
    }

    public function actionUrl(): ?string
    {
        return route('recruitment.my-referrals.show', $this->applicant);
    }

    public function actionLabel(): string
    {
        return 'View referral progress';
    }

    public function toArray(object $notifiable): array
    {
        return [
            'type' => 'referral_stage_changed',
            'message' => 'Referral '.$this->applicant->name.' moved to '.app(ReferralProgressService::class)->staffLabel($this->toStage),
            'applicant_id' => $this->applicant->id,
            'from_stage' => $this->fromStage,
            'to_stage' => $this->toStage,
            'url' => $this->actionUrl(),
        ];
    }
}
