<?php

namespace App\Services\Care\Physiotherapy;

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

    public const STAGE_ASSESSMENT = 'assessment';

    public const STAGE_TREATMENT_PLAN = 'treatment_plan';

    public const STAGE_SESSION = 'session';

    public const STAGE_PROGRESS = 'progress';

    public const STAGE_COMPLETED = 'completed';

    /**
     * @param  array<string, mixed>  $payload
     */
    public function stageFromAssessment(array $payload): string
    {
        $complaint = trim((string) ($payload['chief_complaint'] ?? ''));
        $goals = trim((string) ($payload['goals'] ?? ''));

        if ($complaint !== '' || $goals !== '') {
            return self::STAGE_ASSESSMENT;
        }

        return self::STAGE_CHECK_IN;
    }

    /**
     * @param  array<string, mixed>  $payload
     */
    public function stageFromPlan(array $payload): string
    {
        $diagnosis = trim((string) ($payload['diagnosis'] ?? ''));
        $interventions = trim((string) ($payload['interventions'] ?? ''));

        if ($diagnosis !== '' || $interventions !== '') {
            return self::STAGE_TREATMENT_PLAN;
        }

        return self::STAGE_ASSESSMENT;
    }

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

        if (str_contains($outcome, 'progress review') || str_contains($outcome, 'discharge')) {
            return self::STAGE_PROGRESS;
        }

        if (str_contains($outcome, 'completed') || trim((string) ($payload['modalities'] ?? '')) !== '') {
            return self::STAGE_SESSION;
        }

        return self::STAGE_TREATMENT_PLAN;
    }

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

        if (str_contains($outcome, 'discharge') || str_contains($outcome, 'goals met')) {
            return self::STAGE_PROGRESS;
        }

        if (trim((string) ($payload['exercises'] ?? '')) !== ''
            || trim((string) ($payload['progress_notes'] ?? '')) !== '') {
            return self::STAGE_PROGRESS;
        }

        return self::STAGE_SESSION;
    }

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

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

    /**
     * @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_TREATMENT_PLAN, 'label' => 'Move to treatment plan'],
            self::STAGE_TREATMENT_PLAN => ['next' => self::STAGE_SESSION, 'label' => 'Move to therapy session'],
            self::STAGE_SESSION => ['next' => self::STAGE_PROGRESS, 'label' => 'Move to progress review'],
            self::STAGE_PROGRESS => ['next' => self::STAGE_COMPLETED, 'label' => 'Complete visit'],
            self::STAGE_COMPLETED => ['next' => self::STAGE_COMPLETED, 'label' => 'Completed'],
        ];
    }
}
