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

export const MonitoringAlertHistory: CollectionConfig = {
  slug: 'monitoring-alert-history',
  admin: {
    useAsTitle: 'message',
    group: 'Monitoring',
    description: 'Alert-Log (WORM - Write Once)',
    defaultColumns: ['severity', 'metric', 'message', 'createdAt', 'acknowledgedBy'],
  },
  access: monitoringAlertHistoryAccess,
  fields: [
    {
      name: 'rule',
      type: 'relationship',
      relationTo: 'monitoring-alert-rules',
      admin: { readOnly: true },
    },
    {
      name: 'metric',
      type: 'text',
      required: true,
      admin: { readOnly: true },
    },
    {
      name: 'value',
      type: 'number',
      required: true,
      admin: { readOnly: true },
    },
    {
      name: 'threshold',
      type: 'number',
      required: true,
      admin: { readOnly: true },
    },
    {
      name: 'severity',
      type: 'select',
      required: true,
      options: [
        { label: 'Warnung', value: 'warning' },
        { label: 'Fehler', value: 'error' },
        { label: 'Kritisch', value: 'critical' },
      ],
      admin: { readOnly: true },
    },
    {
      name: 'message',
      type: 'text',
      required: true,
      admin: { readOnly: true },
    },
    {
      name: 'channelsSent',
      type: 'select',
      hasMany: true,
      options: [
        { label: 'E-Mail', value: 'email' },
        { label: 'Slack', value: 'slack' },
        { label: 'Discord', value: 'discord' },
      ],
      admin: { readOnly: true },
    },
    {
      name: 'resolvedAt',
      type: 'date',
      admin: { date: { pickerAppearance: 'dayAndTime' } },
    },
    {
      name: 'acknowledgedBy',
      type: 'relationship',
      relationTo: 'users',
    },
  ],
}
