// Eval report document — ported from EvalReportDocument.tsx
import { Document } from '@react-pdf/renderer';
import { EvalReportCover } from './cover.js';
import { EvalReportMatrixPage } from './matrix-page.js';
import { EvalReportExamplePage } from './example-page.js';
import { EvalReportV2Cover } from './v2-cover.js';
import { EvalReportItemPage } from './item-page.js';
import { calculateWeightedScores } from '../scoring.js';
import type { MatrixData, WeightConfig, EvaluationResult } from './types.js';

// V1 report data (matrix-based)
export interface EvalReportDataV1 {
  customerName: string;
  reportDate: string;
  matrixData: MatrixData;
  weights: WeightConfig;
}

// V2 report data (per-item results)
export interface EvalReportDataV2 {
  customerName: string;
  reportDate: string;
  v2Results: EvaluationResult[];
}

// Union type
export type EvalReportData = EvalReportDataV1 | EvalReportDataV2;

function isV2ReportData(data: EvalReportData): data is EvalReportDataV2 {
  return 'v2Results' in data && Array.isArray(data.v2Results);
}

interface EvalReportDocumentProps {
  data: EvalReportData;
}

export function EvalReportDocument({ data }: EvalReportDocumentProps) {
  if (isV2ReportData(data)) {
    return (
      <Document
        title={`Indemn Evaluation Report - ${data.customerName}`}
        author="Indemn Evals"
        subject={`Evaluation report for ${data.reportDate}`}
      >
        <EvalReportV2Cover
          customerName={data.customerName}
          reportDate={data.reportDate}
          results={data.v2Results}
        />
        {data.v2Results.map((result, index) => (
          <EvalReportItemPage
            key={result.result_id}
            result={result}
            itemNumber={index + 1}
            totalItems={data.v2Results.length}
          />
        ))}
      </Document>
    );
  }

  // V1 report
  const weightedScores = calculateWeightedScores(data.matrixData, data.weights);

  const highlights = {
    totalQuestions: data.matrixData.rows.length,
    totalRules: data.matrixData.evaluators.length,
    overallScore: weightedScores.overall,
    highSeverityScore: weightedScores.bySeverity.High.score,
    mediumSeverityScore: weightedScores.bySeverity.Medium.score,
    highCount: weightedScores.bySeverity.High.count,
    mediumCount: weightedScores.bySeverity.Medium.count,
  };

  const matrixPageNumber = 2;
  const exampleStartPage = 3;

  return (
    <Document
      title={`Indemn Evaluation Report - ${data.customerName}`}
      author="Indemn Evals"
      subject={`Evaluation report for ${data.reportDate}`}
    >
      <EvalReportCover
        customerName={data.customerName}
        reportDate={data.reportDate}
        highlights={highlights}
      />
      <EvalReportMatrixPage
        matrixData={data.matrixData}
        pageNumber={matrixPageNumber}
      />
      {data.matrixData.rows.map((row, index) => (
        <EvalReportExamplePage
          key={row.resultId}
          exampleNumber={index + 1}
          totalExamples={data.matrixData.rows.length}
          pageNumber={exampleStartPage + index}
          row={row}
          evaluators={data.matrixData.evaluators}
        />
      ))}
    </Document>
  );
}
