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

export const MonitoringLogs: CollectionConfig = {
  slug: 'monitoring-logs',
  admin: {
    useAsTitle: 'message',
    group: 'Monitoring',
    description: 'Structured Logs für Business-Events',
    defaultColumns: ['level', 'source', 'message', 'createdAt'],
  },
  access: monitoringLogsAccess,
  fields: [
    {
      name: 'level',
      type: 'select',
      required: true,
      options: [
        { label: 'Debug', value: 'debug' },
        { label: 'Info', value: 'info' },
        { label: 'Warn', value: 'warn' },
        { label: 'Error', value: 'error' },
        { label: 'Fatal', value: 'fatal' },
      ],
      admin: { readOnly: true },
    },
    {
      name: 'source',
      type: 'select',
      required: true,
      options: [
        { label: 'Payload', value: 'payload' },
        { label: 'Queue Worker', value: 'queue-worker' },
        { label: 'Cron', value: 'cron' },
        { label: 'Email', value: 'email' },
        { label: 'OAuth', value: 'oauth' },
        { label: 'Sync', value: 'sync' },
        { label: 'Security', value: 'security' },
      ],
      admin: { readOnly: true },
    },
    {
      name: 'message',
      type: 'text',
      required: true,
      admin: { readOnly: true },
    },
    {
      name: 'context',
      type: 'json',
      admin: { readOnly: true, description: 'Strukturierte Metadaten' },
    },
    {
      name: 'requestId',
      type: 'text',
      admin: { readOnly: true, description: 'Korrelations-ID' },
    },
    {
      name: 'userId',
      type: 'relationship',
      relationTo: 'users',
      admin: { readOnly: true },
    },
    {
      name: 'tenant',
      type: 'relationship',
      relationTo: 'tenants',
      admin: { readOnly: true },
    },
    {
      name: 'duration',
      type: 'number',
      min: 0,
      admin: { readOnly: true, description: 'Dauer in ms' },
    },
  ],
}
