﻿import { randomUUID } from "crypto"
import { NextResponse } from "next/server"
import { touchUpdatedAt } from "@/lib/content-audit"
import { prisma } from "@/lib/prisma"
import { getCurrentUser, isAllowedRole } from "@/lib/user"
import { deleteImage, extractMediaKeyFromUrl } from "@/lib/r2-storage"
import {
  getPersistedMediaValidationError,
  normalizePersistedMediaInPayload,
} from "@/lib/media-manager/validate-persisted-payload"
import {
  HOSPITALS_PAGE_ENTITY_KEY,
  HOSPITALS_PAGE_LOCALE_CODES,
  type HospitalsPageLocaleCode,
} from "@/lib/pages/hospitals-page/locales"
import {
  createEmptyHospitalsPageFormState,
  type HospitalsPageFormStateShape,
} from "@/lib/pages/hospitals-page/form-types"
import { normalizeHospitalsPageLocaleImagesForStorage } from "@/lib/pages/hospitals-page/shared-images"
import {
  parseHospitalsPageJsonForStorage,
  parseHospitalsPagePutBody,
  type HospitalsPageSectionKey,
} from "@/lib/pages/hospitals-page/validate-payload"

export const dynamic = "force-dynamic"
export const revalidate = 0

const NO_CACHE_HEADERS = {
  "Cache-Control": "no-store, no-cache, must-revalidate, proxy-revalidate",
  Pragma: "no-cache",
  Expires: "0",
}

async function ensureAdmin() {
  const user = await getCurrentUser()
  if (!user) {
    return { ok: false as const, res: NextResponse.json({ error: "Unauthorized" }, { status: 401, headers: NO_CACHE_HEADERS }) }
  }
  if (!isAllowedRole(user.role, ["admin"])) {
    return { ok: false as const, res: NextResponse.json({ error: "Forbidden" }, { status: 403, headers: NO_CACHE_HEADERS }) }
  }
  return { ok: true as const, user }
}

function fail(message: string, status = 400) {
  return NextResponse.json({ success: false as const, error: message }, { status, headers: NO_CACHE_HEADERS })
}

function rowToFormState(contentJson: unknown): HospitalsPageFormStateShape {
  let parsed: unknown = contentJson
  if (typeof contentJson === "string") {
    try {
      parsed = JSON.parse(contentJson)
    } catch {
      parsed = undefined
    }
  }
  return parseHospitalsPageJsonForStorage(parsed).data
}

function buildMergedLocalesForSectionSave(
  existingByLocale: Partial<Record<HospitalsPageLocaleCode, HospitalsPageFormStateShape>>,
  incomingLocales: Record<string, HospitalsPageFormStateShape>,
  sectionKey: HospitalsPageSectionKey
): Record<HospitalsPageLocaleCode, HospitalsPageFormStateShape> {
  const next: Record<HospitalsPageLocaleCode, HospitalsPageFormStateShape> = {
    en: createEmptyHospitalsPageFormState(),
    tr: createEmptyHospitalsPageFormState(),
    ar: createEmptyHospitalsPageFormState(),
  }
  for (const locale of HOSPITALS_PAGE_LOCALE_CODES) {
    const base = existingByLocale[locale] ?? createEmptyHospitalsPageFormState()
    const incoming = incomingLocales[locale] ?? base
    next[locale] = { ...base, [sectionKey]: incoming[sectionKey] }
  }
  return next
}

function collectHospitalsPageImagePublicIds(state: HospitalsPageFormStateShape): Set<string> {
  const ids = new Set<string>()
  const explicit = state.hero.imagePublicId.trim()
  if (explicit.startsWith("hospitals-page/")) ids.add(explicit)

  const heroFromUrl = extractMediaKeyFromUrl(state.hero.image, { prefix: "hospitals-page" })
  if (heroFromUrl?.startsWith("hospitals-page/")) ids.add(heroFromUrl)

  for (const cert of state.hero.certifications) {
    const imageId = extractMediaKeyFromUrl(cert.image, { prefix: "hospitals-page" })
    if (imageId?.startsWith("hospitals-page/")) ids.add(imageId)
    const logoId = extractMediaKeyFromUrl(cert.logo ?? "", { prefix: "hospitals-page" })
    if (logoId?.startsWith("hospitals-page/")) ids.add(logoId)
  }
  return ids
}

