import type { AuditSimulationSectionStatus, AuditSimulationStatus } from "@prisma/client";
import { z } from "zod";

export const AUDIT_SECTION_KEYS = [
  "cumplimiento_fiscal",
  "cumplimiento_laboral",
  "documentacion_legal",
  "control_operativo",
  "transparencia_financiera",
] as const;

export type AuditSectionKey = (typeof AUDIT_SECTION_KEYS)[number];

export type AuditKpiSnapshot = {
  completedAudits: number;
  lastScore: number | null;
  registeredContracts: number;
  securedPdfs: number;
};

export type AuditSimulationView = {
  id: string;
  name: string;
  auditType: string;
  status: AuditSimulationStatus;
  overallScore: number | null;
  completedAt: string | null;
  createdAt: string;
  updatedAt: string;
  sections: AuditSimulationSectionView[];
  responses: AuditChecklistResponseView[];
};

export type AuditSimulationSectionView = {
  id: string;
  simulationId: string;
  key: AuditSectionKey;
  score: number | null;
  status: AuditSimulationSectionStatus;
  findings: string[];
  recommendations: string[];
};

export type AuditChecklistResponseView = {
  id: string;
  simulationId: string;
  questionKey: string;
  answer: string;
  evidenceRefs: string[];
};

export type AuditSectionScore = {
  key: AuditSectionKey;
  score: number;
  status: AuditSimulationSectionStatus;
  findings: string[];
  recommendations: string[];
};

export type AuditScoreOutput = {
  overallScore: number;
  sections: AuditSectionScore[];
};

export type InstitutionalDossierComponentStatus = {
  key: "m5" | "m8" | "m9" | "m7";
  label: string;
  status: "completo" | "incompleto";
  totalItems: number;
  withEvidence: number;
};

export type InstitutionalDossierLoadStrategy = "live" | "snapshot";

export type InstitutionalDossierEvidenceLink = {
  label: string;
  href: string;
};

export type InstitutionalDossierEvidenceDocument = {
  id: string;
  fileName: string;
  kind: string | null;
  mimeType: string;
  sizeBytes: number;
  createdAt: string;
  traceLabel: string;
  links: InstitutionalDossierEvidenceLink[];
};

export type InstitutionalDossierPayload = {
  generatedAt: string;
  summary: {
    contracts: number;
    contractDocuments: number;
    proposals: number;
    proposalDocuments: number;
    legalCases: number;
    legalDocuments: number;
    highRiskAlerts: number;
  };
  components: InstitutionalDossierComponentStatus[];
  contracts: Array<{
    id: string;
    title: string;
    status: string;
    deliverables: number;
    payments: number;
    documents: number;
    links: InstitutionalDossierEvidenceLink[];
    documentEntries: InstitutionalDossierEvidenceDocument[];
  }>;
  proposals: Array<{
    id: string;
    title: string;
    status: string;
    documents: number;
    links: InstitutionalDossierEvidenceLink[];
    documentEntries: InstitutionalDossierEvidenceDocument[];
  }>;
  legal: Array<{
    id: string;
    caseType: string;
    severity: string;
    status: string;
    documents: number;
    links: InstitutionalDossierEvidenceLink[];
    documentEntries: InstitutionalDossierEvidenceDocument[];
  }>;
  aiFindings?: AuditAiFindings | null;
};

export type InstitutionalDossierFreshness = {
  generatedAt: string;
  isStale: boolean;
  source: "live" | "snapshot";
  strategy: InstitutionalDossierLoadStrategy;
  snapshotId: string | null;
  staleAfterMinutes: number;
};

export type AuditAiFindings = {
  auditorLikelyFindings: {
    area: string;
    finding: string;
    severity: "alto" | "medio" | "bajo";
  }[];
  missingEvidence: string[];
  topRisks: string[];
  remediationPlan: {
    step: string;
    priority: "alta" | "media" | "baja";
    ownerSuggestion: string;
    targetDate: string;
  }[];
  confidence: "low" | "medium" | "high";
  suggestionId: string;
};

export const AuditSimulationCreateInputSchema = z.object({
  name: z.string().trim().min(2).max(220),
  auditType: z.string().trim().min(2).max(120),
});

export const AuditSimulationScoreInputSchema = z.object({
  responses: z.array(
    z.object({
      questionKey: z.string().trim().min(2).max(120),
      answer: z.string().trim().min(1).max(120),
      evidenceRefs: z.array(z.string().trim().min(1).max(260)).optional(),
    }),
  ),
});

export type AuditSimulationCreateInput = z.infer<typeof AuditSimulationCreateInputSchema>;
export type AuditSimulationScoreInput = z.infer<typeof AuditSimulationScoreInputSchema>;
