export interface SceneNode {
  id: string
  type?: 'video' | 'image'
  videoUrl: string
  imageUrl?: string
  thumbnail?: string
  contentSize?: { w: number; h: number }
  subtitleUrl?: string
  subtitles?: Record<string, string>
  choices?: Choice[]
  hotspots?: Hotspot[]
  qte?: QTEDefinition
  nextScene?: string | Choice[]
  onEnter?: Effect[]
  loopStart?: number
  loopEnd?: number
  bgmUrl?: string
  bgmVolume?: number
  bgmCrossFade?: number
  bgmDuckLevel?: number
  bgmDuckFade?: number
  videoMuted?: boolean
  skippable?: boolean
  streamingUrl?: Record<string, string>
}

export interface Choice {
  text: string
  textKey?: string
  prompt?: string
  promptKey?: string
  targetScene: string
  conditions?: Condition[]
  effects?: Effect[]
  timeLimit?: number
}

export interface Hotspot {
  id: string
  label: string
  labelKey?: string
  targetScene: string
  x: number
  y: number
  width: number
  height: number
  showAt?: number
  hideAt?: number
  conditions?: Condition[]
  effects?: Effect[]
  timeLimit?: number
}

export interface Condition {
  variable: string
  op: '>' | '<' | '>=' | '<=' | '==' | '!=' | 'hasFlag'  // @deprecated hasFlag will be removed
  value: number | string | boolean
}

export interface Effect {
  type: 'set' | 'add' | 'toggleFlag' | 'triggerEvent'  // @deprecated toggleFlag will be removed
  target: string
  value?: number | string | boolean
}

export interface QTEDefinition {
  triggerTime: number
  prompt: string
  promptKey?: string
  keys: string[]
  timeLimit: number
  successScene: string
  failScene: string
  effects?: {
    success: Effect[]
    fail: Effect[]
  }
}

export interface ChapterInfo {
  id: string
  label: string
  labelKey?: string
  startScene: string
  thumbnail?: string
  defaultVariables?: Record<string, number>
}

export interface AchievementDef {
  id: string
  title: string
  titleKey?: string
  description: string
  descKey?: string
  icon?: string
  hidden?: boolean
  condition: Condition
}

export interface EndingDef {
  id: string
  label: string
  labelKey?: string
  sceneId: string
  chapterId?: string
  thumbnail?: string
}

export interface LocalesConfig {
  path: string
  languages: string[]
}

export interface GameData {
  scenes: Record<string, SceneNode>
  startScene: string
  variables: Record<string, number>
  assetBase?: string
  locales?: LocalesConfig
  chapters?: ChapterInfo[]
  achievements?: AchievementDef[]
  endings?: EndingDef[]
  introVideo?: string
  menuVideo?: string
}

export interface ChoiceRecord {
  sceneId: string
  choiceIndex: number
  choiceText: string
}

export interface SaveData {
  slot: number
  timestamp: number
  currentScene: string
  variables: Record<string, number>
  flags: string[]
  history: ChoiceRecord[]
  thumbnail?: string
}

export type EngineEvent =
  | 'sceneChange'
  | 'choiceRequest'
  | 'choiceTimer'
  | 'gameEnd'
  | 'qteTrigger'
  | 'qteTimer'
  | 'qteResult'
  | 'videoEnd'
  | 'choiceTimeout'
  | 'hotspotRequest'
  | 'hotspotUpdate'
  | 'chapterUnlock'
  | 'achievementUnlock'

export interface PlayerTreeNode {
  sceneId: string
  label: string
  visited: boolean
  locked: boolean
  lockHint?: string
  children: PlayerTreeNode[]
  isGateway?: boolean
  gatewayChapterId?: string
}