export async function GET() {
  const auth = await ensureAdmin()
  if (!auth.ok) return auth.res

  try {
    const rows = await prisma.hospitalsPageLocale.findMany({
      where: { entityKey: HOSPITALS_PAGE_ENTITY_KEY },
      orderBy: { locale: "asc" },
    })

    const locales: Partial<Record<HospitalsPageLocaleCode, HospitalsPageFormStateShape>> = {}
    for (const row of rows) {
      if (!HOSPITALS_PAGE_LOCALE_CODES.includes(row.locale as HospitalsPageLocaleCode)) continue
      locales[row.locale as HospitalsPageLocaleCode] = rowToFormState(row.contentJson)
    }

    return NextResponse.json(
      { success: true as const, locales: normalizeHospitalsPageLocaleImagesForStorage(locales) },
      { headers: NO_CACHE_HEADERS }
    )
  } catch (error) {
    console.error("[admin/hospitals-page] GET error:", error)
    return NextResponse.json(
      { success: false as const, error: "Failed to load hospitals page" },
      { status: 500, headers: NO_CACHE_HEADERS }
    )
  }
}

export async function PUT(req: Request) {
  const auth = await ensureAdmin()
  if (!auth.ok) return auth.res

  let body: unknown
  try {
    body = await req.json()
  } catch {
    return fail("Invalid JSON body")
  }

  const normalizedBody = normalizePersistedMediaInPayload(body)
  const mediaError = getPersistedMediaValidationError(normalizedBody)
  if (mediaError) return fail(mediaError)

  const parsed = parseHospitalsPagePutBody(normalizedBody)
  if (!parsed.ok) return fail(parsed.error)

  try {
    const existingRows = await prisma.hospitalsPageLocale.findMany({
      where: { entityKey: HOSPITALS_PAGE_ENTITY_KEY },
      select: { locale: true, contentJson: true },
    })

    const existingByLocale: Partial<Record<HospitalsPageLocaleCode, HospitalsPageFormStateShape>> = {}
    for (const row of existingRows) {
      if (!HOSPITALS_PAGE_LOCALE_CODES.includes(row.locale as HospitalsPageLocaleCode)) continue
      existingByLocale[row.locale as HospitalsPageLocaleCode] = rowToFormState(row.contentJson)
    }

    const localesToPersist =
      parsed.data.sectionKey
        ? buildMergedLocalesForSectionSave(existingByLocale, parsed.data.locales, parsed.data.sectionKey)
        : (parsed.data.locales as Record<HospitalsPageLocaleCode, HospitalsPageFormStateShape>)

    const normalizedLocales = normalizeHospitalsPageLocaleImagesForStorage(localesToPersist)

    const previousPublicIds = new Set<string>()
    for (const row of existingRows) {
      const state = rowToFormState(row.contentJson)
      for (const id of collectHospitalsPageImagePublicIds(state)) previousPublicIds.add(id)
    }

    const nextPublicIds = new Set<string>()
    for (const state of Object.values(normalizedLocales)) {
      if (!state) continue
      for (const id of collectHospitalsPageImagePublicIds(state)) nextPublicIds.add(id)
    }

    const localeNow = new Date()
    const ops = (Object.entries(normalizedLocales) as [HospitalsPageLocaleCode, HospitalsPageFormStateShape][]).map(
      ([locale, state]) =>
        prisma.hospitalsPageLocale.upsert({
          where: { entityKey_locale: { entityKey: HOSPITALS_PAGE_ENTITY_KEY, locale } },
          create: {
            id: randomUUID(),
            updatedAt: localeNow,
            entityKey: HOSPITALS_PAGE_ENTITY_KEY,
            locale,
            contentJson: JSON.stringify(state),
          },
          update: { ...touchUpdatedAt(), contentJson: JSON.stringify(state) },
        })
    )
    await prisma.$transaction(ops)

    const stalePublicIds = [...previousPublicIds].filter((id) => !nextPublicIds.has(id))
    if (stalePublicIds.length > 0) {
      await Promise.allSettled(stalePublicIds.map((id) => deleteImage(id)))
    }
  } catch (error) {
    console.error("[admin/hospitals-page] PUT error:", error)
    return NextResponse.json(
      { success: false as const, error: "Failed to save hospitals page" },
      { status: 500, headers: NO_CACHE_HEADERS }
    )
  }

  return NextResponse.json({ success: true as const }, { headers: NO_CACHE_HEADERS })
}
