<?php

declare(strict_types=1);

namespace App\Modules\Operation;

/**
 * AttendanceService — Manages business rules validations and status text formats
 * for guest check-ins and attendance statuses.
 *
 * No SQL database queries, no HTML generation.
 *
 * @package Inveetaire
 * @ref     PILOT-007
 */
class AttendanceService
{
    /**
     * Get a human-readable display status for guest attendance.
     *
     * @param string $checkinStatus 'not_arrived' | 'checked_in' | 'no_show'
     * @param string|null $checkedInAt Timestamp string.
     * @return string
     */
    public function getAttendanceLabel(string $checkinStatus, ?string $checkedInAt): string
    {
        if ($checkinStatus === 'checked_in') {
            $timeLabel = '';
            if ($checkedInAt !== null) {
                try {
                    $dt = new \DateTime($checkedInAt);
                    $timeLabel = ' at ' . $dt->format('H:i') . ' UTC';
                } catch (\Throwable $e) {
                    // Ignore format errors
                }
            }
            return "Checked In" . $timeLabel;
        }

        if ($checkinStatus === 'no_show') {
            return "Absent / No Show";
        }

        return "Not Arrived Yet";
    }

    /**
     * Verify if a guest is eligible for wedding day check-in.
     * (e.g. they should have RSVP'd yes or not declined).
     *
     * @param array $guestData Complete guest details row.
     * @return array Status indicating eligibility and optional warning messages.
     */
    public function verifyCheckinEligibility(array $guestData): array
    {
        $rsvp = $guestData['rsvp_status'] ?? 'pending';

        if ($rsvp === 'declined') {
            return [
                'eligible' => true,
                'warning'  => 'Warning: This guest responded "Declined / Not Attending" on their RSVP.'
            ];
        }

        if ($rsvp === 'pending') {
            return [
                'eligible' => true,
                'warning'  => 'Note: This guest has not submitted their RSVP response yet.'
            ];
        }

        return [
            'eligible' => true,
            'warning'  => null
        ];
    }
}
