<?php

namespace App\Services\Care\Pathology;

/**
 * Map clinic pathology specialty progress onto visit stages.
 */
class PathologyWorkflowService
{
    public const STAGE_CHECK_IN = 'check_in';

    public const STAGE_REQUEST = 'request';

    public const STAGE_SPECIMEN = 'specimen';

    public const STAGE_PROCESSING = 'processing';

    public const STAGE_REPORT = 'report';

    public const STAGE_VERIFIED = 'verified';

    public const STAGE_COMPLETED = 'completed';

    /**
     * @param  array<string, mixed>  $payload
     */
    public function stageFromRequest(array $payload): string
    {
        if (! empty($payload['specimen_type']) || ! empty($payload['clinical_history'])) {
            return self::STAGE_REQUEST;
        }

        return self::STAGE_CHECK_IN;
    }

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

        return self::STAGE_REQUEST;
    }

    /**
     * @param  array<string, mixed>  $payload
     */
    public function stageFromProcessing(array $payload): string
    {
        $status = strtolower((string) ($payload['status'] ?? ''));
        if (str_contains($status, 'ready')) {
            return self::STAGE_REPORT;
        }

        if (! empty($payload['status']) || ! empty($payload['gross'])) {
            return self::STAGE_PROCESSING;
        }

        return self::STAGE_SPECIMEN;
    }

    /**
     * @param  array<string, mixed>  $payload
     */
    public function stageFromReport(array $payload): string
    {
        if (! empty($payload['diagnosis']) && ! empty($payload['microscopy'])) {
            return self::STAGE_REPORT;
        }

        return self::STAGE_PROCESSING;
    }

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

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

        return self::STAGE_REPORT;
    }

    /**
     * @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_REQUEST, 'label' => 'Start request'],
            self::STAGE_REQUEST => ['next' => self::STAGE_SPECIMEN, 'label' => 'Move to specimen'],
            self::STAGE_SPECIMEN => ['next' => self::STAGE_PROCESSING, 'label' => 'Move to processing'],
            self::STAGE_PROCESSING => ['next' => self::STAGE_REPORT, 'label' => 'Move to report'],
            self::STAGE_REPORT => ['next' => self::STAGE_VERIFIED, 'label' => 'Move to verification'],
            self::STAGE_VERIFIED => ['next' => self::STAGE_COMPLETED, 'label' => 'Complete visit'],
            self::STAGE_COMPLETED => ['next' => self::STAGE_COMPLETED, 'label' => 'Completed'],
        ];
    }
}
