// src/components/resume/templates/GraduateSimple.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 GraduateSimple({ data }: Props) {
  const settings = parseDesignSettings(data)
  const accent = data.accentColor || '#059669'
  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',
        ...fontStyle
      }}
    >
      <style dangerouslySetInnerHTML={{ __html: fontImport }} />

      {/* Header – simple top strip */}
      <div style={{ background: `${accent}12`, borderRadius: '8px', padding: '16px 20px', marginBottom: '20px', borderLeft: `4px solid ${accent}` }}>
        {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',
            margin: '0 0 6px 0'
          }}
        >
          {data.fullName || 'Your Name'}
        </h1>
        {data.professionalTitle && (
          <p style={{ fontSize: '10.5pt', fontWeight: 600, color: accent, marginTop: '2px', marginBottom: '6px' }}>
            {data.professionalTitle}
          </p>
        )}
        <div style={{ display: 'flex', flexWrap: 'wrap', gap: '10px', fontSize: '8.5pt', color: '#52525b' }}>
          {renderContactDetails(data, settings, accent, false)}
        </div>
      </div>

      <div style={{ flexGrow: 1, display: 'flex', flexDirection: 'column', gap: '16px' }}>
        {/* Summary */}
        {data.summary && (
          <GradSection title="About Me" accent={accent} settings={settings}>
            <p style={{ margin: 0, color: '#52525b', whiteSpace: 'pre-line' }}>{data.summary}</p>
          </GradSection>
        )}

        {/* Education first — for graduates */}
        {data.education.length > 0 && (
          <GradSection title="Education" accent={accent} settings={settings}>
            {data.education.map((edu, i) => (
              <div
                key={i}
                style={{
                  marginBottom: i < data.education.length - 1 ? settings.entrySpacing : 0,
                  display: 'flex',
                  gap: '12px',
                  paddingLeft: settings.indentBody ? '12px' : 0
                }}
              >
                <div style={{ width: '3px', background: `${accent}40`, borderRadius: '2px', flexShrink: 0 }} />
                <div>
                  <strong style={{ ...subStyle, color: '#18181b', display: 'block' }}>{edu.degree}</strong>
                  <span style={{ color: accent, fontWeight: 600, fontSize: '9pt' }}>{edu.institution}{edu.location ? ` · ${edu.location}` : ''}</span>
                  <span style={{ color: '#71717a', fontSize: '8.5pt', marginLeft: '8px' }}>{edu.startDate} – {edu.endDate}</span>
                  {edu.description && <p style={{ marginTop: '3px', color: '#52525b', fontSize: '8.5pt' }}>{edu.description}</p>}
                </div>
              </div>
            ))}
          </GradSection>
        )}

        {/* Experience */}
        {data.experiences.length > 0 && (
          <GradSection title="Experience" accent={accent} settings={settings}>
            {data.experiences.map((exp, i) => (
              <div
                key={i}
                style={{
                  marginBottom: i < data.experiences.length - 1 ? settings.entrySpacing : 0,
                  display: 'flex',
                  gap: '12px',
                  paddingLeft: settings.indentBody ? '12px' : 0
                }}
              >
                <div style={{ width: '3px', background: `${accent}40`, borderRadius: '2px', flexShrink: 0 }} />
                <div>
                  <strong style={{ ...subStyle, color: '#18181b', display: 'block' }}>{exp.jobTitle}</strong>
                  <span style={{ color: accent, fontWeight: 600, fontSize: '9pt' }}>{exp.company}</span>
                  <span style={{ color: '#71717a', fontSize: '8.5pt', marginLeft: '8px' }}>
                    {exp.startDate} – {exp.current ? 'Present' : exp.endDate}
                    {exp.location ? ` · ${exp.location}` : ''}
                  </span>
                  {exp.description && (
                    <p
                      style={{
                        marginTop: '3px',
                        color: '#52525b',
                        fontSize: '8.5pt',
                        listStyleType: listStyleType,
                        paddingLeft: settings.listStyle === 'bullet' ? '12px' : 0
                      }}
                    >
                      {exp.description}
                    </p>
                  )}
                </div>
              </div>
            ))}
          </GradSection>
        )}

        {/* Skills – tag chips */}
        {data.skills.length > 0 && (
          <GradSection title="Skills" accent={accent} settings={settings}>
            <div style={{ display: 'flex', flexWrap: 'wrap', gap: '6px' }}>
              {data.skills.map((skill, i) => (
                <span key={i} style={{ background: `${accent}15`, border: `1px solid ${accent}40`, borderRadius: '99px', padding: '3px 10px', fontSize: '8.5pt', color: '#1a1a1a', fontWeight: 500 }}>
                  {skill.name}{skill.level ? ` · ${skill.level}` : ''}
                </span>
              ))}
            </div>
          </GradSection>
        )}

        {/* Projects */}
        {data.projects.length > 0 && (
          <GradSection 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: '8.5pt' }}>{proj.description}</p>}
              </div>
            ))}
          </GradSection>
        )}
      </div>

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

function GradSection({ title, accent, settings, children }: { title: string; accent: string; settings: any; children: React.ReactNode }) {
  return (
    <div style={{ marginBottom: '16px' }}>
      <h2
        style={{
          fontFamily: settings.fontFamily ? `"${settings.fontFamily}", sans-serif` : 'Space Grotesk, sans-serif',
          fontSize: '10pt',
          fontWeight: 700,
          color: accent,
          marginBottom: '8px',
          display: 'flex',
          alignItems: 'center',
          gap: '8px'
        }}
      >
        <span style={{ display: 'block', width: '20px', height: '2px', background: accent }} />
        {title}
      </h2>
      {children}
    </div>
  )
}
