import type { RecordJob } from '../../types'
import type { ContextMenuItem } from './ContextMenu'

export type DownloadMenuState = {
  watching?: boolean
  liked?: boolean | null          // true = like, false = dislike, null/undefined = neutral
  favorite?: boolean
  hot?: boolean
  keep?: boolean
}

export type DownloadMenuActions = {
  onPlay: (job: RecordJob) => void

  onToggleWatch: (job: RecordJob) => void
  onSetLike: (job: RecordJob, liked: boolean | null) => void
  onToggleFavorite: (job: RecordJob) => void
  onMoreFromModel: (modelName: string, job: RecordJob) => void

  onRevealInExplorer: (job: RecordJob) => void
  onAddToDownloadList: (job: RecordJob) => void
  onToggleHot: (job: RecordJob) => void

  onToggleKeep: (job: RecordJob) => void
  onDelete: (job: RecordJob) => void
}

type BuildArgs = {
  job: RecordJob
  modelName: string
  state: DownloadMenuState
  actions: DownloadMenuActions
}

export function buildDownloadContextMenu({ job, modelName, state, actions }: BuildArgs): ContextMenuItem[] {
  const watching = !!state.watching
  const favorite = !!state.favorite
  const hot = !!state.hot
  const keep = !!state.keep
  const liked = state.liked // true / false / null

  return [
    { label: 'Abspielen', onClick: () => actions.onPlay(job) },

    // optionaler Separator-Style: wenn du willst, kann ContextMenu auch "separator" Items rendern
    { label: watching ? 'Nicht beobachten' : 'Beobachten', onClick: () => actions.onToggleWatch(job) },

    {
      label: liked === true ? 'Gefällt mir entfernen' : 'Gefällt mir',
      onClick: () => actions.onSetLike(job, liked === true ? null : true),
    },
    {
      label: liked === false ? 'Gefällt mir nicht entfernen' : 'Gefällt mir nicht',
      onClick: () => actions.onSetLike(job, liked === false ? null : false),
    },

    { label: favorite ? 'Von Favoriten entfernen' : 'Als Favorit markieren', onClick: () => actions.onToggleFavorite(job) },

    {
      label: `Mehr von ${modelName} anzeigen`,
      onClick: () => actions.onMoreFromModel(modelName, job),
      disabled: !modelName || modelName === '—',
    },

    { label: 'Dateipfad im Explorer öffnen', onClick: () => actions.onRevealInExplorer(job), disabled: !job.output },

    { label: 'Zur Downloadliste hinzufügen', onClick: () => actions.onAddToDownloadList(job) },

    { label: hot ? 'Nicht mehr als HOT markieren' : 'Als HOT markieren', onClick: () => actions.onToggleHot(job) },

    { label: keep ? 'Nicht behalten' : 'Behalten', onClick: () => actions.onToggleKeep(job) },

    { label: 'Löschen', onClick: () => actions.onDelete(job), danger: true, disabled: keep },
  ]
}
