// src/components/resume/templates/CompactATS.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 CompactATS({ data }: Props) {
  const settings = parseDesignSettings(data)
  const accent = '#000000' // Keep ATS strictly dark/black for best parsing
  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-black"
      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 – plain text, no decorations */}
      <div style={{ textAlign: 'center', marginBottom: '12px', borderBottom: '1px solid #000', paddingBottom: '10px' }}>
        {renderPhoto(data, settings, accent)}
        <h1
          style={{
            fontFamily: settings.fontFamily ? `"${settings.fontFamily}", sans-serif` : 'Arial, Helvetica, sans-serif',
            fontSize: nameSize,
            fontWeight: settings.nameBold ? 'bold' : 'normal',
            margin: '0 0 4px 0',
            textTransform: 'uppercase',
            letterSpacing: '0.05em'
          }}
        >
          {data.fullName || 'YOUR NAME'}
        </h1>
        {data.professionalTitle && (
          <p style={{ fontSize: '10pt', fontWeight: 'bold', margin: '2px 0 6px 0', textTransform: 'uppercase' }}>
            {data.professionalTitle}
          </p>
        )}
        <div style={{ fontSize: '9pt', color: '#333', display: 'flex', justifyContent: 'center' }}>
          {renderContactDetails(data, settings, accent, false)}
        </div>
      </div>

      <div style={{ flexGrow: 1, display: 'flex', flexDirection: 'column', gap: '14px' }}>
        {/* Summary */}
        {data.summary && (
          <ATSSection title="PROFESSIONAL SUMMARY">
            <p style={{ margin: 0, whiteSpace: 'pre-line' }}>{data.summary}</p>
          </ATSSection>
        )}

        {/* Experience */}
        {data.experiences.length > 0 && (
          <ATSSection title="WORK EXPERIENCE">
            {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: '#000' }}>
                    {exp.jobTitle} — {exp.company}
                    {exp.location ? `, ${exp.location}` : ''}
                  </strong>
                  <span style={{ fontSize: '9pt', color: '#555', whiteSpace: 'nowrap' }}>
                    {exp.startDate} – {exp.current ? 'Present' : exp.endDate}
                  </span>
                </div>
                {exp.description && (
                  <p
                    style={{
                      marginTop: '3px',
                      color: '#333',
                      listStyleType: listStyleType,
                      paddingLeft: settings.listStyle === 'bullet' ? '12px' : 0
                    }}
                  >
                    {exp.description}
                  </p>
                )}
              </div>
            ))}
          </ATSSection>
        )}

        {/* Education */}
        {data.education.length > 0 && (
          <ATSSection title="EDUCATION">
            {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: '#000' }}>
                    {edu.degree} — {edu.institution}
                    {edu.location ? `, ${edu.location}` : ''}
                  </strong>
                  <span style={{ fontSize: '9pt', color: '#555', whiteSpace: 'nowrap' }}>
                    {edu.startDate} – {edu.endDate}
                  </span>
                </div>
                {edu.description && <p style={{ marginTop: '3px', color: '#333' }}>{edu.description}</p>}
              </div>
            ))}
          </ATSSection>
        )}

        {/* Skills */}
        {data.skills.length > 0 && (
          <ATSSection title="SKILLS">
            <p style={{ margin: 0 }}>
              {data.skills.map((s) => s.name + (s.level ? ` (${s.level})` : '')).join(', ')}
            </p>
          </ATSSection>
        )}

        {/* Projects */}
        {data.projects.length > 0 && (
          <ATSSection title="PROJECTS">
            {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: '#000' }}>{proj.name}</strong>
                  {proj.url && renderLink(proj.url, settings, '#000')}
                </div>
                {proj.description && <p style={{ marginTop: '2px', color: '#333' }}>{proj.description}</p>}
              </div>
            ))}
          </ATSSection>
        )}
      </div>

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

function ATSSection({ title, children }: { title: string; children: React.ReactNode }) {
  return (
    <div style={{ marginBottom: '12px' }}>
      <h2
        style={{
          fontFamily: 'Arial, sans-serif',
          fontSize: '10pt',
          fontWeight: 'bold',
          textTransform: 'uppercase',
          letterSpacing: '0.04em',
          borderBottom: '1px solid #000',
          paddingBottom: '2px',
          marginBottom: '6px'
        }}
      >
        {title}
      </h2>
      {children}
    </div>
  )
}
