export const IdentifierType = {
  EMAIL: "EMAIL",
  PHONE: "PHONE",
  SSN: "SSN",
} as const;
export type IdentifierType = (typeof IdentifierType)[keyof typeof IdentifierType];

export const SubscriptionTier = {
  BASIC: "BASIC",
  PLUS: "PLUS",
  PREMIUM: "PREMIUM",
} as const;
export type SubscriptionTier = (typeof SubscriptionTier)[keyof typeof SubscriptionTier];

export const Severity = {
  LOW: "LOW",
  INFO: "INFO",
  MEDIUM: "MEDIUM",
  WARNING: "WARNING",
  HIGH: "HIGH",
  CRITICAL: "CRITICAL",
} as const;
export type Severity = (typeof Severity)[keyof typeof Severity];

export const AlertSource = {
  DARKWATCH: "DARKWATCH",
  SPAMSHIELD: "SPAMSHIELD",
  VOICEPRINT: "VOICEPRINT",
  CALL_ANALYSIS: "CALL_ANALYSIS",
} as const;
export type AlertSource = (typeof AlertSource)[keyof typeof AlertSource];

export const AlertCategory = {
  BREACH_EXPOSURE: "BREACH_EXPOSURE",
  SPAM_CALL: "SPAM_CALL",
  SPAM_SMS: "SPAM_SMS",
  SYNTHETIC_VOICE: "SYNTHETIC_VOICE",
  VOICE_MISMATCH: "VOICE_MISMATCH",
  CALL_QUALITY: "CALL_QUALITY",
  CALL_ANOMALY: "CALL_ANOMALY",
  CALL_EVENT: "CALL_EVENT",
} as const;
export type AlertCategory = (typeof AlertCategory)[keyof typeof AlertCategory];

export const CorrelationStatus = {
  ACTIVE: "ACTIVE",
  RESOLVED: "RESOLVED",
  FALSE_POSITIVE: "FALSE_POSITIVE",
} as const;
export type CorrelationStatus = (typeof CorrelationStatus)[keyof typeof CorrelationStatus];

export const EntityTypes = {
  PHONE_NUMBER: "PHONE_NUMBER",
  EMAIL: "EMAIL",
  USER_ID: "USER_ID",
  CALL_ID: "CALL_ID",
  IP_ADDRESS: "IP_ADDRESS",
} as const;
export type EntityType = (typeof EntityTypes)[keyof typeof EntityTypes];

export interface NormalizedAlertInput {
  source: AlertSource;
  category: AlertCategory;
  severity: Severity;
  userId: string;
  title: string;
  description: string;
  entities: Array<{ type: EntityType; value: string }>;
  sourceAlertId: string;
  payload: Record<string, unknown>;
  timestamp?: Date;
}

export interface CorrelationGroupOutput {
  id: string;
  groupId: string;
  alertCount: number;
  highestSeverity: Severity;
  status: CorrelationStatus;
  entities: Array<{ type: EntityType; value: string }>;
  sources: AlertSource[];
  categories: AlertCategory[];
  createdAt: Date;
  updatedAt: Date;
}

export interface CorrelatedAlertOutput {
  id: string;
  source: AlertSource;
  category: AlertCategory;
  severity: Severity;
  userId: string;
  title: string;
  description: string;
  entities: Array<{ type: EntityType; value: string }>;
  sourceAlertId: string;
  groupId: string;
  payload: Record<string, unknown>;
  createdAt: Date;
}

export interface CorrelationQuery {
  userId?: string;
  groupId?: string;
  source?: AlertSource;
  category?: AlertCategory;
  severity?: Severity;
  status?: CorrelationStatus;
  entityType?: EntityType;
  entityId?: string;
  timeWindowMinutes?: number;
  limit?: number;
  offset?: number;
}

export const AlertChannel = {
  EMAIL: "EMAIL",
  PUSH: "PUSH",
  SMS: "SMS",
} as const;
export type AlertChannel = (typeof AlertChannel)[keyof typeof AlertChannel];

export const AlertStatus = {
  PENDING: "PENDING",
  SENT: "SENT",
  READ: "READ",
} as const;
export type AlertStatus = (typeof AlertStatus)[keyof typeof AlertStatus];

export const ScanJobStatus = {
  PENDING: "PENDING",
  RUNNING: "RUNNING",
  COMPLETED: "COMPLETED",
  FAILED: "FAILED",
} as const;
export type ScanJobStatus = (typeof ScanJobStatus)[keyof typeof ScanJobStatus];

export const WatchListStatus = {
  ACTIVE: "ACTIVE",
  PAUSED: "PAUSED",
} as const;
export type WatchListStatus = (typeof WatchListStatus)[keyof typeof WatchListStatus];

export const DataSource = {
  HIBP: "HIBP",
  SECURITY_TRAILS: "SECURITY_TRAILS",
  CENSYS: "CENSYS",
  SHODAN: "SHODAN",
  HONEYPOT: "HONEYPOT",
} as const;
export type DataSource = (typeof DataSource)[keyof typeof DataSource];

export interface WatchListItemInput {
  identifierType: IdentifierType;
  identifierValue: string;
}

export interface ScanTriggerInput {
  userId: string;
  source?: DataSource;
}

export interface ExposureResult {
  dataSource: DataSource;
  breachName: string;
  exposedAt: Date;
  dataType: string[];
  severity: Severity;
  details: string;
}

