<?php
declare(strict_types=1);

/** Canonical lifecycle (separate from free-form tags). */
final class TicketWorkflow {
    public const TRIAGE = 'triage';
    public const IN_PROGRESS = 'in_progress';
    public const BLOCKED = 'blocked';
    public const WONT_FIX = 'wont_fix';
    public const DONE = 'done';
    public const CANCELLED = 'cancelled';

    /** @return list<string> */
    public static function all(): array {
        return [
            self::TRIAGE,
            self::IN_PROGRESS,
            self::BLOCKED,
            self::WONT_FIX,
            self::DONE,
            self::CANCELLED,
        ];
    }

    public static function normalize(string $state): string {
        $s = strtolower(trim($state));
        return in_array($s, self::all(), true) ? $s : self::TRIAGE;
    }

    /** @return array{id:string,label:string,bg:string} */
    public static function labelDef(string $state): array {
        $map = [
            self::TRIAGE => ['id' => self::TRIAGE, 'label' => 'triage', 'bg' => '#64748b'],
            self::IN_PROGRESS => ['id' => self::IN_PROGRESS, 'label' => 'in progress', 'bg' => '#3b82f6'],
            self::BLOCKED => ['id' => self::BLOCKED, 'label' => 'blocked', 'bg' => '#b45309'],
            self::WONT_FIX => ['id' => self::WONT_FIX, 'label' => "won't fix", 'bg' => '#6b7280'],
            self::DONE => ['id' => self::DONE, 'label' => 'done', 'bg' => '#047857'],
            self::CANCELLED => ['id' => self::CANCELLED, 'label' => 'cancelled', 'bg' => '#b91c1c'],
        ];
        return $map[self::normalize($state)] ?? $map[self::TRIAGE];
    }

    /** Map workflow to legacy status tag id for tag bar sync. */
    public static function statusTagId(string $state): string {
        return match (self::normalize($state)) {
            self::IN_PROGRESS => 'in-progress',
            self::BLOCKED => 'blocked',
            self::WONT_FIX => 'wont-fix',
            self::DONE => 'closed',
            self::CANCELLED => 'rejected',
            default => 'open',
        };
    }

    /**
     * Replace workflow/status tags; keep meta + custom tags.
     *
     * @param list<array{id:string,label:string,bg:string}> $tags
     * @return list<array{id:string,label:string,bg:string}>
     */
    public static function mergeTagsForState(array $tags, string $state): array {
        $keep = [];
        $statusIds = array_merge(TicketTagCatalog::statusIds(), TicketWorkflow::all());
        foreach (normalize_report_tags($tags) as $t) {
            if (TicketTagCatalog::isMetaId($t['id'])) {
                $keep[] = $t;
                continue;
            }
            if (in_array($t['id'], $statusIds, true)) {
                continue;
            }
            $keep[] = $t;
        }
        $sid = self::statusTagId($state);
        $preset = TicketTagCatalog::presets();
        foreach ($preset as $p) {
            if ($p['id'] === $sid) {
                $keep[] = $p;
                break;
            }
        }
        return TicketTagCatalog::normalize($keep);
    }
}
