<?php

namespace App\Services\Care\WomensHealth;

/**
 * Map Women's Health / ANC clinical progress onto visit stages.
 */
class WomensHealthWorkflowService
{
    public const STAGE_CHECK_IN = 'check_in';

    public const STAGE_HISTORY = 'history';

    public const STAGE_EXAM = 'exam';

    public const STAGE_INVESTIGATION = 'investigation';

    public const STAGE_PLAN = 'plan';

    public const STAGE_POSTNATAL = 'postnatal';

    public const STAGE_COMPLETED = 'completed';

    /**
     * @param  array<string, mixed>  $payload
     */
    public function stageFromHistory(array $payload): string
    {
        if (isset($payload['gravida']) || isset($payload['gestational_age_weeks'])) {
            return self::STAGE_HISTORY;
        }

        return self::STAGE_CHECK_IN;
    }

    /**
     * @param  array<string, mixed>  $payload
     */
    public function stageFromExam(array $payload): string
    {
        $findings = trim((string) ($payload['findings'] ?? ''));
        $danger = strtolower((string) ($payload['danger_signs'] ?? ''));

        if ($findings !== '' || $danger !== '') {
            return self::STAGE_EXAM;
        }

        return self::STAGE_HISTORY;
    }

    /**
     * @param  array<string, mixed>  $payload
     */
    public function stageFromFetal(array $payload): string
    {
        if (isset($payload['fetal_heart_rate']) && $payload['fetal_heart_rate'] !== '') {
            return self::STAGE_EXAM;
        }

        return self::STAGE_EXAM;
    }

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

        $plan = trim((string) ($payload['plan'] ?? ''));
        if ($plan !== '') {
            return self::STAGE_PLAN;
        }

        return self::STAGE_INVESTIGATION;
    }

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

        return str_contains($outcome, 'discharged')
            || str_contains($outcome, 'continue anc');
    }

    /**
     * @return array<string, array{next: string, label: string}>
     */
    public function stageFlow(): array
    {
        return [
            self::STAGE_CHECK_IN => ['next' => self::STAGE_HISTORY, 'label' => 'Start ANC history'],
            self::STAGE_HISTORY => ['next' => self::STAGE_EXAM, 'label' => 'Move to exam / vitals'],
            self::STAGE_EXAM => ['next' => self::STAGE_INVESTIGATION, 'label' => 'Move to investigations'],
            self::STAGE_INVESTIGATION => ['next' => self::STAGE_PLAN, 'label' => 'Move to care plan'],
            self::STAGE_PLAN => ['next' => self::STAGE_POSTNATAL, 'label' => 'Move to delivery / postnatal'],
            self::STAGE_POSTNATAL => ['next' => self::STAGE_COMPLETED, 'label' => 'Complete visit'],
            self::STAGE_COMPLETED => ['next' => self::STAGE_COMPLETED, 'label' => 'Completed'],
        ];
    }
}
