export type LLMProvider = 'openai' | 'anthropic' | 'ollama' | 'minimax' | 'openrouter';

export type TerrainCategory = 'I' | 'II' | 'III' | 'IV' | 'V';
export type StructureClass = 'A' | 'B' | 'C';

export interface AuditConfig {
  provider: LLMProvider;
  model: string;
  apiKey: string;
  baseUrl?: string;
}

export type ModuleType =
  | 'galpao'
  | 'cilindro'
  | 'vault'
  | 'dome'
  | 'sign'
  | 'isolated-roof'
  | 'bar'
  | 'bridge'
  | 'tower'
  | 'dynamics'
  | 'cross-module'
  | 'edge-case'
  | 'cpi-variant'
  | 'piperack';

export const MODULE_LABELS: Record<ModuleType, { 'pt-BR': string; 'en-US': string }> = {
  galpao: { 'pt-BR': 'Galpão', 'en-US': 'Warehouse' },
  cilindro: { 'pt-BR': 'Cilindro', 'en-US': 'Cylinder' },
  vault: { 'pt-BR': 'Abóbada', 'en-US': 'Vault' },
  dome: { 'pt-BR': 'Cúpula', 'en-US': 'Dome' },
  sign: { 'pt-BR': 'Muros/Placas', 'en-US': 'Signs/Walls' },
  'isolated-roof': { 'pt-BR': 'Cobertura Isolada', 'en-US': 'Isolated Roof' },
  bar: { 'pt-BR': 'Barras', 'en-US': 'Bars' },
  bridge: { 'pt-BR': 'Pontes', 'en-US': 'Bridges' },
  tower: { 'pt-BR': 'Torres', 'en-US': 'Towers' },
  dynamics: { 'pt-BR': 'Dinâmica', 'en-US': 'Dynamics' },
  'cross-module': { 'pt-BR': 'Cross-Module', 'en-US': 'Cross-Module' },
  'edge-case': { 'pt-BR': 'Edge Cases', 'en-US': 'Edge Cases' },
  'cpi-variant': { 'pt-BR': 'Cpi Variants', 'en-US': 'Cpi Variants' },
  piperack: { 'pt-BR': 'Piperack', 'en-US': 'Piperack' },
};

export type DiagramType =
  | 'warehouse'
  | 'cylinder'
  | 'vault'
  | 'dome'
  | 'sign'
  | 'bar'
  | 'bridge'
  | 'tower'
  | 'isolatedRoof'
  | 'dynamics';

export interface ExpectedRange {
  readonly min: number;
  readonly max: number;
}

export interface AuditScenario {
  readonly id: string;
  readonly module: ModuleType;
  readonly moduleLabel: string;
  readonly description: string;
  readonly inputs: Record<string, unknown>;
  readonly intermediates: Record<string, unknown>;
  readonly outputs: Record<string, unknown>;
  readonly expectedRanges: Record<string, ExpectedRange>;
  readonly diagramType: DiagramType;
  readonly diagramProps: Record<string, unknown>;
  readonly nbrSection: string;
  readonly isBibliographic?: boolean;
  readonly bibliografiaReferencia?: string;
  readonly enunciadoParafraseado?: string;
}

export type CheckStatus = 'PASS' | 'WARN' | 'FAIL';

export interface AuditCheck {
  readonly name: string;
  readonly status: CheckStatus;
  readonly expected: string;
  readonly actual: string;
  readonly explanation: string;
}

export interface ScenarioResult {
  readonly scenarioId: string;
  readonly verdict: CheckStatus;
  readonly checks: readonly AuditCheck[];
  readonly summary: string;
  readonly calculation_steps?: string;
  readonly llmNotes?: string;
  readonly enunciado_problema?: string;
  readonly tabelas_nbr_consultadas?: string;
  readonly resultado_app?: string;
  readonly desvio_percentual?: string;
}

export interface ModuleAuditResult {
  readonly module: ModuleType;
  readonly moduleLabel: string;
  readonly totalScenarios: number;
  readonly passed: number;
  readonly warnings: number;
  readonly failed: number;
  readonly scenarios: readonly ScenarioResult[];
}

export interface AuditReport {
  readonly id: string;
  readonly provider: string;
  readonly model: string;
  readonly timestamp: number;
  readonly totalScenarios: number;
  readonly totalPassed: number;
  readonly totalWarnings: number;
  readonly totalFailed: number;
  readonly modules: readonly ModuleAuditResult[];
  readonly rawResponse?: string;
}

export type AuditStatus = 'idle' | 'generating' | 'calling' | 'parsing' | 'done' | 'error';

export interface AuditRun {
  readonly config: AuditConfig;
  readonly report: AuditReport | null;
  readonly status: AuditStatus;
  readonly progress: number;
  readonly error?: string;
}

export type RouteModuleMap = Record<ModuleType, string>;

export interface AuditLLMResponse {
  readonly scenarioId: string;
  readonly verdict: CheckStatus;
  readonly checks: readonly {
    readonly name: string;
    readonly status: CheckStatus;
    readonly expected: string;
    readonly actual: string;
    readonly explanation: string;
  }[];
  readonly summary: string;
  readonly calculation_steps?: string;
  readonly llmNotes?: string;
  readonly enunciado_problema?: string;
  readonly tabelas_nbr_consultadas?: string;
  readonly resultado_app?: string;
  readonly desvio_percentual?: string;
}
