export type SignaturePolicyStatus = "permitido" | "condicionado" | "requiere_validacion_legal";

export type SignaturePolicyMatrixEntry = {
  id: string;
  stateCode: string;
  stateName: string;
  municipalityCode: string | null;
  municipalityName: string | null;
  appliesTo: "licitaciones" | "contratos" | "general";
  policyStatus: SignaturePolicyStatus;
  policyName: string;
  evidenceRequired: string[];
  notes: string;
  sourceUrl: string | null;
};

export type SignaturePolicyEvaluation = {
  policyStatus: SignaturePolicyStatus;
  policyName: string;
  evidenceRequired: string[];
  notes: string;
  jurisdictionLabel: string;
  sourceUrl: string | null;
};

export type ComplianceAlertSeverity = "high" | "medium" | "low";

export type ComplianceAlertKind =
  | "deadline_overdue"
  | "deadline_soon"
  | "critical_requirement_pending"
  | "signature_policy"
  | "normative_regulation_update"
  | "normative_verification_pending"
  | "contract_deliverable_overdue"
  | "contract_payment_disputed"
  | "legal_case_high_severity";

export type ComplianceAlertView = {
  id: string;
  proposalId: string | null;
  licitationId: string | null;
  title: string;
  description: string;
  severity: ComplianceAlertSeverity;
  kind: ComplianceAlertKind;
  dueAt: string | null;
  createdAt: string;
};

export type ComplianceDeadlineItem = {
  id: string;
  proposalId: string | null;
  licitationId: string | null;
  title: string;
  description: string;
  dueAt: string;
  source: "milestone" | "normative_verification";
  status: "upcoming" | "overdue";
};

export type ComplianceChecklistItem = {
  proposalId: string;
  proposalTitle: string;
  mandatoryResolved: number;
  mandatoryTotal: number;
  completionRate: number;
  signaturePolicyStatus: SignaturePolicyStatus;
};

export type CompliancePanelItem = {
  id: string;
  label: string;
  value: string;
  tone: "neutral" | "success" | "warning" | "danger";
};

export type M7KpiSnapshot = {
  activeLicitations: number;
  completionRate: number;
  criticalPending: number;
  upcoming7Days: number;
};

export type M7Dataset = {
  generatedAt: string;
  kpis: M7KpiSnapshot;
  m3States: {
    consulted: number;
    interested: number;
    active: number;
    new: number;
  };
  tabs: {
    plazos: ComplianceDeadlineItem[];
    alertas: ComplianceAlertView[];
    checklist: ComplianceChecklistItem[];
    panelKpi: CompliancePanelItem[];
  };
  aiPlaybook?: M7AiPlaybook | null;
};

export type M7AiPlaybook = {
  predictedIncidents: {
    title: string;
    likelihood: "alta" | "media" | "baja";
    impact: "alto" | "medio" | "bajo";
    timeHorizon: string;
  }[];
  priorityOrder: string[];
  preventiveActions: {
    action: string;
    ownerSuggestion: string;
    targetDate: string;
  }[];
  escalationAdvice: string[];
  confidence: "low" | "medium" | "high";
  suggestionId: string;
};

export type OfficialNormativeSourceView = {
  id: string;
  stateCode: string;
  stateName: string;
  municipalityCode: string | null;
  municipalityName: string | null;
  authorityName: string;
  title: string;
  officialUrl: string;
  sourceType: "ley" | "reglamento" | "lineamiento" | "portal";
  versionLabel: string | null;
  lastKnownHash: string | null;
  lastVerifiedAt: string | null;
  nextCheckAt: string | null;
  isPilot: boolean;
};

export type OfficialNormativeSuggestionInput = {
  stateCode: string;
  municipalityCode?: string | null;
  authorityName: string;
  title: string;
  officialUrl: string;
  sourceType: OfficialNormativeSourceView["sourceType"];
  notes?: string;
};

export type NormativeVerificationResultView = {
  sourceId: string;
  checkedAt: string;
  changed: boolean;
  status: "success" | "warning" | "failed";
  httpStatus: number | null;
  observedHash: string | null;
  message: string;
};
