/**
 * Backup/Restore module — unit tests.
 */

import { describe, it, expect } from 'vitest';
import { CreateBackupSchema, RestoreBackupSchema, UpdateBackupConfigSchema } from './types.js';

describe('CreateBackupSchema', () => {
  it('validates minimal backup', () => {
    const result = CreateBackupSchema.safeParse({ label: 'Daily backup' });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.type).toBe('full');
    }
  });

  it('validates with all fields', () => {
    const result = CreateBackupSchema.safeParse({
      label: 'Pre-migration backup',
      type: 'selective',
      containers: ['users', 'audit_log', 'subscriptions'],
      expiresInDays: 90,
    });
    expect(result.success).toBe(true);
  });

  it('rejects empty label', () => {
    expect(CreateBackupSchema.safeParse({ label: '' }).success).toBe(false);
  });

  it('rejects invalid type', () => {
    expect(CreateBackupSchema.safeParse({ label: 'Test', type: 'partial' }).success).toBe(false);
  });

  it('rejects expiry over 365 days', () => {
    expect(CreateBackupSchema.safeParse({ label: 'Test', expiresInDays: 366 }).success).toBe(false);
  });

  it('rejects too many containers', () => {
    const containers = Array.from({ length: 51 }, (_, i) => `container_${i}`);
    expect(CreateBackupSchema.safeParse({ label: 'Test', containers }).success).toBe(false);
  });
});

describe('RestoreBackupSchema', () => {
  it('validates minimal restore', () => {
    const result = RestoreBackupSchema.safeParse({ backupId: 'bak_123' });
    expect(result.success).toBe(true);
  });

  it('validates with container filter', () => {
    const result = RestoreBackupSchema.safeParse({
      backupId: 'bak_456',
      containers: ['users', 'subscriptions'],
    });
    expect(result.success).toBe(true);
  });

  it('rejects empty backupId', () => {
    expect(RestoreBackupSchema.safeParse({ backupId: '' }).success).toBe(false);
  });
});

describe('UpdateBackupConfigSchema', () => {
  it('validates partial config update', () => {
    const result = UpdateBackupConfigSchema.safeParse({
      scheduledBackupsEnabled: true,
      retentionDays: 60,
    });
    expect(result.success).toBe(true);
  });

  it('validates container lists', () => {
    const result = UpdateBackupConfigSchema.safeParse({
      includedContainers: ['users', 'audit_log'],
      excludedContainers: ['temp_data'],
    });
    expect(result.success).toBe(true);
  });

  it('rejects retention over 365', () => {
    expect(UpdateBackupConfigSchema.safeParse({ retentionDays: 366 }).success).toBe(false);
  });

  it('rejects maxBackups over 100', () => {
    expect(UpdateBackupConfigSchema.safeParse({ maxBackups: 101 }).success).toBe(false);
  });

  it('rejects zero maxBackups', () => {
    expect(UpdateBackupConfigSchema.safeParse({ maxBackups: 0 }).success).toBe(false);
  });
});
