export type LoadStatus = 'idle' | 'loading' | 'loaded' | 'empty' | 'error'

export type ChangelogEntry = {
  hash: string
  shortHash: string
  date: string
  subject: string
  body: string
}

export type ChangelogData = {
  generatedAt: string
  entries: ChangelogEntry[]
}

export type ScoreSide = 'left' | 'right'

export type PlayerSlot = 'playerA' | 'playerB'

export type CourtSide = 'left' | 'right'

export type GroupTeam = {
  id: number
  playerA: string
  playerB: string
  isPlaceholderA: boolean
  isPlaceholderB: boolean
}

export type RoundGroup = {
  id: number
  teams: GroupTeam[]
}

export type MatchResultsRecord = {
  time: number
  personnel: string
  battlecombination: string | null
}

export type Matchup = {
  leftTeamId: number | null
  rightTeamId: number | null
}

export type ActiveMatchup = {
  leftTeam: GroupTeam | null
  rightTeam: GroupTeam | null
}

export type ScoreState = {
  scoreLeft: number
  scoreRight: number
  gamesLeft: number
  gamesRight: number
  currentGame: number
  targetScore: number
  initialServing: ScoreSide | null
  serving: ScoreSide | null
  leftRightCourtPlayer: PlayerSlot
  rightRightCourtPlayer: PlayerSlot
}

export type PointHistoryEntry = {
  round: number
  starter: number
  winCount: number
  winner: 0 | 1
}

export type ScoreSnapshot = {
  pointLog: PointHistoryEntry[]
  scoreState: ScoreState
}

export type MatchHistoryItem = {
  id: string
  playedAt: string
  matchDate: string
  source: 'db' | 'manual' | 'idle'
  groupId: number
  leftTeamName: string
  rightTeamName: string
  scoreLeft: number
  scoreRight: number
  winner: string
}

export type HistoryUploadPayload = {
  dayOfWeek: number
  players: string[]
  score: [number, number]
  scoreList: Array<[number, number, number, 0 | 1]>
  team: [string[], string[]]
  time: number
  type: 0 | 1
  winScore: number
}

export type HistoryUploadResponse = {
  id: number
}

export type HistoryRecord = {
  id: number
  time: number
  dayOfWeek: number
  score: string
  winScore: number
  type: 0 | 1
  players: string
  team: string
  scoreList: string | null
}

export type HistoryListItem = {
  id: number
  time: number
  playedAt: string
  dayOfWeek: number
  dayLabel: string
  score: [number, number]
  winScore: number
  type: 0 | 1
  typeLabel: string
  players: string[]
  team: [string[], string[]]
  scoreList: Array<[number, number, number, 0 | 1]>
  leftTeamName: string
  rightTeamName: string
  winnerTeamName: string
}

export type HistoryListPage = {
  items: HistoryListItem[]
  page: number
  total: number
  totalPages: number
}

export type LiveRoomStatus = 'live' | 'finished'

export type LiveRoomSession = {
  hostToken: string
  roomId: string
  status: LiveRoomStatus
}

export type LiveRoomSummary = {
  roomId: string
  createdAt: string
  leftTeamName: string
  rightTeamName: string
  scoreLeft: number
  scoreRight: number
  status: LiveRoomStatus
  targetScore: number
  updatedAt: string
}

export type LiveRoomPayload = {
  groupId: number | null
  leftTeamName: string
  matchupLabel: string
  pointLog: PointHistoryEntry[]
  rightTeamName: string
  scoreState: ScoreState
  targetDate: string
}

export type LiveRoomDetail = LiveRoomPayload & {
  createdAt: string
  roomId: string
  status: LiveRoomStatus
  updatedAt: string
  winnerTeamName: string | null
}

export type LiveRoomUpdatePayload = LiveRoomPayload & {
  hostToken: string
  status: LiveRoomStatus
  winnerTeamName: string | null
}
