// src/components/resume/templates/ElegantMinimalist.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 ElegantMinimalist({ data }: Props) {
  const settings = parseDesignSettings(data)
  const accent = data.accentColor || '#0d9488'
  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',
        padding: `${settings.marginTB} ${settings.marginLR}`,
        fontSize: settings.fontSize,
        lineHeight: settings.lineHeight,
        display: 'flex',
        flexDirection: 'column',
        borderTop: `4px solid ${accent}`, // Subtle top accent line
        ...fontStyle
      }}
    >
      <style dangerouslySetInnerHTML={{ __html: fontImport }} />

      {/* Header - Centered */}
      <div style={{ textAlign: 'center', marginBottom: '24px', borderBottom: '1px double #e5e7eb', paddingBottom: '16px' }}>
        {renderPhoto(data, settings, accent)}
        <h1
          style={{
            fontFamily: settings.fontFamily ? `"${settings.fontFamily}", serif` : 'Space Grotesk, sans-serif',
            fontSize: nameSize,
            fontWeight: settings.nameBold ? 700 : 400,
            color: '#111827',
            margin: '0 0 4px 0',
            letterSpacing: '0.02em',
            textTransform: 'uppercase'
          }}
        >
          {data.fullName || 'Your Name'}
        </h1>
        {data.professionalTitle && (
          <p style={{ fontSize: '10.5pt', fontWeight: 600, color: accent, marginTop: '2px', marginBottom: '8px', letterSpacing: '0.05em', textTransform: 'uppercase' }}>
            {data.professionalTitle}
          </p>
        )}
        <div style={{ display: 'flex', justifyContent: 'center', fontSize: '9pt', color: '#52525b' }}>
          {renderContactDetails(data, settings, accent, false)}
        </div>
      </div>

      <div style={{ flexGrow: 1, display: 'flex', flexDirection: 'column', gap: '18px' }}>
        {/* Summary */}
        {data.summary && (
          <MinimalSection title="Profile Statement" accent={accent} settings={settings}>
            <p style={{ color: '#4b5563', margin: 0, textAlign: 'justify', whiteSpace: 'pre-line' }}>{data.summary}</p>
          </MinimalSection>
        )}

        {/* Experience */}
        {data.experiences.length > 0 && (
          <MinimalSection title="Professional 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: '#111827' }}>{exp.jobTitle}</strong>
                  <span style={{ fontSize: '8.5pt', color: '#6b7280', whiteSpace: 'nowrap' }}>
                    {exp.startDate} – {exp.current ? 'Present' : exp.endDate || ''}
                  </span>
                </div>
                <div style={{ color: accent, fontSize: '9pt', fontWeight: 600, fontStyle: 'italic' }}>
                  {exp.company}{exp.location ? `, ${exp.location}` : ''}
                </div>
                {exp.description && (
                  <p
                    style={{
                      marginTop: '4px',
                      color: '#4b5563',
                      fontSize: '9pt',
                      listStyleType: listStyleType,
                      paddingLeft: settings.listStyle === 'bullet' ? '12px' : 0
                    }}
                  >
                    {exp.description}
                  </p>
                )}
              </div>
            ))}
          </MinimalSection>
        )}

        {/* Education */}
        {data.education.length > 0 && (
          <MinimalSection 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: '#111827' }}>{edu.degree}</strong>
                  <span style={{ fontSize: '8.5pt', color: '#6b7280', whiteSpace: 'nowrap' }}>
                    {edu.startDate} – {edu.endDate || ''}
                  </span>
                </div>
                <div style={{ color: accent, fontSize: '9pt', fontWeight: 600, fontStyle: 'italic' }}>
                  {edu.institution}{edu.location ? `, ${edu.location}` : ''}
                </div>
                {edu.description && <p style={{ marginTop: '4px', color: '#4b5563', fontSize: '9pt' }}>{edu.description}</p>}
              </div>
            ))}
          </MinimalSection>
        )}

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

        {/* Projects */}
        {data.projects.length > 0 && (
          <MinimalSection title="Key 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: '#111827' }}>{proj.name}</strong>
                  {proj.url && renderLink(proj.url, settings, accent)}
                </div>
                {proj.description && <p style={{ marginTop: '3px', color: '#4b5563', fontSize: '9pt' }}>{proj.description}</p>}
              </div>
            ))}
          </MinimalSection>
        )}
      </div>

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

function MinimalSection({ 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}", serif` : 'Space Grotesk, sans-serif',
          fontSize: '10pt',
          fontWeight: 600,
          textTransform: 'uppercase',
          letterSpacing: '0.1em',
          color: accent,
          borderBottom: '1px solid #e5e7eb',
          paddingBottom: '4px',
          marginBottom: '10px'
        }}
      >
        {title}
      </h2>
      {children}
    </div>
  )
}
