import type { CollectionConfig } from 'payload'
import { monitoringSnapshotsAccess } from '../lib/access'

export const MonitoringSnapshots: CollectionConfig = {
  slug: 'monitoring-snapshots',
  admin: {
    useAsTitle: 'timestamp',
    group: 'Monitoring',
    description: 'Historische System-Metriken für Trend-Charts',
    defaultColumns: ['timestamp', 'system_cpuUsagePercent', 'system_memoryUsagePercent', 'createdAt'],
  },
  access: monitoringSnapshotsAccess,
  fields: [
    {
      name: 'timestamp',
      type: 'date',
      required: true,
      index: true,
      admin: {
        readOnly: true,
        date: { pickerAppearance: 'dayAndTime' },
      },
    },
    {
      name: 'system',
      type: 'group',
      admin: { description: 'System-Ressourcen (CPU, RAM, Disk)' },
      fields: [
        { name: 'cpuUsagePercent', type: 'number', admin: { readOnly: true } },
        { name: 'memoryUsedMB', type: 'number', admin: { readOnly: true } },
        { name: 'memoryTotalMB', type: 'number', admin: { readOnly: true } },
        { name: 'memoryUsagePercent', type: 'number', admin: { readOnly: true } },
        { name: 'diskUsedGB', type: 'number', admin: { readOnly: true } },
        { name: 'diskTotalGB', type: 'number', admin: { readOnly: true } },
        { name: 'diskUsagePercent', type: 'number', admin: { readOnly: true } },
        { name: 'loadAvg1', type: 'number', admin: { readOnly: true } },
        { name: 'loadAvg5', type: 'number', admin: { readOnly: true } },
        {
          name: 'uptime',
          type: 'number',
          admin: { readOnly: true, description: 'Uptime in Sekunden' },
        },
      ],
    },
    {
      name: 'services',
      type: 'group',
      admin: { description: 'Service-Status (PM2-Prozesse, Datenbank, Cache)' },
      fields: [
        { name: 'payload', type: 'json', admin: { readOnly: true } },
        { name: 'queueWorker', type: 'json', admin: { readOnly: true } },
        { name: 'postgresql', type: 'json', admin: { readOnly: true } },
        { name: 'pgbouncer', type: 'json', admin: { readOnly: true } },
        { name: 'redis', type: 'json', admin: { readOnly: true } },
      ],
    },
    {
      name: 'external',
      type: 'group',
      admin: { description: 'Externe Services (SMTP, OAuth, Cron)' },
      fields: [
        { name: 'smtp', type: 'json', admin: { readOnly: true } },
        { name: 'metaOAuth', type: 'json', admin: { readOnly: true } },
        { name: 'youtubeOAuth', type: 'json', admin: { readOnly: true } },
        { name: 'cronJobs', type: 'json', admin: { readOnly: true } },
        { name: 'secrets', type: 'json', admin: { readOnly: true } },
        { name: 'securityEvents', type: 'json', admin: { readOnly: true } },
      ],
    },
    {
      name: 'performance',
      type: 'group',
      admin: { description: 'Performance-Metriken' },
      fields: [
        { name: 'avgResponseTimeMs', type: 'number', admin: { readOnly: true } },
        { name: 'p95ResponseTimeMs', type: 'number', admin: { readOnly: true } },
        { name: 'p99ResponseTimeMs', type: 'number', admin: { readOnly: true } },
        { name: 'errorRate', type: 'number', admin: { readOnly: true } },
        { name: 'requestsPerMinute', type: 'number', admin: { readOnly: true } },
      ],
    },
  ],
}
