import { describe, it, expect } from 'vitest';
import { CreateExportSchema } from './types.js';
import type { ExportJobDoc, ExportType, ExportFormat, ExportStatus } from './types.js';

describe('CreateExportSchema', () => {
  it('accepts valid export request', () => {
    const result = CreateExportSchema.safeParse({
      type: 'users',
      format: 'csv',
      filters: { plan: 'free' },
    });
    expect(result.success).toBe(true);
  });

  it('defaults format to csv', () => {
    const result = CreateExportSchema.safeParse({ type: 'audit' });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.format).toBe('csv');
      expect(result.data.filters).toEqual({});
    }
  });

  it('accepts all valid export types', () => {
    const types: ExportType[] = [
      'users',
      'audit',
      'telemetry',
      'usage',
      'subscriptions',
      'licenses',
    ];
    for (const type of types) {
      const result = CreateExportSchema.safeParse({ type });
      expect(result.success).toBe(true);
    }
  });

  it('accepts json format', () => {
    const result = CreateExportSchema.safeParse({ type: 'users', format: 'json' });
    expect(result.success).toBe(true);
  });

  it('rejects invalid export type', () => {
    const result = CreateExportSchema.safeParse({ type: 'invoices' });
    expect(result.success).toBe(false);
  });

  it('rejects invalid format', () => {
    const result = CreateExportSchema.safeParse({ type: 'users', format: 'xml' });
    expect(result.success).toBe(false);
  });
});

describe('ExportJobDoc type coverage', () => {
  it('should allow building a valid export job', () => {
    const now = new Date().toISOString();
    const job: ExportJobDoc = {
      id: 'exp_123',
      productId: 'lysnrai',
      type: 'users',
      format: 'csv',
      filters: { plan: 'pro' },
      status: 'pending',
      requestedBy: 'usr_admin',
      createdAt: now,
    };
    expect(job.status).toBe('pending');
    expect(job.blobUrl).toBeUndefined();
  });

  it('should support all export statuses', () => {
    const statuses: ExportStatus[] = ['pending', 'processing', 'ready', 'failed', 'expired'];
    expect(statuses.length).toBe(5);
  });

  it('should support all export formats', () => {
    const formats: ExportFormat[] = ['csv', 'json'];
    expect(formats.length).toBe(2);
  });

  it('should allow completed job with blob URL', () => {
    const job: ExportJobDoc = {
      id: 'exp_456',
      productId: 'lysnrai',
      type: 'audit',
      format: 'json',
      filters: {},
      status: 'ready',
      requestedBy: 'usr_admin',
      blobUrl: 'https://blob.core.windows.net/exports/exp_456.json?sas=token',
      fileName: 'audit-export-2026-03-15.json',
      rowCount: 1500,
      fileSizeBytes: 245000,
      startedAt: '2026-03-15T02:00:00Z',
      completedAt: '2026-03-15T02:00:05Z',
      expiresAt: '2026-03-22T02:00:00Z',
      createdAt: '2026-03-15T02:00:00Z',
    };
    expect(job.blobUrl).toContain('exports');
    expect(job.rowCount).toBe(1500);
  });
});
