import { describe, it, expect } from 'vitest';
import { UpdateMaintenanceSchema, CreateMaintenanceWindowSchema } from './types.js';
import type { MaintenanceConfig, MaintenanceMode } from './types.js';

describe('UpdateMaintenanceSchema', () => {
  it('accepts valid maintenance mode update', () => {
    const result = UpdateMaintenanceSchema.safeParse({
      mode: 'maintenance',
      message: 'Scheduled maintenance in progress.',
    });
    expect(result.success).toBe(true);
  });

  it('accepts all valid modes', () => {
    const modes: MaintenanceMode[] = ['off', 'read_only', 'maintenance', 'emergency'];
    for (const mode of modes) {
      const result = UpdateMaintenanceSchema.safeParse({ mode, message: 'Test' });
      expect(result.success).toBe(true);
    }
  });

  it('rejects invalid mode', () => {
    const result = UpdateMaintenanceSchema.safeParse({ mode: 'partial', message: 'Test' });
    expect(result.success).toBe(false);
  });

  it('rejects empty message', () => {
    const result = UpdateMaintenanceSchema.safeParse({ mode: 'maintenance', message: '' });
    expect(result.success).toBe(false);
  });

  it('defaults bypassRoles and bypassIPs to empty arrays', () => {
    const result = UpdateMaintenanceSchema.safeParse({ mode: 'off', message: 'OK' });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.bypassRoles).toEqual([]);
      expect(result.data.bypassIPs).toEqual([]);
      expect(result.data.affectedServices).toEqual(['*']);
    }
  });

  it('accepts bypass roles and IPs', () => {
    const result = UpdateMaintenanceSchema.safeParse({
      mode: 'read_only',
      message: 'DB migration in progress',
      bypassRoles: ['super_admin'],
      bypassIPs: ['10.0.0.1'],
      affectedServices: ['api', 'extraction'],
    });
    expect(result.success).toBe(true);
  });

  it('accepts scheduled start and end times', () => {
    const result = UpdateMaintenanceSchema.safeParse({
      mode: 'maintenance',
      message: 'Planned downtime',
      scheduledStart: '2026-03-20T02:00:00.000Z',
      scheduledEnd: '2026-03-20T04:00:00.000Z',
    });
    expect(result.success).toBe(true);
  });
});

describe('CreateMaintenanceWindowSchema', () => {
  it('accepts valid maintenance window', () => {
    const result = CreateMaintenanceWindowSchema.safeParse({
      title: 'Database migration',
      message: 'We are performing scheduled database maintenance.',
      scheduledStart: '2026-03-20T02:00:00.000Z',
      scheduledEnd: '2026-03-20T04:00:00.000Z',
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.mode).toBe('maintenance');
      expect(result.data.affectedServices).toEqual(['*']);
    }
  });

  it('accepts read_only mode', () => {
    const result = CreateMaintenanceWindowSchema.safeParse({
      title: 'Index rebuild',
      message: 'Read-only mode during index rebuild.',
      mode: 'read_only',
      scheduledStart: '2026-03-20T02:00:00.000Z',
      scheduledEnd: '2026-03-20T04:00:00.000Z',
    });
    expect(result.success).toBe(true);
  });

  it('rejects emergency mode for windows', () => {
    const result = CreateMaintenanceWindowSchema.safeParse({
      title: 'Emergency',
      message: 'Test',
      mode: 'emergency',
      scheduledStart: '2026-03-20T02:00:00.000Z',
      scheduledEnd: '2026-03-20T04:00:00.000Z',
    });
    expect(result.success).toBe(false);
  });

  it('rejects empty title', () => {
    const result = CreateMaintenanceWindowSchema.safeParse({
      title: '',
      message: 'Test',
      scheduledStart: '2026-03-20T02:00:00.000Z',
      scheduledEnd: '2026-03-20T04:00:00.000Z',
    });
    expect(result.success).toBe(false);
  });
});

describe('MaintenanceConfig type coverage', () => {
  it('should allow building a valid config object', () => {
    const config: MaintenanceConfig = {
      mode: 'off',
      message: '',
      bypassRoles: ['admin'],
      bypassIPs: [],
      affectedServices: ['*'],
      updatedAt: new Date().toISOString(),
      updatedBy: 'usr_admin',
    };
    expect(config.mode).toBe('off');
    expect(config.adminMessage).toBeUndefined();
  });
});
