export interface MiddlewareConfig {
  id: string
  middleware_name: string
  enabled: boolean
  config: Record<string, any>
  updated_at: string | null
}

export interface RateLimitIP {
  id: string
  ip_address: string
  list_type: 'whitelist' | 'blacklist'
  reason: string | null
  expires_at: string | null
  created_at: string
}

export interface MiddlewareEvent {
  id: string
  middleware_name: string
  event_type: string
  ip_address: string | null
  user_id: string | null
  request_path: string | null
  request_method: string | null
  details: Record<string, any> | null
  created_at: string
}

export interface MiddlewareStats {
  middleware_name: string
  total_events: number
  events_last_hour: number
  events_last_24h: number
  top_event_types: Array<{ event_type: string; count: number }>
  top_ips: Array<{ ip_address: string; count: number }>
}

export type TabType = 'overview' | 'rate-limiting' | 'security' | 'logging' | 'events'

export const MIDDLEWARE_INFO: Record<string, { name: string; description: string; icon: string }> = {
  request_id: {
    name: 'Request-ID',
    description: 'Generates unique identifiers for request tracing',
    icon: '🔑',
  },
  security_headers: {
    name: 'Security Headers',
    description: 'Adds security headers (HSTS, CSP, X-Frame-Options)',
    icon: '🛡️',
  },
  cors: {
    name: 'CORS',
    description: 'Cross-Origin Resource Sharing configuration',
    icon: '🌐',
  },
  rate_limiter: {
    name: 'Rate Limiter',
    description: 'Protects against abuse with request limits',
    icon: '⏱️',
  },
  pii_redactor: {
    name: 'PII Redactor',
    description: 'Redacts sensitive data from logs (DSGVO)',
    icon: '🔒',
  },
  input_gate: {
    name: 'Input Gate',
    description: 'Validates request body size and content types',
    icon: '🚧',
  },
}

export const TABS = [
  { id: 'overview', label: 'Overview', icon: '📊' },
  { id: 'rate-limiting', label: 'Rate Limiting', icon: '⏱️' },
  { id: 'security', label: 'Security', icon: '🛡️' },
  { id: 'logging', label: 'Logging', icon: '📝' },
  { id: 'events', label: 'Events', icon: '📋' },
]

export interface MiddlewareHookReturn {
  activeTab: TabType
  setActiveTab: (tab: TabType) => void
  configs: MiddlewareConfig[]
  ipList: RateLimitIP[]
  events: MiddlewareEvent[]
  stats: MiddlewareStats[]
  loading: boolean
  saving: boolean
  newIP: { ip_address: string; list_type: string; reason: string }
  setNewIP: (ip: { ip_address: string; list_type: string; reason: string }) => void
  loadData: () => Promise<void>
  toggleMiddleware: (name: string, enabled: boolean) => Promise<void>
  updateConfig: (name: string, config: Record<string, any>) => Promise<void>
  addIP: () => Promise<void>
  removeIP: (id: string) => Promise<void>
  getConfig: (name: string) => MiddlewareConfig | undefined
  getStats: (name: string) => MiddlewareStats | undefined
}
