<?php

namespace App\Services\Care\Infusion;

/**
 * Map infusion centre clinical progress onto visit stages.
 */
class InfusionWorkflowService
{
    public const STAGE_CHECK_IN = 'check_in';

    public const STAGE_ASSESSMENT = 'assessment';

    public const STAGE_PROTOCOL = 'protocol';

    public const STAGE_SESSION = 'session';

    public const STAGE_MONITORING = 'monitoring';

    public const STAGE_COMPLETED = 'completed';

    /**
     * @param  array<string, mixed>  $payload
     */
    public function stageFromAssessment(array $payload): string
    {
        if (! empty($payload['indication']) || ! empty($payload['fit_for_infusion'])) {
            return self::STAGE_ASSESSMENT;
        }

        return self::STAGE_CHECK_IN;
    }

    /**
     * @param  array<string, mixed>  $payload
     */
    public function stageFromProtocol(array $payload): string
    {
        if (! empty($payload['medication']) && ! empty($payload['dose'])) {
            return self::STAGE_PROTOCOL;
        }

        return self::STAGE_ASSESSMENT;
    }

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

        if (! empty($payload['notes']) || ! empty($payload['start_time'])) {
            return self::STAGE_SESSION;
        }

        return self::STAGE_PROTOCOL;
    }

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

        if (! empty($payload['outcome']) || ! empty($payload['post_vitals'])) {
            return self::STAGE_MONITORING;
        }

        return self::STAGE_SESSION;
    }

    /**
     * @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_ASSESSMENT, 'label' => 'Start assessment'],
            self::STAGE_ASSESSMENT => ['next' => self::STAGE_PROTOCOL, 'label' => 'Move to protocol'],
            self::STAGE_PROTOCOL => ['next' => self::STAGE_SESSION, 'label' => 'Move to infusion session'],
            self::STAGE_SESSION => ['next' => self::STAGE_MONITORING, 'label' => 'Move to monitoring'],
            self::STAGE_MONITORING => ['next' => self::STAGE_COMPLETED, 'label' => 'Complete visit'],
            self::STAGE_COMPLETED => ['next' => self::STAGE_COMPLETED, 'label' => 'Completed'],
        ];
    }
}
