// src/components/resume/templates/CreativeAccent.tsx
import React from 'react'
import type { ResumeData } from '@/types'
import {
  parseDesignSettings,
  getFontImportsAndStyle,
  getNameSize,
  getSubtitleStyle,
  renderContactDetails,
  renderPhoto,
  renderFooter,
  renderLink
} from './templateHelper'

interface Props {
  data: ResumeData
}

export function CreativeAccent({ data }: Props) {
  const settings = parseDesignSettings(data)
  const accent = data.accentColor || '#e11d48'
  const { fontImport, fontStyle } = getFontImportsAndStyle(settings)
  const nameSize = getNameSize(settings)
  const subStyle = getSubtitleStyle(settings, accent)

  const listStyleType = settings.listStyle === 'bullet' ? 'disc' : 'none'

  return (
    <div
      className="print-page bg-white text-neutral-800"
      style={{
        width: '210mm',
        minHeight: '297mm',
        fontSize: settings.fontSize,
        lineHeight: settings.lineHeight,
        display: 'flex',
        flexDirection: 'column',
        ...fontStyle
      }}
    >
      <style dangerouslySetInnerHTML={{ __html: fontImport }} />

      {/* Bold accent header band - Padding is defined by margins horizontally */}
      <div style={{ background: accent, padding: `20px ${settings.marginLR} 16px`, color: '#ffffff' }}>
        {renderPhoto(data, settings, '#ffffff')}
        <h1
          style={{
            fontFamily: settings.fontFamily ? `"${settings.fontFamily}", sans-serif` : 'Space Grotesk, sans-serif',
            fontSize: nameSize,
            fontWeight: settings.nameBold ? 700 : 400,
            margin: 0,
            color: '#ffffff',
            letterSpacing: '-0.02em'
          }}
        >
          {data.fullName || 'Your Name'}
        </h1>
        {data.professionalTitle && (
          <p style={{ fontSize: '11pt', fontWeight: 600, color: 'rgba(255,255,255,0.9)', marginTop: '4px', marginBottom: '8px' }}>
            {data.professionalTitle}
          </p>
        )}
        <div style={{ marginTop: '8px', fontSize: '8.5pt', color: '#ffffff' }}>
          {renderContactDetails(data, settings, accent, true)}
        </div>
      </div>

      {/* Body with padding */}
      <div style={{ padding: `${settings.marginTB} ${settings.marginLR}`, flexGrow: 1, display: 'flex', flexDirection: 'column', gap: '16px' }}>
        {/* Summary */}
        {data.summary && (
          <div style={{ margin: '8px 0', padding: '12px 14px', borderLeft: `4px solid ${accent}`, background: `${accent}0a` }}>
            <p style={{ margin: 0, color: '#3f3f46', fontStyle: 'italic', whiteSpace: 'pre-line' }}>{data.summary}</p>
          </div>
        )}

        <div style={{ display: 'grid', gridTemplateColumns: settings.columns === 'two' ? '1fr 1fr' : '1fr', gap: '0 24px' }}>
          {/* Column/Content block */}
          <div style={{ display: 'flex', flexDirection: 'column', gap: '16px' }}>
            {data.experiences.length > 0 && (
              <CreativeSection title="Experience" accent={accent} settings={settings}>
                {data.experiences.map((exp, i) => (
                  <div
                    key={i}
                    style={{
                      marginBottom: i < data.experiences.length - 1 ? settings.entrySpacing : 0,
                      position: 'relative',
                      paddingLeft: '12px',
                      marginLeft: settings.indentBody ? '12px' : 0
                    }}
                  >
                    <div style={{ position: 'absolute', left: 0, top: '5px', width: '6px', height: '6px', borderRadius: '50%', background: accent }} />
                    <strong style={{ ...subStyle, color: '#18181b', display: 'block' }}>{exp.jobTitle}</strong>
                    <span style={{ color: accent, fontSize: '9pt', fontWeight: 600 }}>{exp.company}</span>
                    <span style={{ color: '#71717a', fontSize: '8pt', display: 'block' }}>
                      {exp.startDate} – {exp.current ? 'Present' : exp.endDate}{exp.location ? ` · ${exp.location}` : ''}
                    </span>
                    {exp.description && (
                      <p
                        style={{
                          marginTop: '4px',
                          color: '#52525b',
                          fontSize: '8.5pt',
                          listStyleType: listStyleType,
                          paddingLeft: settings.listStyle === 'bullet' ? '12px' : 0
                        }}
                      >
                        {exp.description}
                      </p>
                    )}
                  </div>
                ))}
              </CreativeSection>
            )}

            {data.projects.length > 0 && (
              <CreativeSection title="Projects" accent={accent} settings={settings}>
                {data.projects.map((proj, i) => (
                  <div
                    key={i}
                    style={{
                      marginBottom: i < data.projects.length - 1 ? settings.entrySpacing : 0,
                      paddingLeft: '12px',
                      position: 'relative',
                      marginLeft: settings.indentBody ? '12px' : 0
                    }}
                  >
                    <div style={{ position: 'absolute', left: 0, top: '5px', width: '6px', height: '6px', borderRadius: '50%', background: accent }} />
                    <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline' }}>
                      <strong style={{ ...subStyle, color: '#18181b' }}>{proj.name}</strong>
                      {proj.url && renderLink(proj.url, settings, accent)}
                    </div>
                    {proj.description && <p style={{ marginTop: '3px', color: '#52525b', fontSize: '8.5pt' }}>{proj.description}</p>}
                  </div>
                ))}
              </CreativeSection>
            )}
          </div>

          {/* Right Column (merged if single column) */}
          <div style={{ display: 'flex', flexDirection: 'column', gap: '16px', marginTop: settings.columns === 'two' ? 0 : '16px' }}>
            {data.education.length > 0 && (
              <CreativeSection title="Education" accent={accent} settings={settings}>
                {data.education.map((edu, i) => (
                  <div
                    key={i}
                    style={{
                      marginBottom: i < data.education.length - 1 ? settings.entrySpacing : 0,
                      paddingLeft: '12px',
                      position: 'relative',
                      marginLeft: settings.indentBody ? '12px' : 0
                    }}
                  >
                    <div style={{ position: 'absolute', left: 0, top: '5px', width: '6px', height: '6px', borderRadius: '50%', background: accent }} />
                    <strong style={{ ...subStyle, color: '#18181b', display: 'block' }}>{edu.degree}</strong>
                    <span style={{ color: accent, fontSize: '9pt', fontWeight: 600 }}>{edu.institution}</span>
                    <span style={{ color: '#71717a', fontSize: '8pt', display: 'block' }}>{edu.startDate} – {edu.endDate}</span>
                    {edu.description && <p style={{ marginTop: '4px', color: '#52525b', fontSize: '8.5pt' }}>{edu.description}</p>}
                  </div>
                ))}
              </CreativeSection>
            )}

            {data.skills.length > 0 && (
              <CreativeSection title="Skills" accent={accent} settings={settings}>
                <div style={{ display: 'flex', flexWrap: 'wrap', gap: '5px' }}>
                  {data.skills.map((skill, i) => (
                    <span key={i} style={{ background: accent, color: '#ffffff', borderRadius: '99px', padding: '2px 10px', fontSize: '8pt', fontWeight: 600 }}>
                      {skill.name}{skill.level ? ` · ${skill.level}` : ''}
                    </span>
                  ))}
                </div>
              </CreativeSection>
            )}
          </div>
        </div>
      </div>

      {renderFooter(data, settings)}
    </div>
  )
}

function CreativeSection({ title, accent, settings, children }: { title: string; accent: string; settings: any; children: React.ReactNode }) {
  return (
    <div style={{ marginBottom: '14px' }}>
      <h2
        style={{
          fontFamily: settings.fontFamily ? `"${settings.fontFamily}", sans-serif` : 'Space Grotesk, sans-serif',
          fontSize: '10.5pt',
          fontWeight: 700,
          color: '#18181b',
          marginBottom: '10px',
          paddingBottom: '4px',
          borderBottom: `2px solid ${accent}`
        }}
      >
        {title}
      </h2>
      {children}
    </div>
  )
}
