/** The shape `tools/foundry/kitindex.py` writes to `public/kits.json`. */

export type Tier = 'S' | 'A' | 'B' | 'C' | 'D'
export type Family = 'drums' | 'hits' | 'bass' | 'tonal' | 'vox' | 'fx'

export type Sample = {
  n: number                 // the index SuperDirt will answer to: `# n <n>`
  name: string
  file: string
  url: string
  sr: number
  ch: number
  peak_dbfs: number
  rms_dbfs: number
  env: number[]
  // present only for kits the Foundry cut — a hand-made kit has none of this
  bars: number | null
  bpm: number | null
  start_s: number | null      // where in the source track it was cut from
  dur_s: number | null
  tier: Tier | null
  grade: number | null
  family: Family | null
  stem_role: string | null
  track: string | null
  mode: 'loop' | 'chop' | null
  flags: string[]
  tags: Record<string, [string, number][]>
}

export type Kit = {
  kit: string
  n_samples: number
  source: 'foundry' | 'legacy'
  track: string | null
  family: Family | null
  bpm: number | null
  samples: Sample[]
}

export type KitIndex = {
  generated: string
  root: string
  peaks: number
  kits: Kit[]
}

/** Family → the role colour it shares with the orbit rail elsewhere on the bridge. */
export const FAMILY_COLOR: Record<string, string> = {
  drums: 'var(--color-percs)',
  hits: 'var(--color-blocked)',
  bass: 'var(--color-bass)',
  tonal: 'var(--color-melodic)',
  vox: 'var(--color-vox)',
  fx: 'var(--color-atmos)',
}
export const familyColor = (f?: string | null) =>
  FAMILY_COLOR[f ?? ''] ?? 'var(--color-ink-faint)'

/** Tempo, to two decimals, trailing zeros dropped: 123.05, 132.5, 120.
 *  One formatter so the transport, the rack header and every row agree — the same tempo
 *  printed two ways reads as a disagreement in the data. */
export const fmtBpm = (b: number) => String(Math.round(b * 100) / 100)

export const TIER_COLOR: Record<string, string> = {
  S: 'var(--color-ready)', A: 'var(--color-tops)', B: 'var(--color-wip)',
  C: 'var(--color-ink-faint)', D: 'var(--color-blocked)',
}
