'use client'

import { useEffect } from 'react'
import { Language, PitchData, FMResult, FMAssumption } from '@/lib/types'
import { PrintCoverPage, PrintProblemPage, PrintSolutionPage, PrintProductPage, PrintMarketPage, PrintTeamPage, PrintMilestonesPage, PrintTheAskPage } from './PrintCoreSlides'
import { PrintFinancialsPage, PrintAssumptionsPage, PrintCapTablePage, PrintDisclaimerPage, aggregateAnnualRows } from './PrintFinancialSlides'

interface PrintDeckProps {
  pitchData: PitchData
  versionName: string
  fmResults: FMResult[]
  fmAssumptions: FMAssumption[]
  financial: boolean
  lang: Language
}

export default function PrintDeck({ pitchData, versionName, fmResults, fmAssumptions, financial, lang }: PrintDeckProps) {
  const isWandeldarlehen = (pitchData.funding?.instrument || '').toLowerCase() === 'wandeldarlehen'
  const hasCapTable = financial && !isWandeldarlehen
  const totalPages = financial ? (hasCapTable ? 13 : 12) : 9
  const annualRows = aggregateAnnualRows(fmResults)
  const de = lang === 'de'

  useEffect(() => {
    const t = setTimeout(() => window.print(), 900)
    return () => clearTimeout(t)
  }, [])

  function p(n: number) { return { lang, pageNum: n, totalPages, versionName } }

  return (
    <>
      <style>{`
        @media print {
          @page { size: A4 landscape; margin: 0; }
          body { margin: 0; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
          .no-print { display: none !important; }
          .print-page {
            page-break-after: always !important;
            break-after: page !important;
            page-break-inside: avoid !important;
            break-inside: avoid !important;
            margin: 0 !important;
            box-shadow: none !important;
            width: 297mm !important;
            height: 210mm !important;
          }
          .print-page-cover {
            page-break-after: always !important;
            break-after: page !important;
            margin: 0 !important;
            box-shadow: none !important;
          }
          .print-deck-wrapper { padding: 0 !important; }
        }
        @media screen {
          body { background: #d1d5db; }
        }
        * { box-sizing: border-box; }
      `}</style>

      {/* Toolbar — screen only */}
      <div className="no-print" style={{
        position: 'sticky', top: 0, zIndex: 100,
        background: '#1e1b4b', color: '#fff',
        padding: '10px 24px',
        display: 'flex', alignItems: 'center', justifyContent: 'space-between',
        fontFamily: 'system-ui, -apple-system, sans-serif',
        fontSize: '13px', boxShadow: '0 2px 8px rgba(0,0,0,0.3)',
      }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: '16px' }}>
          <span style={{ fontWeight: 700 }}>BreakPilot</span>
          <span style={{ opacity: 0.6, fontSize: '11px' }}>{versionName}</span>
          <span style={{ fontSize: '11px', padding: '2px 8px', borderRadius: '99px', background: financial ? '#7c3aed22' : '#6366f122', color: financial ? '#a78bfa' : '#818cf8' }}>
            {financial ? (de ? 'PDF + Finanzen' : 'PDF + Financial') : 'Standard PDF'}
          </span>
          <span style={{ fontSize: '11px', opacity: 0.5 }}>{totalPages} {de ? 'Seiten' : 'pages'}</span>
        </div>
        <div style={{ display: 'flex', gap: '10px' }}>
          <button onClick={() => window.print()} style={{ padding: '6px 16px', background: '#6366f1', color: '#fff', border: 'none', borderRadius: '6px', cursor: 'pointer', fontWeight: 600, fontSize: '13px' }}>
            {de ? 'Drucken / Als PDF speichern' : 'Print / Save as PDF'}
          </button>
          <button onClick={() => window.close()} style={{ padding: '6px 12px', background: 'rgba(255,255,255,0.1)', color: '#fff', border: '1px solid rgba(255,255,255,0.2)', borderRadius: '6px', cursor: 'pointer', fontSize: '13px' }}>
            {de ? 'Schließen' : 'Close'}
          </button>
        </div>
      </div>

      <div className="print-deck-wrapper" style={{ padding: '32px 0' }}>
        <PrintCoverPage company={pitchData.company} funding={pitchData.funding} versionName={versionName} lang={lang} />
        <PrintProblemPage {...p(2)} />
        <PrintSolutionPage {...p(3)} />
        <PrintProductPage products={pitchData.products || []} {...p(4)} />
        <PrintMarketPage market={pitchData.market || []} {...p(5)} />
        <PrintTeamPage team={pitchData.team || []} {...p(6)} />
        <PrintMilestonesPage milestones={pitchData.milestones || []} {...p(7)} />
        <PrintTheAskPage funding={pitchData.funding} {...p(8)} />

        {/* Page 9: standard last page OR financial annex start */}
        {!financial && <PrintDisclaimerPage {...p(9)} />}

        {financial && (
          <>
            {annualRows.length > 0
              ? <PrintFinancialsPage annualRows={annualRows} {...p(9)} />
              : <PrintDisclaimerPage {...p(9)} />
            }
            <PrintAssumptionsPage assumptions={fmAssumptions} {...p(10)} />
            {hasCapTable && <PrintCapTablePage {...p(11)} />}
            <PrintDisclaimerPage {...p(hasCapTable ? 12 : 11)} />
          </>
        )}
      </div>
    </>
  )
}
