import type { ExecutionStatus } from '../../execution/core/sse-executions'
import type { TimeRange } from '../../operations/observability/types'

/**
 * @deprecated Use TimeRange from '@elevasis/core' directly. Kept as alias for backward compatibility.
 */
export type StatsTimeRange = TimeRange

/** Stats returned by /command-view/stats (counts only, no error details) */
export interface ResourceStats {
  resourceId: string
  totalRuns: number
  successCount: number
  failureCount: number // Used for badge: "X failed"
  warningCount: number // Completed with warnings (counts toward success)
  lastRunAt: string | null
  // NO recentErrors or totalErrors - fetched on-demand via /resource-errors
}

/** Response from /command-view/resource-errors (on-demand) */
export interface ResourceErrorsResponse {
  resourceId: string
  errors: ErrorSummary[]
  totalErrors: number // Total count for "showing 10 of X" display
  timeRange: StatsTimeRange
}

export interface ErrorSummary {
  executionId: string
  errorType: string
  errorMessage: string
  occurredAt: string
}

/** Single execution summary for Recent Executions list in command view */
export interface CommandViewExecution {
  executionId: string
  status: ExecutionStatus
  startedAt: string
  completedAt: string | null
  errorMessage: string | null // Only present if failed
}

/** Response from /command-view/resource-executions (on-demand) */
export interface ResourceExecutionsResponse {
  resourceId: string
  executions: CommandViewExecution[]
  totalExecutions: number // Total count for "showing 10 of X" display
  timeRange: StatsTimeRange
  page: number
  limit: number
}

export interface HumanCheckpointStats {
  checkpointId: string
  pendingCount: number
  completedCount: number
  expiredCount: number
  lastDecisionAt: string | null
}

/** Response from /command-view/stats */
export interface CommandViewStatsResponse {
  resources: Record<string, ResourceStats>
  humanCheckpoints: Record<string, HumanCheckpointStats>
  timeRange: StatsTimeRange
  generatedAt: string
}
