<?php

declare(strict_types=1);

namespace App\Support\OpsUx;

/**
 * Normalize OperationRun legacy storage fields into Ops-UX canonical statuses.
 */
final class OperationStatusNormalizer
{
    /**
     * Returns one of: queued|running|succeeded|partial|blocked|failed.
     */
    public static function toUxStatus(?string $status, ?string $outcome): string
    {
        $status = strtolower(trim((string) $status));
        $outcome = strtolower(trim((string) $outcome));

        if ($status === 'queued') {
            return 'queued';
        }

        if ($status === 'running') {
            return 'running';
        }

        if ($status === 'completed' && $outcome === 'blocked') {
            return 'blocked';
        }

        // Terminal normalization (compatibility)
        if ($status === 'failed' || $outcome === 'failed') {
            return 'failed';
        }

        if ($status === 'completed' && $outcome === 'partially_succeeded') {
            return 'partial';
        }

        if ($status === 'completed' && $outcome === 'succeeded') {
            return 'succeeded';
        }

        // Best-effort fallback.
        return match ($outcome) {
            'partially_succeeded' => 'partial',
            'succeeded' => 'succeeded',
            'blocked' => 'blocked',
            'failed' => 'failed',
            default => 'failed',
        };
    }
}
