<?php

namespace App\Services\Care\Vaccination;

/**
 * Map vaccination clinical progress onto visit stages.
 */
class VaccinationWorkflowService
{
    public const STAGE_CHECK_IN = 'check_in';

    public const STAGE_ELIGIBILITY = 'eligibility';

    public const STAGE_CONSENT = 'consent';

    public const STAGE_ADMINISTER = 'administer';

    public const STAGE_OBSERVATION = 'observation';

    public const STAGE_COMPLETED = 'completed';

    /**
     * @param  array<string, mixed>  $payload
     */
    public function stageFromEligibility(array $payload): string
    {
        if (! empty($payload['cleared']) || ! empty($payload['vaccine'])) {
            return self::STAGE_ELIGIBILITY;
        }

        return self::STAGE_CHECK_IN;
    }

    /**
     * @param  array<string, mixed>  $payload
     */
    public function stageFromConsent(array $payload): string
    {
        if (! empty($payload['consent_obtained'])) {
            return self::STAGE_ADMINISTER;
        }

        if (! empty($payload['information_given'])) {
            return self::STAGE_CONSENT;
        }

        return self::STAGE_ELIGIBILITY;
    }

    /**
     * @param  array<string, mixed>  $payload
     */
    public function stageFromAdmin(array $payload): string
    {
        $outcome = strtolower((string) ($payload['outcome'] ?? ''));
        if (str_contains($outcome, 'completed') || ! empty($payload['batch'])) {
            return self::STAGE_OBSERVATION;
        }

        return self::STAGE_ADMINISTER;
    }

    /**
     * @param  array<string, mixed>  $payload
     */
    public function stageFromObservation(array $payload): string
    {
        if ($this->shouldCompleteVisit($payload)) {
            return self::STAGE_COMPLETED;
        }

        $aefi = strtolower((string) ($payload['aefi'] ?? ''));
        if ($aefi !== '' || ! empty($payload['outcome'])) {
            return self::STAGE_OBSERVATION;
        }

        return self::STAGE_ADMINISTER;
    }

    /**
     * @param  array<string, mixed>  $payload
     */
    public function shouldCompleteVisit(array $payload): bool
    {
        $outcome = strtolower((string) ($payload['outcome'] ?? ''));

        return str_contains($outcome, 'completed');
    }

    /**
     * @return array<string, array{next: string, label: string}>
     */
    public function stageFlow(): array
    {
        return [
            self::STAGE_CHECK_IN => ['next' => self::STAGE_ELIGIBILITY, 'label' => 'Start eligibility'],
            self::STAGE_ELIGIBILITY => ['next' => self::STAGE_CONSENT, 'label' => 'Move to consent'],
            self::STAGE_CONSENT => ['next' => self::STAGE_ADMINISTER, 'label' => 'Move to administer'],
            self::STAGE_ADMINISTER => ['next' => self::STAGE_OBSERVATION, 'label' => 'Move to observation'],
            self::STAGE_OBSERVATION => ['next' => self::STAGE_COMPLETED, 'label' => 'Complete visit'],
            self::STAGE_COMPLETED => ['next' => self::STAGE_COMPLETED, 'label' => 'Completed'],
        ];
    }
}
