import Link from 'next/link'
import { ArrowRight } from 'lucide-react'
import type { BannerText } from '@/lib/db/queries/getHeroBanners'

export type OverlaySize = 'hero' | 'large' | 'card'

interface BannerOverlayProps {
  text: BannerText
  linkUrl: string | null
  size: OverlaySize
  headingLevel?: 'h1' | 'h2' | 'h3'
}

const TITLE_SIZE: Record<OverlaySize, string> = {
  hero: 'text-3xl sm:text-5xl lg:text-6xl xl:text-7xl',
  large: 'text-2xl sm:text-4xl lg:text-5xl',
  card: 'text-xl sm:text-2xl',
}

const SUBTITLE_SIZE: Record<OverlaySize, string> = {
  hero: 'text-base sm:text-lg lg:text-xl',
  large: 'text-sm sm:text-base lg:text-lg',
  card: 'text-sm',
}

const PADDING: Record<OverlaySize, string> = {
  hero: 'p-6 sm:p-10 lg:p-16',
  large: 'p-6 sm:p-10 lg:p-12',
  card: 'p-5 sm:p-6',
}

const MAX_WIDTH: Record<OverlaySize, string> = {
  hero: 'max-w-2xl',
  large: 'max-w-xl',
  card: 'max-w-[85%]',
}

// The admin-entered text + CTA drawn over a banner image (banners.show_text).
// Position uses logical "start/end", so it flips automatically in RTL.
// The shade is a gradient toward the text side (a flat tint when centered)
// so the photo stays visible where there's no text.
export default function BannerOverlay({ text, linkUrl, size, headingLevel = 'h2' }: BannerOverlayProps) {
  const light = text.theme === 'light'
  const shade = light ? '0,0,0' : '255,255,255'
  const strong = Math.min(text.overlay + 0.25, 0.9)
  const Heading = headingLevel

  const shadeStyle =
    text.position === 'center'
      ? { background: `rgba(${shade},${text.overlay})` }
      : {
          // --dir flips with the document direction (see .banner-shade in globals.css)
          background: `linear-gradient(var(--banner-shade-dir, to right), rgba(${shade},${strong}) 0%, rgba(${shade},${text.overlay}) 45%, rgba(${shade},0) 85%)`,
        }

  const justify =
    text.position === 'center' ? 'justify-center' : text.position === 'end' ? 'justify-end' : 'justify-start'
  const align =
    text.position === 'center' ? 'items-center text-center' : text.position === 'end' ? 'items-end text-end' : 'items-start text-start'
  const shadeClass = text.position === 'end' ? 'banner-shade-end' : 'banner-shade-start'

  return (
    <>
      {/* Mobile: always a bottom-up shade (text sits at the bottom there). */}
      <div
        aria-hidden
        className="absolute inset-0 md:hidden"
        style={{ background: `linear-gradient(to top, rgba(${shade},${strong}) 0%, rgba(${shade},${text.overlay}) 50%, rgba(${shade},0.05) 100%)` }}
      />
      <div aria-hidden className={`absolute inset-0 hidden md:block ${shadeClass}`} style={shadeStyle} />

      <div className={`absolute inset-0 flex items-end md:items-center ${justify} ${PADDING[size]}`}>
        <div
          className={`flex flex-col gap-3 sm:gap-4 ${align} ${MAX_WIDTH[size]} ${light ? 'text-white' : 'text-gray-900'}`}
        >
          {text.eyebrow && (
            <span
              className={`inline-flex items-center gap-2 text-[11px] sm:text-xs font-semibold uppercase tracking-[0.25em] ${
                light ? 'text-white/85' : 'text-gray-700'
              }`}
            >
              <span className="h-px w-6 bg-current opacity-60" aria-hidden />
              {text.eyebrow}
            </span>
          )}
          <Heading
            className={`${TITLE_SIZE[size]} font-extrabold leading-[1.05] tracking-tight text-balance ${
              light ? 'drop-shadow-[0_2px_12px_rgba(0,0,0,0.25)]' : ''
            }`}
          >
            {text.title}
          </Heading>
          {text.subtitle && (
            <p className={`${SUBTITLE_SIZE[size]} leading-relaxed text-pretty ${light ? 'text-white/90' : 'text-gray-700'}`}>
              {text.subtitle}
            </p>
          )}
          {text.ctaLabel && linkUrl && (
            <Link
              href={linkUrl}
              className={`group/cta mt-1 sm:mt-2 inline-flex items-center gap-2 rounded-full font-semibold transition-all duration-300 bg-primary text-primary-foreground hover:gap-3 shadow-lg shadow-black/10 ${
                size === 'card' ? 'px-5 py-2.5 text-sm' : 'px-7 py-3.5 text-sm sm:text-base'
              }`}
            >
              {text.ctaLabel}
              <ArrowRight size={size === 'card' ? 16 : 18} className="rtl:rotate-180 transition-transform" />
            </Link>
          )}
        </div>
      </div>
    </>
  )
}
