import { describe, it, expect } from 'vitest'
import mongoose from 'mongoose'
import AdminAlertDismissal from '../../../server/models/adminAlertDismissal.js'

describe('AdminAlertDismissal model', () => {
  it('exposes the expected schema paths', () => {
    const paths = AdminAlertDismissal.schema.paths
    expect(paths.adminId).toBeDefined()
    expect(paths.alertType).toBeDefined()
    expect(paths.signature).toBeDefined()
    expect(paths.dismissedAt).toBeDefined()
  })

  it('marks adminId, alertType, and signature as required', () => {
    const paths = AdminAlertDismissal.schema.paths
    expect(paths.adminId.isRequired).toBe(true)
    expect(paths.alertType.isRequired).toBe(true)
    expect(paths.signature.isRequired).toBe(true)
  })

  it('restricts alertType to the nine known slugs', () => {
    const enumValues = AdminAlertDismissal.schema.paths.alertType.enumValues
    expect(enumValues).toEqual([
      'slack_invite_failed',
      'no_slack_handle_week',
      'stuck_pending_payment',
      'member_suspended',
      'preregistrant_selected_not_invited',
      'preregistrant_expired',
      'event_draft_imminent',
      'event_near_capacity',
      'tag_suggestions_pending'
    ])
  })

  it('declares a unique compound index on adminId + alertType', () => {
    const indexes = AdminAlertDismissal.schema.indexes()
    const compound = indexes.find(([fields]) =>
      fields.adminId === 1 && fields.alertType === 1
    )
    expect(compound).toBeDefined()
    expect(compound[1].unique).toBe(true)
  })

  it('defaults dismissedAt to the current time', () => {
    const doc = new AdminAlertDismissal({
      adminId: new mongoose.Types.ObjectId(),
      alertType: 'slack_invite_failed',
      signature: 'abc'
    })
    expect(doc.dismissedAt).toBeInstanceOf(Date)
  })
})
