import type { PieceRunResult } from '../engine/piece-runner.js';

/** jobs_total / job_duration_seconds メトリクスの status ラベル値。 */
export type JobMetricStatus = 'succeeded' | 'failed' | 'aborted' | 'cancelled' | 'waiting_human' | 'error';

/**
 * Phase 3b: capture the terminal status for the jobs_total label.
 * result.status uses piece-runner's own enum
 * ('completed'|'aborted'|'error'|'waiting_human'|'waiting_subtasks'|'cancelled'); map to the
 * metric enum (waiting_subtasks stays "succeeded" for the metric
 * because the job pauses cleanly — not a failure).
 *
 * worker.ts executeJob 内の switch を純関数として切り出したもの（挙動不変）。
 */
export function mapPieceStatusToMetric(status: PieceRunResult['status']): JobMetricStatus {
  switch (status) {
    case 'completed': return 'succeeded';
    case 'aborted': return 'aborted';
    case 'cancelled': return 'cancelled';
    case 'waiting_human': return 'waiting_human';
    case 'waiting_subtasks': return 'succeeded';
    case 'error':
    default: return 'failed';
  }
}
