import { Document, Page, View, Text, StyleSheet } from "@react-pdf/renderer";
import type { SeoSection } from "../types";
import { S, C } from "./styles";
import { Cover } from "./components/Cover";
import { PageHeader } from "./components/PageHeader";
import { PageFooter } from "./components/PageFooter";
import { ExecutiveBriefPdfSection } from "./components/ExecutiveBriefPdfSection";

const s = StyleSheet.create({
  ctaPage: { backgroundColor: C.accentDark, padding: 72, justifyContent: "center", alignItems: "center" },
  ctaHeading: { fontSize: 28, fontFamily: "Helvetica-Bold", color: C.white, textAlign: "center", marginBottom: 16 },
  ctaSubtext: { fontSize: 11, color: "rgba(255,255,255,0.75)", textAlign: "center", lineHeight: 1.6, marginBottom: 24, maxWidth: 400 },
  ctaUrlBox: { backgroundColor: "rgba(255,255,255,0.12)", borderRadius: 6, paddingHorizontal: 20, paddingVertical: 10 },
  ctaUrl: { fontSize: 10, color: C.white, letterSpacing: 0.5 },
});

export interface ExecutiveAuditPdfProps {
  brandName: string;
  website: string;
  auditDate: string;
  auditId: string;
  isFirstParty: boolean;
  seo: SeoSection;
  comprehensiveAuditUrl?: string;
}

export function ExecutiveAuditPdf({
  brandName, website, auditDate, isFirstParty, seo, comprehensiveAuditUrl,
}: ExecutiveAuditPdfProps) {
  const displayName = seo.displayName || brandName;
  const brief = seo.executiveBrief;

  if (!brief) return null;

  return (
    <Document
      title={`meSEO Executive Brief — ${brandName}`}
      author="meSEO"
      subject="AI Search Visibility Executive Brief"
      creator="meSEO Audit Engine"
    >
      {/* Cover */}
      <Cover
        brandName={displayName}
        website={website}
        auditDate={auditDate}
        score={seo.healthScore}
        isFirstParty={isFirstParty}
        siteTagDaysActive={seo.siteTagContext?.daysActive}
        aiTrend={seo.aiSearchPerformance?.trendVsTrailing90d}
      />

      {/* Executive Brief — 5 pages (page breaks handled internally) */}
      <Page size="LETTER" style={S.contentPage}>
        <PageHeader sectionName="Executive Brief" brandName={displayName} />
        <PageFooter isFirstParty={isFirstParty} />
        <ExecutiveBriefPdfSection brief={brief} isFirstParty={isFirstParty} />
      </Page>

      {/* Optional closing CTA — "Download the Comprehensive Audit" */}
      {comprehensiveAuditUrl && (
        <Page size="LETTER" style={s.ctaPage}>
          <Text style={s.ctaHeading}>Ready for the Full Picture?</Text>
          <Text style={s.ctaSubtext}>
            This Executive Brief covers the strategic highlights. The Comprehensive Audit includes all{" "}
            {isFirstParty ? "40+" : "25+"} sections: page-by-page reviews, implementation code, schema templates,
            competitive intelligence, and complete data tables.
          </Text>
          <View style={s.ctaUrlBox}>
            <Text style={s.ctaUrl}>{comprehensiveAuditUrl}</Text>
          </View>
        </Page>
      )}
    </Document>
  );
}
