import type { CollectionConfig } from 'payload'

export interface ConsentLogData {
  action: 'accept' | 'reject' | 'update'
  purpose: 'analytics' | 'marketing' | 'functional' | 'all'
  userAgent?: string
  ip?: string
  timestamp: string
  previousConsent?: Record<string, boolean>
  newConsent?: Record<string, boolean>
}

const ConsentLogs: CollectionConfig = {
  slug: 'consent-logs',
  admin: {
    useAsTitle: 'timestamp',
    defaultColumns: ['timestamp', 'action', 'purpose', 'ip'],
    description: 'Log of all consent actions for PDPA compliance',
  },
  access: {
    create: () => true, // Allow anyone to create consent logs (public endpoint)
    read: () => true, // Allow reading for compliance purposes
    update: () => false, // Consent logs should not be modified
    delete: () => false, // Consent logs should not be deleted
  },
  fields: [
    {
      name: 'action',
      type: 'select',
      required: true,
      options: [
        { label: 'Accept', value: 'accept' },
        { label: 'Reject', value: 'reject' },
        { label: 'Update', value: 'update' },
      ],
      admin: {
        description: 'The type of consent action',
      },
    },
    {
      name: 'purpose',
      type: 'select',
      required: true,
      options: [
        { label: 'Analytics', value: 'analytics' },
        { label: 'Marketing', value: 'marketing' },
        { label: 'Functional', value: 'functional' },
        { label: 'All', value: 'all' },
      ],
      admin: {
        description: 'The purpose of the consent',
      },
    },
    {
      name: 'analytics',
      type: 'checkbox',
      defaultValue: false,
      admin: {
        description: 'Consent for analytics cookies',
      },
    },
    {
      name: 'marketing',
      type: 'checkbox',
      defaultValue: false,
      admin: {
        description: 'Consent for marketing cookies',
      },
    },
    {
      name: 'functional',
      type: 'checkbox',
      defaultValue: false,
      admin: {
        description: 'Consent for functional cookies',
      },
    },
    {
      name: 'userAgent',
      type: 'text',
      admin: {
        readOnly: true,
        description: 'Browser user agent string',
      },
    },
    {
      name: 'ip',
      type: 'text',
      admin: {
        readOnly: true,
        description: 'IP address of the user',
      },
    },
    {
      name: 'timestamp',
      type: 'date',
      required: true,
      admin: {
        readOnly: true,
        description: 'When the consent was given',
      },
    },
    {
      name: 'previousConsent',
      type: 'json',
      admin: {
        readOnly: true,
        description: 'Previous consent state (for updates)',
      },
    },
    {
      name: 'newConsent',
      type: 'json',
      admin: {
        readOnly: true,
        description: 'New consent state',
      },
    },
  ],
}

export default ConsentLogs
