import { ReportEntity } from '../entities/report.entity';
import { ReportStatus } from '../enums/report-status.enum';
import { ReportType } from '../enums/report-type.enum';

describe('ReportEntity', () => {
  it('creates a new report in GENERATING status', () => {
    const report = ReportEntity.createNew(
      'rpt-1',
      'user-1',
      ReportType.INDUSTRIAL_LOCATION,
      'KCN Bình Dương',
      { province: 'Bình Dương' },
    );

    expect(report.id).toBe('rpt-1');
    expect(report.userId).toBe('user-1');
    expect(report.type).toBe(ReportType.INDUSTRIAL_LOCATION);
    expect(report.status).toBe(ReportStatus.GENERATING);
    expect(report.content).toBeNull();
    expect(report.pdfUrl).toBeNull();
    expect(report.errorMsg).toBeNull();
  });

  it('transitions to READY with content and PDF URL', () => {
    const report = ReportEntity.createNew(
      'rpt-2',
      'user-1',
      ReportType.RESIDENTIAL_MARKET,
      'Thị trường Q1 2026',
      { city: 'HCM' },
    );

    const content = { sections: { summary: { title: 'Tóm tắt', content: 'Test' } } };
    report.markReady(content, 'https://minio/reports/rpt-2.pdf');

    expect(report.status).toBe(ReportStatus.READY);
    expect(report.content).toEqual(content);
    expect(report.pdfUrl).toBe('https://minio/reports/rpt-2.pdf');
  });

  it('transitions to READY without PDF URL', () => {
    const report = ReportEntity.createNew(
      'rpt-3',
      'user-1',
      ReportType.DISTRICT_ANALYSIS,
      'Phân tích Quận 1',
      {},
    );

    report.markReady({ sections: {} }, null);

    expect(report.status).toBe(ReportStatus.READY);
    expect(report.pdfUrl).toBeNull();
  });

  it('transitions to FAILED with error message', () => {
    const report = ReportEntity.createNew(
      'rpt-4',
      'user-1',
      ReportType.PORTFOLIO,
      'Portfolio',
      {},
    );

    report.markFailed('Claude API timeout');

    expect(report.status).toBe(ReportStatus.FAILED);
    expect(report.errorMsg).toBe('Claude API timeout');
    expect(report.content).toBeNull();
  });
});
