export const PUBLIC_HOMEPAGE_GRID_COLUMNS = 12
export const PUBLIC_HOMEPAGE_GRID_ROWS = 12

export type PublicHomepageSectionType = 'hero' | 'offers' | 'vehicles' | 'pricing'

export type PublicHomepageLayoutItem = {
  id: string
  type: PublicHomepageSectionType
  x: number
  y: number
  w: number
  h: number
}

export type PublicHomepageLayout = {
  items: PublicHomepageLayoutItem[]
}

export const defaultPublicHomepageLayout: PublicHomepageLayout = {
  items: [
    { id: 'hero', type: 'hero', x: 1, y: 1, w: 7, h: 5 },
    { id: 'offers', type: 'offers', x: 8, y: 1, w: 5, h: 3 },
    { id: 'vehicles', type: 'vehicles', x: 1, y: 6, w: 7, h: 4 },
    { id: 'pricing', type: 'pricing', x: 8, y: 4, w: 5, h: 6 },
  ],
}

const SECTION_LIMITS: Record<PublicHomepageSectionType, { minW: number; minH: number; maxW: number; maxH: number }> = {
  hero: { minW: 4, minH: 4, maxW: 12, maxH: 8 },
  offers: { minW: 3, minH: 2, maxW: 12, maxH: 8 },
  vehicles: { minW: 4, minH: 3, maxW: 12, maxH: 8 },
  pricing: { minW: 4, minH: 4, maxW: 12, maxH: 9 },
}

function clamp(value: number, min: number, max: number) {
  return Math.min(Math.max(value, min), max)
}

export function clonePublicHomepageLayout(): PublicHomepageLayout {
  return JSON.parse(JSON.stringify(defaultPublicHomepageLayout)) as PublicHomepageLayout
}

export function getPublicHomepageSectionLimits(type: PublicHomepageSectionType) {
  return SECTION_LIMITS[type]
}

export function normalizePublicHomepageLayoutItem(item: PublicHomepageLayoutItem): PublicHomepageLayoutItem {
  const limits = SECTION_LIMITS[item.type]
  const w = clamp(Math.round(item.w), limits.minW, limits.maxW)
  const h = clamp(Math.round(item.h), limits.minH, limits.maxH)
  const x = clamp(Math.round(item.x), 1, PUBLIC_HOMEPAGE_GRID_COLUMNS - w + 1)
  const y = clamp(Math.round(item.y), 1, PUBLIC_HOMEPAGE_GRID_ROWS - h + 1)
  return {
    id: item.id,
    type: item.type,
    x,
    y,
    w,
    h,
  }
}

export function resolvePublicHomepageLayout(
  layout?: Partial<PublicHomepageLayout> | null,
  visibleTypes?: PublicHomepageSectionType[],
): PublicHomepageLayout {
  const defaultsByType = new Map(defaultPublicHomepageLayout.items.map((item) => [item.type, item] as const))
  const allowedTypes = new Set<PublicHomepageSectionType>(visibleTypes ?? (['hero', 'offers', 'vehicles', 'pricing'] as PublicHomepageSectionType[]))
  const items = (layout?.items ?? [])
    .filter((item): item is PublicHomepageLayoutItem => {
      if (!item || typeof item !== 'object') return false
      if (!('type' in item) || !allowedTypes.has(item.type as PublicHomepageSectionType)) return false
      return ['id', 'x', 'y', 'w', 'h'].every((key) => key in item)
    })
    .map((item) => normalizePublicHomepageLayoutItem(item))

  const seen = new Set(items.map((item) => item.type))
  for (const type of allowedTypes) {
    if (seen.has(type)) continue
    const fallback = defaultsByType.get(type)
    if (fallback) items.push(fallback)
  }

  return {
    items: items.sort((a, b) => (a.y - b.y) || (a.x - b.x)),
  }
}
