import React from 'react'
import type { FDCardGridBlock as FDCardGridBlockProps } from '@/payload-types'

const cardStyleMap: Record<
  string,
  { bg: string; headingText: string; bodyText: string; linkText: string; border: string }
> = {
  navy: {
    bg: 'bg-fd-navy',
    headingText: 'text-fd-yellow',
    bodyText: 'text-white',
    linkText: 'text-fd-yellow hover:text-fd-yellow/80',
    border: '',
  },
  gray: {
    bg: 'bg-[#e5e5e5] dark:bg-white/10',
    headingText: 'text-fd-navy dark:text-fd-yellow',
    bodyText: 'text-fd-navy dark:text-white',
    linkText: 'text-fd-navy hover:text-fd-navy/70 dark:text-fd-yellow dark:hover:text-fd-yellow/80',
    border: '',
  },
  yellow: {
    bg: 'bg-fd-yellow',
    headingText: 'text-fd-navy',
    bodyText: 'text-fd-navy',
    linkText: 'text-fd-navy hover:text-fd-navy/70',
    border: '',
  },
  green: {
    bg: 'bg-[#4ADE80]',
    headingText: 'text-fd-navy',
    bodyText: 'text-fd-navy',
    linkText: 'text-fd-navy hover:text-fd-navy/70',
    border: '',
  },
  outlined: {
    bg: 'bg-white dark:bg-white/10',
    headingText: 'text-fd-navy dark:text-fd-yellow',
    bodyText: 'text-fd-navy dark:text-white',
    linkText: 'text-fd-navy/70 hover:text-fd-navy dark:text-fd-yellow/80 dark:hover:text-fd-yellow',
    border: 'border-6 border-gray-200 dark:border-white/10 shadow-sm',
  },
}

const sectionBgMap: Record<string, string> = {
  white: 'bg-white dark:bg-fd-navy',
  navy: 'bg-fd-navy',
  gray: 'bg-fd-gray-light dark:bg-fd-navy',
}

const layoutGridMap: Record<string, string> = {
  '1-2': 'lg:grid-cols-[1fr_2fr]',
  '2-1': 'lg:grid-cols-[2fr_1fr]',
  '1-1-1': 'lg:grid-cols-3',
  '1-1': 'lg:grid-cols-2',
}

const styleClassMap: Record<string, string> = {
  normal: '',
  bold: 'font-joey-bold',
  italic: 'italic',
  boldItalic: 'font-joey-bold italic',
}

export const FDCardGridBlockComponent: React.FC<FDCardGridBlockProps> = ({
  layout = '1-1-1',
  cardStyle = 'outlined',
  cards,
  sectionBackground = 'white',
}) => {
  const style = cardStyleMap[cardStyle] || cardStyleMap.outlined
  const sectionBg = sectionBgMap[sectionBackground || 'white']
  const gridCols = layoutGridMap[layout] || layoutGridMap['1-1-1']

  return (
    <section className={`w-full py-12 md:py-16 lg:py-20 ${sectionBg}`}>
      <div className="max-w-[1200px] mx-auto px-6 md:px-8">
        <div className={`grid grid-cols-1 md:grid-cols-2 ${gridCols} gap-4 md:gap-6`}>
          {cards?.map((card, index) => {
            const mode = card.displayMode || 'content'

            let cardContent: React.ReactNode

            if (mode === 'centeredHeading') {
              cardContent = (
                <div
                  className={`${style.bg} ${style.border} rounded-[70px] px-10 md:px-14 py-14 md:py-20 flex items-center justify-center min-h-[280px] md:min-h-[360px]`}
                >
                  <span
                    className={`font-joey-heavy text-fd-display leading-tight text-center ${style.headingText}`}
                  >
                    {card.heading}
                  </span>
                </div>
              )
            } else if (mode === 'centeredBody') {
              cardContent = (
                <div
                  className={`${style.bg} ${style.border} rounded-[70px] px-10 md:px-14 py-14 md:py-20 flex items-center justify-center min-h-[280px] md:min-h-[360px]`}
                >
                  <p
                    className={`font-joey text-fd-body-lg text-center ${style.bodyText}`}
                  >
                    {card.centeredBodyText}
                  </p>
                </div>
              )
            } else {
              cardContent = (
                <div
                  className={`${style.bg} ${style.border} rounded-[70px] px-10 md:px-14 py-10 md:py-14 flex flex-col gap-1 h-full`}
                >
                  {card.heading && (
                    <h3
                      className={`font-joey-heavy text-fd-h2 mb-2 ${style.headingText}`}
                    >
                      {card.heading}
                    </h3>
                  )}

                  {card.contentLines?.map((line, lineIndex) => {
                    const textStyle = styleClassMap[line.style || 'normal'] || ''
                    const baseClass = `font-joey text-fd-body-lg ${style.bodyText} ${textStyle}`

                    if (line.link) {
                      return (
                        <a
                          key={lineIndex}
                          href={line.link}
                          className={`${baseClass} ${style.linkText} underline underline-offset-2 transition-colors`}
                        >
                          {line.text}
                        </a>
                      )
                    }

                    return (
                      <p key={lineIndex} className={baseClass}>
                        {line.text}
                      </p>
                    )
                  })}
                </div>
              )
            }

            if (card.cardLink) {
              return (
                <a
                  key={index}
                  href={card.cardLink}
                  className="block hover:opacity-90 transition-opacity"
                >
                  {cardContent}
                </a>
              )
            }
            return <div key={index}>{cardContent}</div>
          })}
        </div>
      </div>
    </section>
  )
}
