import type { OperationResultDto } from '../api/contracts'

export type OpenClawSurfaceState =
  | 'ready'
  | 'connected'
  | 'initializing'
  | 'reconnecting'
  | 'disconnected'
  | 'degraded'
  | 'failed'
  | 'unsupported'
  | 'forbidden'
  | 'management_disabled'
  | 'invalid'
  | 'error'

export interface OpenClawConnection {
  state: OpenClawSurfaceState
  configured: boolean
  credentialConfigured: boolean
  connected: boolean
  endpoint: string
  gatewayVersion: string | null
  requiredVersion: string | null
  versionPinned: boolean
  versionMatches: boolean
  protocolVersion: number | null
  grantedScopes: string[]
  advertisedEvents: string[]
  lastConnectedAt: string | null
  lastEventAt: string | null
  reconnectAttempts: number
  message: string | null
  recovery: string | null
  checkedAt: string
  deviceId: string | null
  pairingRequired: boolean
  pairingRequestId: string | null
}

export interface OpenClawCapability {
  id: string
  label: string
  method: string
  requiredScope: string
  available: boolean
  state: OpenClawSurfaceState
  reason: string | null
}

export interface OpenClawCollection<T> {
  state: OpenClawSurfaceState
  items: T[]
  nextCursor: string | null
  message: string | null
  recovery: string | null
  checkedAt: string
}

export interface OpenClawOperation<T> {
  ok: boolean
  state: string
  message: string
  data: T | null
  recovery: string | null
  completedAt: string
  operationId: string | null
  correlationId: string | null
  idempotencyKey: string | null
  traceParent: string | null
  actor: string | null
  operation?: OperationResultDto | null
}

export interface OpenClawTask {
  id: string
  title: string
  status: string
  kind: string | null
  runtime: string | null
  agentId: string | null
  sessionKey: string | null
  runId: string | null
  flowId: string | null
  parentTaskId: string | null
  createdAt: string | null
  startedAt: string | null
  updatedAt: string | null
  finishedAt: string | null
  progress: number | null
  summary: string | null
  error: string | null
  canCancel: boolean
}

export interface OpenClawSession {
  key: string
  sessionId: string | null
  agentId: string
  title: string
  status: string
  kind: string | null
  channel: string | null
  model: string | null
  provider: string | null
  runId: string | null
  updatedAt: string | null
  inputTokens: number | null
  outputTokens: number | null
  totalTokens: number | null
  canAbort: boolean
}

export interface OpenClawCronJob {
  id: string
  name: string
  description: string | null
  schedule: string
  timeZone: string | null
  enabled: boolean
  status: string
  agentId: string | null
  sessionKey: string | null
  nextRunAt: string | null
  lastRunAt: string | null
  lastRunStatus: string | null
  lastError: string | null
  canRun: boolean
  resourceHash: string | null
}

export interface OpenClawCronSchedule {
  kind: string
  expression: string | null
  timeZone: string | null
  at: string | null
  everyMs: number | null
  anchorMs: number | null
  staggerMs: number | null
  command: string | null
  workingDirectory: string | null
}

export interface OpenClawCronPayload {
  kind: string
  text: string | null
  message: string | null
  model: string | null
  fallbacks: string[]
  thinking: string | null
  timeoutSeconds: number | null
  allowUnsafeExternalContent: boolean | null
  lightContext: boolean | null
  toolsAllow: string[]
  arguments: string[]
  workingDirectory: string | null
  environmentKeys: string[]
  inputConfigured: boolean
  noOutputTimeoutSeconds: number | null
  outputMaxBytes: number | null
}

export interface OpenClawCronDestination {
  channel: string | null
  target: string | null
  accountId: string | null
  mode: string | null
}

export interface OpenClawCronDelivery {
  mode: string
  channel: string | null
  target: string | null
  threadId: string | null
  accountId: string | null
  bestEffort: boolean | null
  completionDestination: OpenClawCronDestination | null
  failureDestination: OpenClawCronDestination | null
}

export interface OpenClawCronTrigger {
  script: string
  once: boolean
}

export interface OpenClawCronFailureAlert {
  after: number | null
  channel: string | null
  target: string | null
  cooldownMs: number | null
  includeSkipped: boolean | null
  mode: string | null
  accountId: string | null
}

