import { sanitizeFileBaseName } from "@/lib/media-manager/paths"

/** Canonical file base names aligned with dashboard section labels. */
export const MEDIA_FILE_BASE = {
  mainPhoto: "main-photo",
  doctorPhoto: "doctor-photo",
  gallery: "gallery",
  heroGallery: "hero-gallery",
  beforeAfter: "before-after",
  certificates: "certificates",
  accreditationLogo: "accreditation-logo",
  accreditationCertificate: "accreditation-certificate",
  overview: "overview",
  reviews: "reviews",
  coverImage: "cover-image",
  contentImages: "content-images",
  seoImages: "seo-images",
  userAvatar: "avatar",
} as const

export type MediaFilePart = "before" | "after"

export interface ResolveUploadFileNameInput {
  /** R2 folder segment (e.g. main-photo, gallery, certificates). */
  section: string
  /** File base name when it differs from the folder segment (e.g. reviews inside certificates/). */
  fileBaseName?: string
  /** Zero-based index for multiple images in the same section. */
  index?: number
  /** before / after pair within a before-after section. */
  part?: MediaFilePart
}

/**
 * Build a stable file base name for R2 object keys.
 *
 * - Single image (no index): `main-photo`, `cover-image`, `doctor-photo`
 * - Multiple images: `gallery1`, `gallery2`, `reviews1`, `hero-gallery1`
 * - Before/after pairs: `before1`, `after1`, `before2`, `after2`
 */
export function buildMediaFileBaseName(
  base: string,
  options?: { index?: number; part?: MediaFilePart }
): string {
  const normalized = sanitizeFileBaseName(base)
  if (!normalized) return "image"

  const idx = options?.index
  const hasIndex = typeof idx === "number" && Number.isInteger(idx) && idx >= 0

  if (options?.part === "before" || options?.part === "after") {
    const n = (hasIndex ? idx : 0) + 1
    return `${options.part}${n}`
  }

  if (hasIndex) {
    return `${normalized}${idx + 1}`
  }

  return normalized
}

/** Resolve the file base name used on upload (section-driven, not the user's original filename). */
export function resolveUploadFileName(input: ResolveUploadFileNameInput): string {
  const base =
    (typeof input.fileBaseName === "string" && input.fileBaseName.trim()) ||
    (typeof input.section === "string" && input.section.trim()) ||
    "image"
  return buildMediaFileBaseName(base, { index: input.index, part: input.part })
}

export function parseUploadFileIndex(value: unknown): number | undefined {
  if (typeof value === "number" && Number.isInteger(value) && value >= 0) return value
  if (typeof value === "string" && /^\d+$/.test(value.trim())) {
    const n = Number(value.trim())
    return Number.isInteger(n) && n >= 0 ? n : undefined
  }
  return undefined
}

export function parseUploadFilePart(value: unknown): MediaFilePart | undefined {
  return value === "before" || value === "after" ? value : undefined
}

/** Resolve file base name from upload API body (commit sends structured fields). */
export function resolveUploadFileNameFromBody(
  section: string,
  body: Record<string, unknown>
): string {
  const explicit = typeof body.fileName === "string" ? sanitizeFileBaseName(body.fileName) : ""
  if (explicit) return explicit

  return resolveUploadFileName({
    section,
    fileBaseName: typeof body.fileBaseName === "string" ? body.fileBaseName : undefined,
    index: parseUploadFileIndex(body.fileIndex),
    part: parseUploadFilePart(body.filePart),
  })
}
