import { describe, it, expect } from 'vitest';
import { SponsorSponsorshipsViewModel } from './SponsorSponsorshipsViewModel';
import { SponsorshipDetailViewModel } from './SponsorshipDetailViewModel';

describe('SponsorSponsorshipsViewModel', () => {
  const dto = {
    sponsorId: 'sp-1',
    sponsorName: 'Acme Corp',
  } as any;

  const makeDetail = (overrides: Partial<SponsorshipDetailViewModel> = {}) => {
    const base = new SponsorshipDetailViewModel({
      id: 'detail-1',
      leagueId: 'league-1',
      leagueName: 'Pro League',
      seasonId: 'season-1',
      seasonName: 'Season 1',
    } as any);
    Object.assign(base, overrides);
    return base;
  };

  it('maps core sponsor identifiers from DTO', () => {
    const vm = new SponsorSponsorshipsViewModel(dto);

    expect(vm.sponsorId).toBe(dto.sponsorId);
    expect(vm.sponsorName).toBe(dto.sponsorName);
  });

  it('derives counts and investment summary from attached sponsorships', () => {
    const vm = new SponsorSponsorshipsViewModel(dto);
    vm.sponsorships = [
      makeDetail({ id: 'd1', amount: 2_000, currency: 'USD', status: 'active' }),
      makeDetail({ id: 'd2', amount: 1_000, currency: 'USD', status: 'expired' }),
      makeDetail({ id: 'd3', amount: 3_000, currency: 'USD', status: 'active' }),
    ];

    expect(vm.totalCount).toBe(3);
    expect(vm.activeSponsorships).toHaveLength(2);
    expect(vm.activeCount).toBe(2);
    expect(vm.hasSponsorships).toBe(true);

    const expectedInvestment = vm.sponsorships.reduce((sum, s) => sum + s.amount, 0);
    expect(vm.totalInvestment).toBe(expectedInvestment);
    expect(vm.formattedTotalInvestment).toBe(`USD ${expectedInvestment.toLocaleString()}`);
  });

  it('handles empty sponsorships gracefully', () => {
    const vm = new SponsorSponsorshipsViewModel(dto);

    expect(vm.totalCount).toBe(0);
    expect(vm.activeSponsorships).toEqual([]);
    expect(vm.activeCount).toBe(0);
    expect(vm.hasSponsorships).toBe(false);
    expect(vm.totalInvestment).toBe(0);
    expect(vm.formattedTotalInvestment).toBe('USD 0');
  });
});