export interface AlertInput {
  userId: string;
  exposureId: string;
  severity: Severity;
  channel: AlertChannel;
  dedupKey: string;
}

export const AnalysisJobStatus = {
  PENDING: "PENDING",
  RUNNING: "RUNNING",
  COMPLETED: "COMPLETED",
  FAILED: "FAILED",
} as const;
export type AnalysisJobStatus = (typeof AnalysisJobStatus)[keyof typeof AnalysisJobStatus];

export const AnalysisType = {
  SYNTHETIC_DETECTION: "SYNTHETIC_DETECTION",
  VOICE_MATCH: "VOICE_MATCH",
  BATCH: "BATCH",
} as const;
export type AnalysisType = (typeof AnalysisType)[keyof typeof AnalysisType];

export const DetectionVerdict = {
  NATURAL: "NATURAL",
  SYNTHETIC: "SYNTHETIC",
  UNCERTAIN: "UNCERTAIN",
} as const;
export type DetectionVerdict = (typeof DetectionVerdict)[keyof typeof DetectionVerdict];

export interface VoiceEnrollmentInput {
  label: string;
  audioBuffer: Buffer;
  sampleRate?: number;
}

export interface AnalyzeAudioInput {
  audioBuffer: Buffer;
  sampleRate?: number;
  analysisType?: AnalysisType;
}

export interface BatchAnalyzeInput {
  audioBuffers: Array<{ name: string; buffer: Buffer; sampleRate?: number }>;
  analysisType?: AnalysisType;
}

export interface AnalysisResultOutput {
  jobId: string;
  syntheticScore: number;
  verdict: DetectionVerdict;
  confidence: number;
  matchedEnrollmentId?: string;
  matchedSimilarity?: number;
  processingTimeMs: number;
  modelVersion?: string;
}

export interface VoiceEnrollmentOutput {
  id: string;
  label: string;
  embeddingDim: number;
  sampleRate: number;
  durationSec?: number;
  createdAt: Date;
}

export const ScheduleStatus = {
  ACTIVE: "ACTIVE",
  PAUSED: "PAUSED",
} as const;
export type ScheduleStatus = (typeof ScheduleStatus)[keyof typeof ScheduleStatus];

export const WebhookEventType = {
  SCAN_TRIGGER: "SCAN_TRIGGER",
  BREACH_DETECTED: "BREACH_DETECTED",
  SUBSCRIPTION_CHANGE: "SUBSCRIPTION_CHANGE",
} as const;
export type WebhookEventType = (typeof WebhookEventType)[keyof typeof WebhookEventType];

export interface WebhookTriggerInput {
  eventType: string;
  payload: Record<string, unknown>;
  source?: string;
  signature?: string;
}

export interface ScanScheduleOutput {
  id: string;
  userId: string;
  intervalMinutes: number;
  cronExpression: string;
  status: ScheduleStatus;
  lastScanAt?: Date;
  nextScanAt?: Date;
}

export interface SchedulerConfig {
  intervalMinutes: number;
  cronExpression: string;
}

export { generateRequestId, extractOrGenerateRequestId } from "./requestId";

// ============================================
// Report Generation Types
// ============================================

export const ReportType = {
  MONTHLY_PLUS: "MONTHLY_PLUS",
  ANNUAL_PREMIUM: "ANNUAL_PREMIUM",
} as const;
export type ReportType = (typeof ReportType)[keyof typeof ReportType];

export const ReportStatus = {
  PENDING: "PENDING",
  GENERATING: "GENERATING",
  COMPLETED: "COMPLETED",
  FAILED: "FAILED",
  DELIVERED: "DELIVERED",
} as const;
export type ReportStatus = (typeof ReportStatus)[keyof typeof ReportStatus];

export interface ReportExposureSummary {
  totalExposures: number;
  newExposures: number;
  resolvedExposures: number;
  criticalExposures: number;
  warningExposures: number;
  infoExposures: number;
  exposuresBySource: Record<string, number>;
}

export interface ReportSpamStats {
  callsBlocked: number;
  textsBlocked: number;
  callsFlagged: number;
  textsFlagged: number;
  falsePositives: number;
  totalSpamEvents: number;
}

export interface ReportVoiceStats {
  analysesRun: number;
  threatsDetected: number;
  enrollmentsActive: number;
  syntheticDetections: number;
  voiceMismatchEvents: number;
}

export interface ReportHomeTitleStats {
  propertiesMonitored: number;
  changesDetected: number;
  alertsTriggered: number;
}

export interface ReportRecommendation {
  category: string;
  priority: 'high' | 'medium' | 'low';
  title: string;
  description: string;
}

export interface ReportDataPayload {
  exposureSummary: ReportExposureSummary;
  spamStats: ReportSpamStats;
  voiceStats: ReportVoiceStats;
  homeTitleStats?: ReportHomeTitleStats;
  recommendations: ReportRecommendation[];
  protectionScore: number;
  previousProtectionScore?: number;
}

export interface GenerateReportInput {
  userId: string;
  subscriptionId: string;
  reportType: ReportType;
  periodStart?: Date;
  periodEnd?: Date;
}

export interface SecurityReportOutput {
  id: string;
  userId: string;
  reportType: ReportType;
  status: ReportStatus;
  periodStart: Date;
  periodEnd: Date;
  title: string;
  summary?: string;
  pdfUrl?: string;
  dataPayload?: ReportDataPayload;
  error?: string;
  createdAt: Date;
  deliveredAt?: Date;
}
