import type { ClaudeJobStatusApi } from '@/lib/job-status'

export type SoloTaskStatus = 'TO_DO' | 'IN_PROGRESS' | 'REVIEW' | 'DONE'
export type SoloColumnStatus = 'TO_DO' | 'IN_PROGRESS' | 'DONE'
export type SoloVerifyRequired = 'ALIGNED' | 'ALIGNED_OR_PARTIAL' | 'ANY'
export type VerifyResultApi = 'aligned' | 'partial' | 'empty' | 'divergent'

export interface SoloTask {
  id: string
  title: string
  description: string | null
  implementation_plan: string | null
  priority: number
  sort_order: number
  status: SoloTaskStatus
  verify_only: boolean
  verify_required: SoloVerifyRequired
  story_id: string
  story_code: string | null
  story_title: string
  task_code: string | null
  pbi_code: string | null
  pbi_title: string | null
  pbi_description: string | null
}

export interface SoloUnassignedStoryTask {
  id: string
  title: string
  description: string | null
  priority: number
  status: string
}

export interface SoloUnassignedStory {
  id: string
  code: string | null
  title: string
  tasks: SoloUnassignedStoryTask[]
}

export interface SoloWorkspaceProduct {
  id: string
  name: string
  repo_url?: string | null
}

export interface SoloWorkspaceSprint {
  id: string
  sprint_goal: string
}

export interface SoloWorkspaceSnapshot {
  product: SoloWorkspaceProduct
  sprint: SoloWorkspaceSprint
  activeUserId: string
  tasks: SoloTask[]
  unassignedStories: SoloUnassignedStory[]
}

export interface JobState {
  job_id: string
  task_id: string
  status: ClaudeJobStatusApi
  branch?: string
  pushed_at?: string | null
  pr_url?: string | null
  verify_result?: VerifyResultApi | null
  summary?: string
  error?: string
}

export type ClaudeJobEvent =
  | {
      type: 'claude_job_enqueued'
      job_id: string
      task_id: string
      user_id: string
      product_id: string
      status: 'queued'
    }
  | {
      type: 'claude_job_status'
      job_id: string
      task_id: string
      user_id: string
      product_id: string
      status: ClaudeJobStatusApi
      branch?: string
      pushed_at?: string
      pr_url?: string
      verify_result?: VerifyResultApi
      summary?: string
      error?: string
    }

export interface RealtimeEvent {
  op: 'I' | 'U' | 'D'
  entity: 'task' | 'story'
  id: string
  story_id?: string
  product_id: string
  sprint_id: string | null
  assignee_id: string | null
  status?: SoloTaskStatus | 'OPEN' | 'IN_SPRINT' | 'DONE'
  sort_order?: number
  title?: string
  code?: string | null
  description?: string | null
  priority?: number
  task_status?: SoloTaskStatus
  task_sort_order?: number
  task_title?: string
  story_status?: 'OPEN' | 'IN_SPRINT' | 'DONE'
  story_sort_order?: number
  story_title?: string
  story_code?: string | null
  changed_fields?: string[]
}

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

export type ResyncReason = 'visible' | 'reconnect' | 'manual' | 'unknown-event'
