import type { ExecutionLogMessage } from '../engine/base/logging'
import type { ResourceStatus } from '../../platform/registry/types'

// Execution status type shared between API and UI
export type ExecutionStatus = 'pending' | 'running' | 'completed' | 'failed' | 'warning'

/**
 * Event sent when new execution starts
 */
export interface ExecutionStartedEvent {
  type: 'new-execution'
  resourceId: string
  executionId: string
  timestamp: number
  data?: undefined
}

/**
 * Event sent when execution log message is emitted
 */
export interface ExecutionLogEvent {
  type: 'log'
  resourceId: string
  executionId: string
  timestamp: number
  data: {
    log: ExecutionLogMessage
  }
}

/**
 * Event sent when execution completes (success or failure)
 */
export interface ExecutionCompleteEvent {
  type: 'execution-complete'
  resourceId: string
  executionId: string
  timestamp: number
  data: {
    success: boolean
    status?: ExecutionStatus
    result?: unknown
    error?: string
  }
}

/**
 * Connection confirmation event
 */
export interface ExecutionConnectedEvent {
  type: 'connected'
  resourceId: string
  executionId?: undefined
  timestamp: number
  data?: undefined
}

/**
 * Union of all execution SSE events
 */
export type ExecutionSSEEvent =
  | ExecutionStartedEvent
  | ExecutionLogEvent
  | ExecutionCompleteEvent
  | ExecutionConnectedEvent

// API execution types
export interface APIExecutionSummary {
  id: string
  status: ExecutionStatus
  startTime: number
  endTime?: number
  resourceStatus?: ResourceStatus // 'dev' | 'prod' - optional for backward compatibility
}

export interface APIExecutionDetail extends APIExecutionSummary {
  executionLogs: ExecutionLogMessage[]
  input?: unknown
  result?: unknown
  error?: string
  resourceStatus: ResourceStatus // Required in detail view
  apiVersion?: string | null
  resourceVersion?: string | null
  sdkVersion?: string | null
  isArchived?: boolean
  archivedLogCount?: number
}

// Archived logs pointer stored in execution_logs.logs column after archival
export interface ArchivedLogsPointer {
  __archived: true
  storage_path: string
  archived_at: string
  original_count: number
}

export function isArchivedLogs(logs: unknown): logs is ArchivedLogsPointer {
  return (
    typeof logs === 'object' &&
    logs !== null &&
    !Array.isArray(logs) &&
    (logs as Record<string, unknown>).__archived === true
  )
}

// API request/response types
export interface APIExecutionListResponse {
  executions: APIExecutionSummary[]
  total?: number
  limit?: number
  offset?: number
  hasMore?: boolean
}

// Organization -> Resource -> Execution structure
export type ExecutionMap = Map<string, APIExecutionDetail>
export type ResourceExecutionMap = Map<string, ExecutionMap>
export type OrganizationExecutionStore = Map<string, ResourceExecutionMap>

// SSE client types - generic to avoid Fastify dependency in core
export type ClientSet<T = unknown> = Set<T>
export type ResourceClientMap<T = unknown> = Map<string, ClientSet<T>>
export type OrganizationClientStore<T = unknown> = Map<string, ResourceClientMap<T>>
