'use client';

import * as React from 'react';
import { calculatePositions } from '../logic/pricing';
import { Page as PDFPage } from '@react-pdf/renderer';
import { pdfStyles } from './pdf/SharedUI';
import { DINLayout } from './pdf/DINLayout';
import { SimpleLayout } from './pdf/SimpleLayout';

// Modules
import { FrontPageModule } from './pdf/modules/FrontPageModule';
import { BriefingModule } from './pdf/modules/BriefingModule';
import { SitemapModule } from './pdf/modules/SitemapModule';
import { EstimationModule } from './pdf/modules/EstimationModule';
import { TransparenzModule, techPageModule as TechPageModule, PrinciplesModule } from './pdf/modules/CommonModules';
import { AboutModule, CrossSellModule } from './pdf/modules/BrandingModules';

interface PDFProps {
  state: any;
  totalPrice: number;
  monthlyPrice: number;
  totalPagesCount: number;
  pricing: any;
  mode?: 'estimation' | 'full';
  headerIcon?: string;
  footerLogo?: string;
  techDetails?: { t: string, d: string }[];
  principles?: { t: string, d: string }[];
}

export const EstimationPDF = ({
  state,
  totalPrice,
  pricing,
  mode = 'full',
  headerIcon,
  footerLogo,
  techDetails,
  principles,
  ...props
}: PDFProps) => {
  const date = new Date().toLocaleDateString('de-DE', {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
  });

  const positions = calculatePositions(state, pricing);

  const companyData = {
    name: "Marc Mintel",
    address1: "Georg-Meistermann-Straße 7",
    address2: "54586 Schüller",
    ustId: "DE367588065"
  };

  const bankData = {
    name: "N26",
    bic: "NTSBDEB1XXX",
    iban: "DE50 1001 1001 2620 4328 65"
  };

  const commonProps = {
    state,
    date,
    icon: headerIcon,
    footerLogo,
    companyData,
    bankData
  };

  if (mode === 'estimation') {
    return (
      <DINLayout {...commonProps} showAddress={true} showFooterDetails={true}>
        <EstimationModule state={state} positions={positions} totalPrice={totalPrice} date={date} />
      </DINLayout>
    );
  }

  // Full Portfolio Mode
  let pageCounter = 1;
  const getPageNum = () => (pageCounter++).toString().padStart(2, '0');

  return (
    <>
      <PDFPage size="A4" style={pdfStyles.titlePage}>
        <FrontPageModule state={state} headerIcon={headerIcon} date={date} />
      </PDFPage>

      <SimpleLayout {...commonProps} pageNumber={getPageNum()}>
        <BriefingModule state={state} />
      </SimpleLayout>

      {state.sitemap && state.sitemap.length > 0 && (
        <SimpleLayout {...commonProps} pageNumber={getPageNum()}>
          <SitemapModule state={state} />
        </SimpleLayout>
      )}

      <SimpleLayout {...commonProps} pageNumber={getPageNum()}>
        <EstimationModule state={state} positions={positions} totalPrice={totalPrice} date={date} />
      </SimpleLayout>

      <SimpleLayout {...commonProps} pageNumber={getPageNum()}>
        <TransparenzModule pricing={pricing} />
      </SimpleLayout>

      {techDetails && techDetails.length > 0 && (
        <SimpleLayout {...commonProps} pageNumber={getPageNum()}>
          <TechPageModule techDetails={techDetails} headerIcon={headerIcon} />
        </SimpleLayout>
      )}

      {principles && principles.length > 0 && (
        <SimpleLayout {...commonProps} pageNumber={getPageNum()}>
          <PrinciplesModule principles={principles} />
        </SimpleLayout>
      )}

      <SimpleLayout {...commonProps} pageNumber={getPageNum()}>
        <AboutModule />
      </SimpleLayout>

      <SimpleLayout {...commonProps} pageNumber={getPageNum()}>
        <CrossSellModule state={state} />
      </SimpleLayout>
    </>
  );
};
