// src/components/resume/templates/CleanProfessional.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 CleanProfessional({ data }: Props) {
  const settings = parseDesignSettings(data)
  const accent = data.accentColor || '#2b2ca8'
  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-900"
      style={{
        width: '210mm',
        minHeight: '297mm',
        padding: `${settings.marginTB} ${settings.marginLR}`,
        fontSize: settings.fontSize,
        lineHeight: settings.lineHeight,
        display: 'flex',
        flexDirection: 'column',
        ...fontStyle
      }}
    >
      <style dangerouslySetInnerHTML={{ __html: fontImport }} />

      {/* Header */}
      <div style={{ borderBottom: `2.5px solid ${accent}`, paddingBottom: '12px', marginBottom: '20px' }}>
        {renderPhoto(data, settings, accent)}
        <h1
          style={{
            fontFamily: settings.fontFamily ? `"${settings.fontFamily}", sans-serif` : 'Space Grotesk, sans-serif',
            fontSize: nameSize,
            fontWeight: settings.nameBold ? 700 : 400,
            color: '#18181b',
            letterSpacing: '-0.02em',
            margin: 0
          }}
        >
          {data.fullName || 'Your Name'}
        </h1>
        {data.professionalTitle && (
          <p style={{ fontSize: '11pt', fontWeight: 600, color: accent, marginTop: '4px', marginBottom: '8px' }}>
            {data.professionalTitle}
          </p>
        )}
        <div style={{ marginTop: '6px', fontSize: '9pt', color: '#52525b' }}>
          {renderContactDetails(data, settings, accent, false)}
        </div>
      </div>

      <div style={{ flexGrow: 1, display: 'flex', flexDirection: 'column', gap: '18px' }}>
        {/* Summary */}
        {data.summary && (
          <Section title="Professional Summary" accent={accent} settings={settings}>
            <p style={{ color: '#3f3f46', margin: 0, whiteSpace: 'pre-line' }}>{data.summary}</p>
          </Section>
        )}

        {/* Experience */}
        {data.experiences.length > 0 && (
          <Section title="Work Experience" accent={accent} settings={settings}>
            {data.experiences.map((exp, i) => (
              <div
                key={i}
                style={{
                  marginBottom: i < data.experiences.length - 1 ? settings.entrySpacing : 0,
                  paddingLeft: settings.indentBody ? '12px' : 0
                }}
              >
                <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline' }}>
                  <strong style={{ ...subStyle, color: '#18181b' }}>{exp.jobTitle}</strong>
                  <span style={{ fontSize: '8.5pt', color: '#71717a', whiteSpace: 'nowrap' }}>
                    {exp.startDate} – {exp.current ? 'Present' : exp.endDate || ''}
                  </span>
                </div>
                <div style={{ color: accent, fontSize: '9.5pt', fontWeight: 600 }}>
                  {exp.company}{exp.location ? `, ${exp.location}` : ''}
                </div>
                {exp.description && (
                  <p
                    style={{
                      marginTop: '4px',
                      color: '#52525b',
                      fontSize: '9pt',
                      listStyleType: listStyleType,
                      paddingLeft: settings.listStyle === 'bullet' ? '12px' : 0
                    }}
                  >
                    {exp.description}
                  </p>
                )}
              </div>
            ))}
          </Section>
        )}

        {/* Education */}
        {data.education.length > 0 && (
          <Section 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: settings.indentBody ? '12px' : 0
                }}
              >
                <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline' }}>
                  <strong style={{ ...subStyle, color: '#18181b' }}>{edu.degree}</strong>
                  <span style={{ fontSize: '8.5pt', color: '#71717a', whiteSpace: 'nowrap' }}>
                    {edu.startDate} – {edu.endDate || ''}
                  </span>
                </div>
                <div style={{ color: accent, fontSize: '9.5pt', fontWeight: 600 }}>
                  {edu.institution}{edu.location ? `, ${edu.location}` : ''}
                </div>
                {edu.description && <p style={{ marginTop: '4px', color: '#52525b', fontSize: '9pt' }}>{edu.description}</p>}
              </div>
            ))}
          </Section>
        )}

        {/* Skills */}
        {data.skills.length > 0 && (
          <Section title="Skills" accent={accent} settings={settings}>
            <div style={{ display: 'flex', flexWrap: 'wrap', gap: '6px' }}>
              {data.skills.map((skill, i) => (
                <span
                  key={i}
                  style={{
                    background: '#f4f4f5',
                    border: '1px solid #e4e4e7',
                    borderRadius: '4px',
                    padding: '2px 8px',
                    fontSize: '8.5pt',
                    color: '#3f3f46'
                  }}
                >
                  {skill.name}{skill.level ? ` · ${skill.level}` : ''}
                </span>
              ))}
            </div>
          </Section>
        )}

        {/* Projects */}
        {data.projects.length > 0 && (
          <Section 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: settings.indentBody ? '12px' : 0
                }}
              >
                <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: '9pt' }}>{proj.description}</p>}
              </div>
            ))}
          </Section>
        )}
      </div>

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

function Section({ 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: '10pt',
          fontWeight: 700,
          textTransform: 'uppercase',
          letterSpacing: '0.08em',
          color: accent,
          borderBottom: `1px solid #e4e4e7`,
          paddingBottom: '4px',
          marginBottom: '10px'
        }}
      >
        {title}
      </h2>
      {children}
    </div>
  )
}
