export interface AuditLogEntry {
  id: string;
  timestamp: Date;
  user: {
    id: string;
    username: string;
    email: string;
    roles: string[];
  };
  action: {
    type: AuditActionType;
    resource: string;
    resourceId?: string;
    description: string;
  };
  context: {
    sessionId: string;
    requestId: string;
    ipAddress: string;
    userAgent: string;
    location?: {
      country: string;
      city: string;
      coordinates?: [number, number];
    };
    mfaEnabled: boolean;
  };
  result: {
    success: boolean;
    decision?: 'Allow' | 'Deny';
    reason?: string;
    errorCode?: string;
    errorMessage?: string;
  };
  severity: AuditSeverity;
  tags: string[];
  metadata: Record<string, any>;
  compliance: {
    soc2Relevant: boolean;
    hipaaRelevant: boolean;
    pciRelevant: boolean;
    gdprRelevant: boolean;
  };
  retention: {
    retentionPeriod: number; // days
    archiveDate?: Date;
    deletionDate?: Date;
  };
}

export type AuditActionType =
  | 'authentication'
  | 'authorization'
  | 'policy_evaluation'
  | 'policy_creation'
  | 'policy_update'
  | 'policy_deletion'
  | 'user_creation'
  | 'user_update'
  | 'user_deletion'
  | 'role_assignment'
  | 'role_revocation'
  | 'data_access'
  | 'data_modification'
  | 'data_deletion'
  | 'system_configuration'
  | 'backup_creation'
  | 'backup_restoration'
  | 'security_incident'
  | 'compliance_check'
  | 'anomaly_detection'
  | 'session_start'
  | 'session_end'
  | 'mfa_challenge'
  | 'password_change'
  | 'api_access';

export type AuditSeverity = 'low' | 'medium' | 'high' | 'critical';

export interface AuditSearchFilters {
  dateRange: {
    start?: Date;
    end?: Date;
  };
  users: string[];
  actions: AuditActionType[];
  resources: string[];
  severity: AuditSeverity[];
  success?: boolean;
  complianceFrameworks: string[];
  tags: string[];
  requestId?: string;
  sessionId?: string;
  ipAddress?: string;
  searchTerm?: string;
}

export interface SavedSearch {
  id: string;
  name: string;
  description?: string;
  filters: AuditSearchFilters;
  isPublic: boolean;
  createdBy: string;
  createdAt: Date;
  lastUsed?: Date;
  useCount: number;
}

export interface AuditExportRequest {
  format: 'csv' | 'json' | 'pdf';
  filters: AuditSearchFilters;
  includeMetadata: boolean;
  includeCompliance: boolean;
  dateFormat?: string;
  template?: string;
}

export interface ComplianceReport {
  id: string;
  type: 'soc2' | 'hipaa' | 'pci' | 'gdpr' | 'custom';
  title: string;
  description: string;
  generatedAt: Date;
  period: {
    start: Date;
    end: Date;
  };
  summary: {
    totalEvents: number;
    compliantEvents: number;
    violations: number;
    criticalFindings: number;
  };
  sections: ComplianceReportSection[];
  recommendations: ComplianceRecommendation[];
  attestations: Attestation[];
}

export interface ComplianceReportSection {
  id: string;
  title: string;
  description: string;
  status: 'compliant' | 'non-compliant' | 'partial' | 'not-applicable';
  findings: ComplianceFinding[];
  evidence: AuditLogEntry[];
  controls: ComplianceControl[];
}

export interface ComplianceFinding {
  id: string;
  severity: 'low' | 'medium' | 'high' | 'critical';
  title: string;
  description: string;
  recommendation: string;
  status: 'open' | 'in-progress' | 'resolved' | 'accepted-risk';
  assignee?: string;
  dueDate?: Date;
  evidence: AuditLogEntry[];
  remediation?: RemediationTask;
}

export interface ComplianceControl {
  id: string;
  framework: string;
  controlId: string;
  title: string;
  description: string;
  status: 'implemented' | 'partially-implemented' | 'not-implemented';
  effectiveness: 'effective' | 'partially-effective' | 'ineffective' | 'not-tested';
  lastTested?: Date;
  nextTestDate?: Date;
  evidence: string[];
}

export interface ComplianceRecommendation {
  id: string;
  priority: 'low' | 'medium' | 'high' | 'critical';
  title: string;
  description: string;
  category: string;
  effort: 'low' | 'medium' | 'high';
  impact: 'low' | 'medium' | 'high';
  timeline: string;
  assignee?: string;
  status: 'pending' | 'in-progress' | 'completed' | 'deferred';
}

export interface RemediationTask {
  id: string;
  findingId: string;
  title: string;
  description: string;
  priority: 'low' | 'medium' | 'high' | 'critical';
  status: 'pending' | 'in-progress' | 'completed' | 'verified' | 'closed';
  assignee: string;
  createdAt: Date;
  dueDate: Date;
  completedAt?: Date;
  verifiedAt?: Date;
  steps: RemediationStep[];
  comments: RemediationComment[];
}

export interface RemediationStep {
  id: string;
  title: string;
  description: string;
  status: 'pending' | 'in-progress' | 'completed' | 'skipped';
  assignee?: string;
  dueDate?: Date;
  completedAt?: Date;
  evidence?: string[];
}

export interface RemediationComment {
  id: string;
  author: string;
  content: string;
  createdAt: Date;
  type: 'comment' | 'status-change' | 'assignment' | 'evidence';
}

export interface Attestation {
  id: string;
  type: 'annual' | 'quarterly' | 'monthly' | 'ad-hoc';
  framework: string;
  period: {
    start: Date;
    end: Date;
  };
  attestor: {
    name: string;
    title: string;
    email: string;
  };
  status: 'draft' | 'pending' | 'signed' | 'expired';
  createdAt: Date;
  signedAt?: Date;
  expiresAt?: Date;
  controls: string[];
  evidence: AuditLogEntry[];
  signature?: {
    hash: string;
    algorithm: string;
    timestamp: Date;
  };
  certifications: AttestationCertification[];
}

export interface AttestationCertification {
  id: string;
  name: string;
  authority: string;
  validFrom: Date;
  validTo: Date;
  status: 'valid' | 'expired' | 'revoked';
  certificateUrl?: string;
}

export interface AuditDashboardStats {
  totalEvents: number;
  recentEvents: number;
  successRate: number;
  failureRate: number;
  criticalEvents: number;
  complianceScore: number;
  topUsers: Array<{ user: string; count: number }>;
  topActions: Array<{ action: AuditActionType; count: number }>;
  topResources: Array<{ resource: string; count: number }>;
  hourlyDistribution: Array<{ hour: number; count: number }>;
  geographicDistribution: Array<{ country: string; count: number }>;
}

export interface AuditVisualizationData {
  timeline: Array<{
    timestamp: Date;
    events: number;
    severity: Record<AuditSeverity, number>;
  }>;
  userActivity: Array<{
    user: string;
    actions: Array<{
      timestamp: Date;
      action: AuditActionType;
      success: boolean;
    }>;
  }>;
  resourceAccess: Array<{
    resource: string;
    accessCount: number;
    uniqueUsers: number;
    lastAccessed: Date;
  }>;
  complianceBreakdown: Array<{
    framework: string;
    compliant: number;
    nonCompliant: number;
    total: number;
  }>;
}

export interface WebSocketMessage {
  type: 'new_audit_log' | 'compliance_alert' | 'system_status' | 'heartbeat';
  data: any;
  timestamp: Date;
}
