import { describe, it, expect } from 'vitest'
import type {
  ErrorRecord,
  ErrorDetailFull,
  ErrorDetailResponse,
  ErrorDistribution,
  ErrorTrend,
  FailingResource
} from '../types'

describe('Error Tracking Types', () => {
  it('ErrorRecord type is correctly defined', () => {
    const record: ErrorRecord = {
      id: '123e4567-e89b-12d3-a456-426614174000',
      timestamp: '2024-01-15T10:30:00Z',
      errorType: 'ValidationError',
      message: 'Invalid input',
      executionId: '223e4567-e89b-12d3-a456-426614174000',
      resourceId: 'workflow-123',
      resourceName: 'Test Workflow',
      severity: 'warning'
    }

    expect(record).toBeDefined()
    expect(record.severity).toBe('warning')
  })

  it('ErrorDetailFull extends ErrorRecord with optional fields', () => {
    const detail: ErrorDetailFull = {
      id: '123e4567-e89b-12d3-a456-426614174000',
      timestamp: '2024-01-15T10:30:00Z',
      errorType: 'ValidationError',
      message: 'Invalid input',
      executionId: '223e4567-e89b-12d3-a456-426614174000',
      resourceId: 'workflow-123',
      resourceName: 'Test Workflow',
      severity: 'critical',
      stackTrace: 'Error stack...',
      retryAttempt: 2,
      stepName: 'ValidateInput',
      stepSequence: 1,
      errorContext: { field: 'email' },
      executionContext: { userId: '123' }
    }

    expect(detail).toBeDefined()
    expect(detail.stackTrace).toBe('Error stack...')
  })

  it('ErrorDetailResponse contains paginated error records', () => {
    const response: ErrorDetailResponse = {
      errors: [],
      total: 100,
      page: 1,
      limit: 20
    }

    expect(response).toBeDefined()
    expect(response.total).toBe(100)
  })

  it('ErrorDistribution contains analytics data', () => {
    const distribution: ErrorDistribution = {
      label: 'ValidationError',
      count: 42,
      percentage: 35.5
    }

    expect(distribution).toBeDefined()
    expect(distribution.percentage).toBe(35.5)
  })

  it('ErrorTrend contains time-series data', () => {
    const trend: ErrorTrend = {
      time: '2024-01-15T10:00:00Z',
      errorCount: 150,
      criticalCount: 10,
      warningCount: 50,
      infoCount: 90
    }

    expect(trend).toBeDefined()
    expect(trend.errorCount).toBe(150)
  })

  it('FailingResource contains resource health data', () => {
    const resource: FailingResource = {
      resourceId: 'workflow-123',
      resourceName: 'Test Workflow',
      errorCount: 25,
      criticalCount: 5,
      warningCount: 15,
      mostCommonError: 'ValidationError'
    }

    expect(resource).toBeDefined()
    expect(resource.mostCommonError).toBe('ValidationError')
  })

  it('severity type is correctly constrained', () => {
    const validSeverities: Array<ErrorRecord['severity']> = [
      'critical',
      'warning',
      'info'
    ]

    expect(validSeverities).toHaveLength(3)
  })
})
