// =============================================================================
// TOM Generator Module - Public API
// =============================================================================

// Types
export * from './types'

// Context and Hooks
export {
  TOMGeneratorProvider,
  useTOMGenerator,
  TOMGeneratorContext,
} from './context'
export type {
  TOMGeneratorAction,
  TOMGeneratorContextValue,
} from './context'

// Rules Engine
export {
  TOMRulesEngine,
  getTOMRulesEngine,
  evaluateControlsForContext,
  deriveTOMsForContext,
  performQuickGapAnalysis,
} from './rules-engine'

// Control Library
export {
  getControlLibrary,
  getAllControls,
  getControlById,
  getControlsByCategory,
  getControlsByType,
  getControlsByPriority,
  getControlsByTag,
  getAllTags,
  getCategoryMetadata,
  getAllCategories,
  getLibraryMetadata,
  searchControls,
  getControlsByFramework,
  getControlsCountByCategory,
} from './controls/loader'
export type { ControlLibrary } from './controls/loader'

// AI Integration
export {
  AI_PROMPTS,
  getDocumentAnalysisPrompt,
  getTOMDescriptionPrompt,
  getGapRecommendationsPrompt,
  getDocumentTypeDetectionPrompt,
  getClauseExtractionPrompt,
  getComplianceAssessmentPrompt,
} from './ai/prompts'
export type {
  DocumentAnalysisPromptContext,
  TOMDescriptionPromptContext,
  GapRecommendationsPromptContext,
} from './ai/prompts'

export {
  TOMDocumentAnalyzer,
  getDocumentAnalyzer,
  analyzeEvidenceDocument,
  detectEvidenceDocumentType,
  getEvidenceGapsForAllControls,
} from './ai/document-analyzer'
export type {
  AnalysisResult,
  DocumentTypeDetectionResult,
} from './ai/document-analyzer'

// Export Functions
export {
  generateDOCXContent,
  generateDOCXBlob,
} from './export/docx'
export type {
  DOCXExportOptions,
  DocxElement,
  DocxParagraph,
  DocxTable,
  DocxTableRow,
} from './export/docx'

export {
  generatePDFContent,
  generatePDFBlob,
} from './export/pdf'
export type {
  PDFExportOptions,
  PDFSection,
} from './export/pdf'

export {
  generateZIPFiles,
  generateZIPBlob,
} from './export/zip'
export type {
  ZIPExportOptions,
  ZIPFileEntry,
} from './export/zip'

// Demo Data
export {
  generateDemoState,
  generateEmptyState,
  generatePartialState,
  DEMO_COMPANY_PROFILES,
  DEMO_DATA_PROFILES,
  DEMO_ARCHITECTURE_PROFILES,
  DEMO_SECURITY_PROFILES,
  DEMO_RISK_PROFILES,
  DEMO_EVIDENCE_DOCUMENTS,
} from './demo-data'
export type { DemoScenario } from './demo-data'

// =============================================================================
// CONVENIENCE EXPORTS
// =============================================================================

import { TOMRulesEngine } from './rules-engine'
import {
  TOMGeneratorState,
  RulesEngineEvaluationContext,
  DerivedTOM,
  GapAnalysisResult,
  EvidenceDocument,
} from './types'

/**
 * Create a new TOM Rules Engine instance
 */
export function createRulesEngine(): TOMRulesEngine {
  return new TOMRulesEngine()
}

/**
 * Derive TOMs for a given state
 */
export function deriveTOMsFromState(state: TOMGeneratorState): DerivedTOM[] {
  const engine = new TOMRulesEngine()
  return engine.deriveAllTOMs({
    companyProfile: state.companyProfile,
    dataProfile: state.dataProfile,
    architectureProfile: state.architectureProfile,
    securityProfile: state.securityProfile,
    riskProfile: state.riskProfile,
  })
}

/**
 * Perform gap analysis on a state
 */
export function analyzeGapsFromState(
  state: TOMGeneratorState
): GapAnalysisResult {
  const engine = new TOMRulesEngine()
  return engine.performGapAnalysis(state.derivedTOMs, state.documents)
}

/**
 * Get completion statistics for a state
 */
export function getStateStatistics(state: TOMGeneratorState): {
  totalControls: number
  requiredControls: number
  implementedControls: number
  partialControls: number
  notImplementedControls: number
  complianceScore: number
  stepsCompleted: number
  totalSteps: number
  documentsUploaded: number
} {
  const totalControls = state.derivedTOMs.length
  const requiredControls = state.derivedTOMs.filter(
    (t) => t.applicability === 'REQUIRED'
  ).length
  const implementedControls = state.derivedTOMs.filter(
    (t) => t.implementationStatus === 'IMPLEMENTED'
  ).length
  const partialControls = state.derivedTOMs.filter(
    (t) => t.implementationStatus === 'PARTIAL'
  ).length
  const notImplementedControls = state.derivedTOMs.filter(
    (t) => t.implementationStatus === 'NOT_IMPLEMENTED'
  ).length

  const stepsCompleted = state.steps.filter((s) => s.completed).length
  const totalSteps = state.steps.length

  return {
    totalControls,
    requiredControls,
    implementedControls,
    partialControls,
    notImplementedControls,
    complianceScore: state.gapAnalysis?.overallScore ?? 0,
    stepsCompleted,
    totalSteps,
    documentsUploaded: state.documents.length,
  }
}
