import type { TaskStatusApi } from '@/lib/task-status'

export type SprintStatus = 'OPEN' | 'CLOSED'

export interface SprintWorkspaceSprint {
  id: string
  product_id: string
  code: string
  sprint_goal: string
  status: SprintStatus
  start_date: string | null
  end_date: string | null
  created_at: Date
  completed_at: Date | null
}

export interface SprintWorkspaceStory {
  id: string
  code: string | null
  title: string
  description: string | null
  acceptance_criteria: string | null
  priority: number
  sort_order: number
  status: string
  pbi_id: string
  sprint_id: string | null
  created_at: Date
  taskCount?: number
  doneCount?: number
  assignee_id?: string | null
  assignee_username?: string | null
}

export interface SprintWorkspaceTask {
  id: string
  code: string | null
  title: string
  description: string | null
  priority: number
  sort_order: number
  status: TaskStatusApi | string
  story_id: string
  sprint_id: string | null
  created_at: Date
}

export interface SprintWorkspaceTaskDetail extends SprintWorkspaceTask {
  _detail: true
  implementation_plan?: string | null
  acceptance_criteria?: string | null
  requires_opus?: boolean
  verify_only?: boolean
  estimated_minutes?: number | null
}

export function isDetail(
  task: SprintWorkspaceTask | SprintWorkspaceTaskDetail,
): task is SprintWorkspaceTaskDetail {
  return (task as SprintWorkspaceTaskDetail)._detail === true
}

export interface ActiveProductRef {
  id: string
  name: string
}

export interface SprintWorkspaceSnapshot {
  product?: ActiveProductRef
  sprint?: SprintWorkspaceSprint
  stories: SprintWorkspaceStory[]
  tasksByStory: Record<string, SprintWorkspaceTask[]>
}

export interface ProductSprintsList {
  productId: string
  sprints: SprintWorkspaceSprint[]
}

export type Op = 'I' | 'U' | 'D'

export interface SprintEntityRealtimeEvent {
  entity: 'sprint'
  op: Op
  id: string
  product_id: string
  [key: string]: unknown
}

export interface SprintStoryRealtimeEvent {
  entity: 'story'
  op: Op
  id: string
  product_id: string
  pbi_id?: string
  sprint_id?: string | null
  [key: string]: unknown
}

export interface SprintTaskRealtimeEvent {
  entity: 'task'
  op: Op
  id: string
  product_id: string
  story_id?: string
  sprint_id?: string | null
  [key: string]: unknown
}

export type SprintRealtimeEvent =
  | SprintEntityRealtimeEvent
  | SprintStoryRealtimeEvent
  | SprintTaskRealtimeEvent

export type ResyncReason =
  | 'visible'
  | 'reconnect'
  | 'manual'
  | 'unknown-event'
  | 'stale-scope'
  | 'mutation-settled'

export type RealtimeStatus = 'connecting' | 'open' | 'disconnected'

export interface OptimisticEntityPatchMutation {
  kind: 'entity-patch'
  entity: 'sprint' | 'story' | 'task'
  id: string
  prev:
    | SprintWorkspaceSprint
    | SprintWorkspaceStory
    | SprintWorkspaceTask
    | SprintWorkspaceTaskDetail
    | undefined
}

export type OptimisticMutation =
  | OptimisticEntityPatchMutation

export interface PendingOptimisticMutation {
  id: string
  mutation: OptimisticMutation
  createdAt: number
}
