import type { CurrentUser, DoneEvent, ErrorEvent, TrackEvent, WarningEvent } from './types'

type AssistantStatus = 'streaming' | 'done' | 'error'
export type AppMode = 'demo' | 'live'
export type TransportFailureKind =
  'cancelled' | 'http' | 'network' | 'parse' | 'protocol' | 'unexpected_eof'

export interface TransportFailure {
  kind: TransportFailureKind
  message: string
}

export interface PlaylistState {
  status: 'idle' | 'saving' | 'saved' | 'error'
  name: string | null
  url: string | null
  message: string | null
}

interface UserTurn {
  id: string
  role: 'user'
  text: string
}

export interface AssistantTurn {
  id: string
  role: 'assistant'
  query: string
  status: AssistantStatus
  requestId: string | null
  intentSummary: string
  candidateCount: number | null
  tracks: TrackEvent[]
  warnings: WarningEvent[]
  error: ErrorEvent | null
  transportFailure: TransportFailure | null
  completion: DoneEvent | null
  playlist: PlaylistState
}

export type ChatTurn = UserTurn | AssistantTurn

export type AuthState =
  | { status: 'checking'; user: null; message: null }
  | { status: 'anonymous'; user: null; message: null }
  | { status: 'authenticated'; user: CurrentUser; message: null }
  | { status: 'logging_out'; user: CurrentUser; message: null }
  | { status: 'failed'; user: null; message: string }

export type HealthState =
  | { status: 'checking'; mode: null; message: null }
  | { status: 'ready'; mode: AppMode; message: null }
  | { status: 'failed'; mode: null; message: string }

export interface EventLogEntry {
  timestamp: string
  type: string
  detail: string
}

export const EMPTY_PLAYLIST_STATE: PlaylistState = {
  status: 'idle',
  name: null,
  url: null,
  message: null,
}
