import { describe, it, expect } from 'vitest'
import {
  ErrorRecordSchema,
  ErrorDetailFullSchema,
  ErrorDistributionSchema,
  ErrorTrendSchema
} from '../schemas'

describe('Error Tracking Schemas', () => {
  describe('ErrorRecordSchema', () => {
    it('validates valid error record', () => {
      const validRecord = {
        id: '123e4567-e89b-12d3-a456-426614174000',
        timestamp: '2024-01-15T10:30:00Z',
        errorType: 'ValidationError',
        message: 'Invalid input provided',
        executionId: '223e4567-e89b-12d3-a456-426614174000',
        resourceId: 'workflow-123',
        resourceName: 'Test Workflow',
        severity: 'warning' as const
      }

      const result = ErrorRecordSchema.safeParse(validRecord)
      expect(result.success).toBe(true)
    })

    it('rejects invalid severity', () => {
      const invalidRecord = {
        id: '123e4567-e89b-12d3-a456-426614174000',
        timestamp: '2024-01-15T10:30:00Z',
        errorType: 'ValidationError',
        message: 'Invalid input provided',
        executionId: '223e4567-e89b-12d3-a456-426614174000',
        resourceId: 'workflow-123',
        resourceName: 'Test Workflow',
        severity: 'invalid' // Invalid severity
      }

      const result = ErrorRecordSchema.safeParse(invalidRecord)
      expect(result.success).toBe(false)
    })

    it('rejects non-UUID id', () => {
      const invalidRecord = {
        id: 'not-a-uuid',
        timestamp: '2024-01-15T10:30:00Z',
        errorType: 'ValidationError',
        message: 'Invalid input provided',
        executionId: '223e4567-e89b-12d3-a456-426614174000',
        resourceId: 'workflow-123',
        resourceName: 'Test Workflow',
        severity: 'warning' as const
      }

      const result = ErrorRecordSchema.safeParse(invalidRecord)
      expect(result.success).toBe(false)
    })
  })

  describe('ErrorDetailFullSchema', () => {
    it('validates error detail with all optional fields', () => {
      const validDetail = {
        id: '123e4567-e89b-12d3-a456-426614174000',
        timestamp: '2024-01-15T10:30:00Z',
        errorType: 'ValidationError',
        message: 'Invalid input provided',
        executionId: '223e4567-e89b-12d3-a456-426614174000',
        resourceId: 'workflow-123',
        resourceName: 'Test Workflow',
        severity: 'critical' as const,
        stackTrace: 'Error: Test\n  at line 1',
        retryAttempt: 2,
        stepName: 'ValidateInput',
        stepSequence: 1,
        errorContext: { field: 'email', value: 'invalid' },
        executionContext: { userId: '123', orgId: '456' }
      }

      const result = ErrorDetailFullSchema.safeParse(validDetail)
      expect(result.success).toBe(true)
    })

    it('validates error detail without optional fields', () => {
      const minimalDetail = {
        id: '123e4567-e89b-12d3-a456-426614174000',
        timestamp: '2024-01-15T10:30:00Z',
        errorType: 'ValidationError',
        message: 'Invalid input provided',
        executionId: '223e4567-e89b-12d3-a456-426614174000',
        resourceId: 'workflow-123',
        resourceName: 'Test Workflow',
        severity: 'info' as const
      }

      const result = ErrorDetailFullSchema.safeParse(minimalDetail)
      expect(result.success).toBe(true)
    })
  })

  describe('ErrorDistributionSchema', () => {
    it('validates error distribution data', () => {
      const validDistribution = {
        label: 'ValidationError',
        count: 42,
        percentage: 35.5
      }

      const result = ErrorDistributionSchema.safeParse(validDistribution)
      expect(result.success).toBe(true)
    })

    it('rejects missing fields', () => {
      const invalidDistribution = {
        label: 'ValidationError',
        count: 42
        // Missing percentage
      }

      const result = ErrorDistributionSchema.safeParse(invalidDistribution)
      expect(result.success).toBe(false)
    })
  })

  describe('ErrorTrendSchema', () => {
    it('validates error trend data', () => {
      const validTrend = {
        time: '2024-01-15T10:00:00Z',
        errorCount: 150,
        criticalCount: 10,
        warningCount: 50,
        infoCount: 90
      }

      const result = ErrorTrendSchema.safeParse(validTrend)
      expect(result.success).toBe(true)
    })

    it('rejects non-number counts', () => {
      const invalidTrend = {
        time: '2024-01-15T10:00:00Z',
        errorCount: '150', // Should be number
        criticalCount: 10,
        warningCount: 50,
        infoCount: 90
      }

      const result = ErrorTrendSchema.safeParse(invalidTrend)
      expect(result.success).toBe(false)
    })
  })
})
