/**
 * Data Retention module — unit tests.
 */

import { describe, it, expect } from 'vitest';
import {
  CreateRetentionPolicySchema,
  UpdateRetentionPolicySchema,
  getCutoffDate,
} from './types.js';

describe('CreateRetentionPolicySchema', () => {
  it('validates minimal policy', () => {
    const result = CreateRetentionPolicySchema.safeParse({
      containerName: 'audit_log',
      label: 'Delete old audit entries',
      retentionDays: 90,
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.action).toBe('delete');
      expect(result.data.dateField).toBe('createdAt');
      expect(result.data.isEnabled).toBe(true);
      expect(result.data.schedule).toBe('daily');
    }
  });

  it('validates with all fields', () => {
    const result = CreateRetentionPolicySchema.safeParse({
      containerName: 'delivery_log',
      label: 'Archive old delivery records',
      retentionDays: 365,
      action: 'archive',
      dateField: 'updatedAt',
      filter: 'status = "delivered"',
      isEnabled: false,
      schedule: 'weekly',
    });
    expect(result.success).toBe(true);
  });

  it('rejects empty container name', () => {
    expect(
      CreateRetentionPolicySchema.safeParse({
        containerName: '',
        label: 'Test',
        retentionDays: 30,
      }).success
    ).toBe(false);
  });

  it('rejects zero retention days', () => {
    expect(
      CreateRetentionPolicySchema.safeParse({
        containerName: 'test',
        label: 'Test',
        retentionDays: 0,
      }).success
    ).toBe(false);
  });

  it('rejects retention over 10 years', () => {
    expect(
      CreateRetentionPolicySchema.safeParse({
        containerName: 'test',
        label: 'Test',
        retentionDays: 3651,
      }).success
    ).toBe(false);
  });

  it('rejects invalid action', () => {
    expect(
      CreateRetentionPolicySchema.safeParse({
        containerName: 'test',
        label: 'Test',
        retentionDays: 30,
        action: 'destroy',
      }).success
    ).toBe(false);
  });
});

describe('UpdateRetentionPolicySchema', () => {
  it('validates partial update', () => {
    const result = UpdateRetentionPolicySchema.safeParse({
      retentionDays: 180,
      isEnabled: false,
    });
    expect(result.success).toBe(true);
  });

  it('validates label change', () => {
    expect(UpdateRetentionPolicySchema.safeParse({ label: 'New label' }).success).toBe(true);
  });

  it('validates null filter (to clear)', () => {
    expect(UpdateRetentionPolicySchema.safeParse({ filter: null }).success).toBe(true);
  });
});

describe('getCutoffDate', () => {
  it('returns date in the past', () => {
    const cutoff = getCutoffDate(30);
    const cutoffDate = new Date(cutoff);
    expect(cutoffDate.getTime()).toBeLessThan(Date.now());
  });

  it('returns approximately N days ago', () => {
    const days = 90;
    const cutoff = getCutoffDate(days);
    const diff = Date.now() - new Date(cutoff).getTime();
    const diffDays = diff / (1000 * 60 * 60 * 24);
    expect(diffDays).toBeGreaterThanOrEqual(89.9);
    expect(diffDays).toBeLessThanOrEqual(90.1);
  });

  it('returns ISO string format', () => {
    const cutoff = getCutoffDate(7);
    expect(cutoff).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/);
  });
});
