import { describe, it, expect } from 'vitest';
import { CreateIncidentSchema, UpdateIncidentSchema } from './types.js';
import type { ServiceHealth } from './types.js';

describe('CreateIncidentSchema', () => {
  it('accepts valid incident', () => {
    const result = CreateIncidentSchema.safeParse({
      title: 'Platform Service Degraded',
      severity: 'major',
      message: 'Increased error rates on platform-service.',
      affectedServices: ['Platform Service'],
    });
    expect(result.success).toBe(true);
  });

  it('rejects empty title', () => {
    const result = CreateIncidentSchema.safeParse({
      title: '',
      severity: 'minor',
      message: 'Test',
      affectedServices: ['Platform Service'],
    });
    expect(result.success).toBe(false);
  });

  it('rejects empty affected services', () => {
    const result = CreateIncidentSchema.safeParse({
      title: 'Test',
      severity: 'minor',
      message: 'Test',
      affectedServices: [],
    });
    expect(result.success).toBe(false);
  });

  it('rejects invalid severity', () => {
    const result = CreateIncidentSchema.safeParse({
      title: 'Test',
      severity: 'unknown',
      message: 'Test',
      affectedServices: ['Platform Service'],
    });
    expect(result.success).toBe(false);
  });

  it('accepts all valid severities', () => {
    for (const severity of ['minor', 'major', 'critical']) {
      const result = CreateIncidentSchema.safeParse({
        title: 'Test',
        severity,
        message: 'Test',
        affectedServices: ['Platform Service'],
      });
      expect(result.success).toBe(true);
    }
  });
});

describe('UpdateIncidentSchema', () => {
  it('accepts status update', () => {
    const result = UpdateIncidentSchema.safeParse({
      status: 'identified',
      message: 'Root cause identified — database connection pool exhaustion.',
    });
    expect(result.success).toBe(true);
  });

  it('accepts severity change only', () => {
    const result = UpdateIncidentSchema.safeParse({ severity: 'critical' });
    expect(result.success).toBe(true);
  });

  it('accepts resolution', () => {
    const result = UpdateIncidentSchema.safeParse({
      status: 'resolved',
      message: 'Connection pool size increased. Monitoring.',
    });
    expect(result.success).toBe(true);
  });

  it('accepts all valid statuses', () => {
    for (const status of ['investigating', 'identified', 'monitoring', 'resolved']) {
      const result = UpdateIncidentSchema.safeParse({ status });
      expect(result.success).toBe(true);
    }
  });

  it('rejects invalid status', () => {
    const result = UpdateIncidentSchema.safeParse({ status: 'unknown' });
    expect(result.success).toBe(false);
  });
});

describe('ServiceHealth type coverage', () => {
  it('should support all health states', () => {
    const states: ServiceHealth[] = [
      'operational',
      'degraded',
      'partial_outage',
      'major_outage',
      'maintenance',
    ];
    expect(states.length).toBe(5);
  });
});
