import {
  CatalogAuditAction,
  CATALOG_AUDIT_MODULE,
  logCatalogAudit,
  summarizeOddsUpdates,
} from './catalog-audit';
import { AuditService } from './audit.service';

describe('catalog-audit', () => {
  it('uses CATALOG module and stable action codes', () => {
    expect(CATALOG_AUDIT_MODULE).toBe('CATALOG');
    expect(CatalogAuditAction.PUBLISH_MATCH).toBe('PUBLISH_MATCH');
    expect(CatalogAuditAction.UPDATE_MATCH_ODDS).toBe('UPDATE_MATCH_ODDS');
    expect(Object.keys(CatalogAuditAction).length).toBeGreaterThanOrEqual(30);
  });

  it('summarizeOddsUpdates truncates large batches', () => {
    const ids = Array.from({ length: 25 }, (_, i) => BigInt(i + 1));
    const summary = summarizeOddsUpdates(ids, 20);
    expect(summary.updatedCount).toBe(25);
    expect(summary.selectionIds).toHaveLength(20);
    expect(summary.selectionIdsTruncated).toBe(true);
    expect(summary.total).toBe(25);
  });

  it('summarizeOddsUpdates keeps full list when small', () => {
    const summary = summarizeOddsUpdates(['1', '2']);
    expect(summary.selectionIds).toEqual(['1', '2']);
    expect(summary.selectionIdsTruncated).toBeUndefined();
  });

  it('logCatalogAudit delegates to AuditService.log', async () => {
    const audit = {
      log: jest.fn().mockResolvedValue({ id: 1n }),
    } as unknown as AuditService;
    await logCatalogAudit(audit, {
      operatorId: 99n,
      action: CatalogAuditAction.PUBLISH_MATCH,
      targetId: '42',
      afterData: { status: 'PUBLISHED' },
    });
    expect(audit.log).toHaveBeenCalledWith({
      operatorId: 99n,
      operatorType: 'ADMIN',
      action: 'PUBLISH_MATCH',
      module: CATALOG_AUDIT_MODULE,
      targetId: '42',
      beforeData: undefined,
      afterData: { status: 'PUBLISHED' },
      ipAddress: undefined,
    });
  });
});
