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

export interface BacklogPbi {
  id: string
  code: string | null
  title: string
  priority: number
  sort_order: number
  description?: string | null
  created_at: Date
  status: PbiStatusApi
}

export interface BacklogStory {
  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
}

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

export interface TaskDetail extends BacklogTask {
  _detail: true
  implementation_plan?: string | null
  acceptance_criteria?: string | null
  requires_opus?: boolean
  estimated_minutes?: number | null
}

export function isDetail(task: BacklogTask | TaskDetail): task is TaskDetail {
  return (task as TaskDetail)._detail === true
}

export interface ActiveProduct {
  id: string
  name: string
}

export interface ProductBacklogSnapshot {
  product?: ActiveProduct
  pbis: BacklogPbi[]
  storiesByPbi: Record<string, BacklogStory[]>
  tasksByStory: Record<string, BacklogTask[]>
}

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

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

export interface StoryRealtimeEvent {
  entity: 'story'
  op: Op
  id: string
  product_id: string
  pbi_id?: string
  [key: string]: unknown
}

export interface TaskRealtimeEvent {
  entity: 'task'
  op: Op
  id: string
  product_id: string
  story_id?: string
  [key: string]: unknown
}

export type ProductRealtimeEvent =
  | PbiRealtimeEvent
  | StoryRealtimeEvent
  | TaskRealtimeEvent

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

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

export interface OptimisticPbiOrderMutation {
  kind: 'pbi-order'
  prevPbiIds: string[]
}

export interface OptimisticEntityPatchMutation {
  kind: 'entity-patch'
  entity: 'pbi' | 'story' | 'task'
  id: string
  prev: BacklogPbi | BacklogStory | BacklogTask | TaskDetail | undefined
}

export type OptimisticMutation =
  | OptimisticPbiOrderMutation
  | OptimisticEntityPatchMutation

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

// PBI-79 / ST-1336: sprint-membership state voor backlog-page.
export interface PbiSummaryEntry {
  totalStoryCount: number
  inActiveSprintStoryCount: number
}

export interface CrossSprintBlock {
  sprintId: string
  sprintName: string
}

export interface SprintMembershipSlice {
  pbiSummary: Record<string, PbiSummaryEntry>
  crossSprintBlocks: Record<string, CrossSprintBlock>
  pending: { adds: string[]; removes: string[] }
  loadedSummaryForSprintId: string | null
}
