import galleryJson from '@/data/gallery.json'
import referencesJson from '@/data/references.json'

export type GalleryItem = {
  id: string
  picture: string
  title: string | null
  created_at: string
  /** A fájl valódi pixelmérete — ebből jön a helyfoglalás, így nincs ugrálás. */
  w: number
  h: number
}

export type Reference = {
  id: string
  name: string
  place: string | null
  picture: string
  short_message: string | null
  content: string | null
  created_at: string
  w: number
  h: number
}

/** A MySQL `galery` tábla, created_at DESC — ahogy a Symfony action rendezte. */
export const gallery = galleryJson as GalleryItem[]

/** A MySQL `reference` tábla, created_at DESC. */
export const references = referencesJson as Reference[]

/**
 * A főoldalon az eredeti 3-3 elemet mutatott.
 * A galéria maradt 3 kiemelt kép; a véleményeknél viszont most egyszerre
 * egy nagy kártya látszik, ezért az arcképsávba több pár kerül.
 */
export const homeGallery = gallery.slice(0, 6)
export const homeReferences = references.slice(0, 8)

export function getReference(id: string) {
  return references.find((r) => r.id === id)
}

/**
 * Az eredeti show action a szomszédos referenciákra léptetett.
 * Itt a lista tényleges sorrendjén megyünk körbe, így nem lehet
 * hiányzó id-re mutató link (az eredeti id+1 / id-1 aritmetikája
 * lyukas id-soroknál 404-re vitt).
 */
export function getReferenceNeighbours(id: string) {
  const i = references.findIndex((r) => r.id === id)
  if (i === -1) return { prev: null, next: null }
  const prev = references[(i - 1 + references.length) % references.length]
  const next = references[(i + 1) % references.length]
  return { prev, next }
}
