import { describe, it, expect, beforeEach } from 'vitest'
import { auditLogService, type AuditLogEntry } from '../auditLog'

describe('auditLogService', () => {
  beforeEach(() => {
    auditLogService.clear()
  })

  it('should log an entry', () => {
    auditLogService.log({
      action: 'create',
      entityType: 'compliance',
      entityId: 'test-id',
      entityName: 'Test Entity',
    })

    const logs = auditLogService.getLogs()
    expect(logs).toHaveLength(1)
    expect(logs[0].action).toBe('create')
    expect(logs[0].entityType).toBe('compliance')
    expect(logs[0].entityId).toBe('test-id')
    expect(logs[0].entityName).toBe('Test Entity')
    expect(logs[0].id).toBeDefined()
    expect(logs[0].timestamp).toBeDefined()
  })

  it('should filter logs by action', () => {
    auditLogService.log({
      action: 'create',
      entityType: 'compliance',
      entityId: '1',
      entityName: 'Entity 1',
    })

    auditLogService.log({
      action: 'update',
      entityType: 'compliance',
      entityId: '2',
      entityName: 'Entity 2',
    })

    const createLogs = auditLogService.getLogs({ action: 'create' })
    expect(createLogs).toHaveLength(1)
    expect(createLogs[0].action).toBe('create')
  })

  it('should filter logs by entity type', () => {
    auditLogService.log({
      action: 'create',
      entityType: 'compliance',
      entityId: '1',
      entityName: 'Entity 1',
    })

    auditLogService.log({
      action: 'create',
      entityType: 'milestone',
      entityId: '2',
      entityName: 'Entity 2',
    })

    const complianceLogs = auditLogService.getLogs({ entityType: 'compliance' })
    expect(complianceLogs).toHaveLength(1)
    expect(complianceLogs[0].entityType).toBe('compliance')
  })

  it('should filter logs by date range', () => {
    const now = new Date()
    const yesterday = new Date(now.getTime() - 24 * 60 * 60 * 1000)
    const tomorrow = new Date(now.getTime() + 24 * 60 * 60 * 1000)

    auditLogService.log({
      action: 'create',
      entityType: 'compliance',
      entityId: '1',
      entityName: 'Entity 1',
    })

    const logs = auditLogService.getLogs({
      startDate: yesterday.toISOString(),
      endDate: tomorrow.toISOString(),
    })

    expect(logs.length).toBeGreaterThan(0)
  })

  it('should get logs by entity', () => {
    auditLogService.log({
      action: 'create',
      entityType: 'compliance',
      entityId: 'test-id',
      entityName: 'Test Entity',
    })

    auditLogService.log({
      action: 'update',
      entityType: 'compliance',
      entityId: 'test-id',
      entityName: 'Test Entity',
    })

    const entityLogs = auditLogService.getLogsByEntity('compliance', 'test-id')
    expect(entityLogs).toHaveLength(2)
    expect(entityLogs.every((log) => log.entityId === 'test-id')).toBe(true)
  })

  it('should limit log history size', () => {
    // Add more than maxLogs entries
    for (let i = 0; i < 10001; i++) {
      auditLogService.log({
        action: 'create',
        entityType: 'compliance',
        entityId: `id-${i}`,
        entityName: `Entity ${i}`,
      })
    }

    const logs = auditLogService.getLogs()
    expect(logs.length).toBeLessThanOrEqual(10000)
  })
})