export interface OpenClawCronJobDetail {
  id: string
  name: string
  displayName: string | null
  description: string | null
  enabled: boolean
  deleteAfterRun: boolean
  agentId: string | null
  sessionKey: string | null
  sessionTarget: string
  wakeMode: string
  schedule: OpenClawCronSchedule
  payload: OpenClawCronPayload
  delivery: OpenClawCronDelivery | null
  trigger: OpenClawCronTrigger | null
  failureAlert: OpenClawCronFailureAlert | null
  createdAt: string | null
  updatedAt: string | null
  nextRunAt: string | null
  lastRunAt: string | null
  lastRunStatus: string | null
  lastError: string | null
  resourceHash: string
  canUpdate: boolean
  canDelete: boolean
  canRun: boolean
}

export interface OpenClawCronRunDiagnostic {
  occurredAt: string | null
  source: string
  severity: string
  message: string
  toolName: string | null
  exitCode: number | null
  truncated: boolean
}

export interface OpenClawCronRun {
  id: string
  jobId: string
  jobName: string | null
  runId: string | null
  status: string
  action: string
  summary: string | null
  error: string | null
  errorReason: string | null
  deliveryStatus: string | null
  deliveryError: string | null
  delivered: boolean | null
  triggerFired: boolean | null
  diagnosticsSummary: string | null
  diagnostics: OpenClawCronRunDiagnostic[]
  sessionId: string | null
  sessionKey: string | null
  occurredAt: string | null
  runAt: string | null
  durationMs: number | null
  nextRunAt: string | null
  model: string | null
  provider: string | null
  inputTokens: number | null
  outputTokens: number | null
  totalTokens: number | null
}

export interface CreateOpenClawCronJobRequest {
  name: string
  schedule: Record<string, unknown>
  sessionTarget: string
  wakeMode: string
  payload: Record<string, unknown>
  description?: string | null
  enabled?: boolean
  agentId?: string | null
  sessionKey?: string | null
  deleteAfterRun?: boolean | null
  delivery?: Record<string, unknown> | null
  trigger?: Record<string, unknown> | null
  failureAlert?: Record<string, unknown> | boolean | null
  declarationKey?: string | null
  displayName?: string | null
}

export interface PatchOpenClawCronJobRequest {
  patch: Record<string, unknown>
  expectedHash: string
}

export interface OpenClawCronRunEnqueue {
  jobId: string
  enqueued: boolean
  runId: string | null
}

export interface OpenClawQueuedCronRun {
  jobId: string
  runId: string | null
  enqueuedAt: string
}

export interface OpenClawActivity {
  id: string
  eventType: string
  kind: string
  action: string
  status: string
  message: string
  severity: string | null
  actor: string | null
  agentId: string | null
  sessionKey: string | null
  runId: string | null
  occurredAt: string | null
  source: string
}

export interface OpenClawApproval {
  id: string
  kind: string
  title: string
  description: string | null
  status: string
  severity: string
  command: string | null
  workingDirectory: string | null
  agentId: string | null
  sessionKey: string | null
  requestedAt: string | null
  expiresAt: string | null
  allowedDecisions: string[]
  canResolve: boolean
}

export interface OpenClawModel {
  id: string
  name: string
  provider: string
  configured: boolean
  available: boolean
  contextWindow: number | null
  reason: string | null
}

export interface OpenClawModelAuthExpiry {
  at: string
  remainingMs: number
  label: string
}

export interface OpenClawModelAuthProfileSummary {
  type: 'oauth' | 'token' | 'api_key' | 'unknown'
  status: string
  count: number
}

export interface OpenClawModelAuthApiKey {
  source: 'config' | 'env'
  envVar: string | null
}

export interface OpenClawModelAuthUsage {
  summary: string | null
  plan: string | null
}

export interface OpenClawModelAuthProvider {
  provider: string
  displayName: string
  status: string
  expiry: OpenClawModelAuthExpiry | null
  profiles: OpenClawModelAuthProfileSummary[]
  apiKey: OpenClawModelAuthApiKey | null
  usage: OpenClawModelAuthUsage | null
}

export interface OpenClawAgent {
  id: string
  name: string
  description: string | null
  model: string | null
  provider: string | null
  workspace: string | null
  status: string
}

export interface OpenClawOverview {
  connection: OpenClawConnection
  capabilities: OpenClawCapability[]
  tasks: OpenClawCollection<OpenClawTask>
  sessions: OpenClawCollection<OpenClawSession>
  cronJobs: OpenClawCollection<OpenClawCronJob>
  approvals: OpenClawCollection<OpenClawApproval>
  activity: OpenClawCollection<OpenClawActivity>
  models: OpenClawCollection<OpenClawModel>
  agents: OpenClawCollection<OpenClawAgent>
  generatedAt: string
}
