import { describe, it, expect } from 'vitest'
import type {
  SystemMetrics,
  MonitoringEvent,
  AlertCondition,
  LogLevel,
  LogSource,
} from '@/lib/monitoring/types'

describe('Monitoring Types', () => {
  it('SystemMetrics has all required sections', () => {
    const metrics: SystemMetrics = {
      timestamp: new Date().toISOString(),
      system: {
        cpuUsagePercent: 23,
        memoryUsedMB: 4200,
        memoryTotalMB: 8192,
        memoryUsagePercent: 51.3,
        diskUsedGB: 30,
        diskTotalGB: 50,
        diskUsagePercent: 60,
        loadAvg1: 0.5,
        loadAvg5: 0.8,
        uptime: 1209600,
      },
      services: {
        payload: {
          status: 'online',
          pid: 1234,
          memoryMB: 512,
          uptimeSeconds: 86400,
          restarts: 0,
        },
        queueWorker: {
          status: 'online',
          pid: 5678,
          memoryMB: 256,
          uptimeSeconds: 86400,
          restarts: 0,
        },
        postgresql: { status: 'online', connections: 12, maxConnections: 50, latencyMs: 2 },
        pgbouncer: { status: 'online', activeConnections: 8, waitingClients: 0, poolSize: 20 },
        redis: { status: 'online', memoryUsedMB: 48, connectedClients: 5, opsPerSec: 120 },
      },
      external: {
        smtp: {
          status: 'online',
          lastCheck: new Date().toISOString(),
          responseTimeMs: 180,
        },
        metaOAuth: { status: 'ok', tokensTotal: 2, tokensExpiringSoon: 1, tokensExpired: 0 },
        youtubeOAuth: { status: 'ok', tokensTotal: 3, tokensExpiringSoon: 0, tokensExpired: 0 },
        cronJobs: {
          communitySync: { lastRun: new Date().toISOString(), status: 'ok' },
          tokenRefresh: { lastRun: new Date().toISOString(), status: 'ok' },
          youtubeSync: { lastRun: new Date().toISOString(), status: 'ok' },
        },
        secrets: {
          status: 'ok',
          checkedAt: new Date().toISOString(),
          missing: [],
          expiringSoon: [],
          expired: [],
          rotationOverdue: [],
        },
        securityEvents: {
          windowMs: 300000,
          counters: [],
        },
      },
      performance: {
        avgResponseTimeMs: 120,
        p95ResponseTimeMs: 350,
        p99ResponseTimeMs: 800,
        errorRate: 0.02,
        requestsPerMinute: 45,
      },
    }

    expect(metrics.system.cpuUsagePercent).toBe(23)
    expect(metrics.services.payload.status).toBe('online')
    expect(metrics.external.smtp.status).toBe('online')
    expect(metrics.performance.avgResponseTimeMs).toBe(120)
  })

  it('MonitoringEvent types are exhaustive', () => {
    const events: MonitoringEvent['type'][] = ['health', 'service', 'alert', 'log', 'performance']
    expect(events).toHaveLength(5)
  })

  it('AlertCondition covers all comparison operators', () => {
    const conditions: AlertCondition[] = ['gt', 'lt', 'eq', 'gte', 'lte']
    expect(conditions).toHaveLength(5)
  })

  it('LogLevel covers all severity levels', () => {
    const levels: LogLevel[] = ['debug', 'info', 'warn', 'error', 'fatal']
    expect(levels).toHaveLength(5)
  })

  it('LogSource covers all system components', () => {
    const sources: LogSource[] = ['payload', 'queue-worker', 'cron', 'email', 'oauth', 'sync', 'security']
    expect(sources).toHaveLength(7)
  })
})
