<?php

namespace App\Notifications;

use App\Models\Interview;

class InterviewAssignedNotification extends HrmsNotification
{
    public function __construct(public Interview $interview) {}

    public function notificationCategory(): string
    {
        return 'recruitment';
    }

    public function notificationSubject(): string
    {
        $name = $this->interview->applicant?->name ?? 'a candidate';

        return match ($this->interview->status) {
            'cancelled' => 'Interview cancelled — '.$name,
            'completed' => 'Interview completed — '.$name,
            default => 'Interview assigned — '.$name,
        };
    }

    public function notificationLines(): array
    {
        $when = $this->interview->scheduled_at?->format('d M Y, h:i A') ?? '—';
        $role = $this->interview->applicant?->jobPosting?->title ?? '—';

        $lines = [
            'You have been assigned an interview.',
            'Candidate: '.($this->interview->applicant?->name ?? '—'),
            'Role: '.$role,
            'Type: '.ucfirst((string) $this->interview->interview_type),
            'When: '.$when,
            'Status: '.ucfirst((string) $this->interview->status),
        ];

        if ($this->interview->meeting_link) {
            $lines[] = 'Meeting: '.$this->interview->meeting_link;
        }

        return $lines;
    }

    public function actionUrl(): ?string
    {
        return route('recruitment.my-interviews');
    }

    public function actionLabel(): string
    {
        return 'View interview schedule';
    }

    public function toArray(object $notifiable): array
    {
        $name = $this->interview->applicant?->name ?? 'a candidate';
        $when = $this->interview->scheduled_at?->format('d M Y, h:i A') ?? '—';

        return [
            'type' => 'interview_assigned',
            'message' => 'Interview with '.$name.' on '.$when,
            'interview_id' => $this->interview->id,
            'applicant_id' => $this->interview->applicant_id,
            'status' => $this->interview->status,
            'meeting_link' => $this->interview->meeting_link,
            'url' => $this->actionUrl(),
        ];
    }

    protected function fcmType(): string
    {
        return 'interview_assigned';
    }

    protected function fcmRoute(): string
    {
        return '/interviews';
    }

    protected function fcmData(): array
    {
        return [
            'interview_id' => (string) $this->interview->id,
            'status' => (string) $this->interview->status,
            'meeting_link' => (string) ($this->interview->meeting_link ?? ''),
        ];
    }
}
