import { describe, it, expect } from 'vitest';
import { LeagueMembershipsViewModel } from './LeagueMembershipsViewModel';
import { LeagueMemberViewModel } from './LeagueMemberViewModel';
import type { LeagueMemberDTO } from '../types/generated/LeagueMemberDTO';

const createMembershipDto = (overrides: Partial<LeagueMemberDTO> = {}): LeagueMemberDTO => ({
  driverId: 'driver-1',
  leagueId: 'league-1',
  joinedAt: '2024-01-01T00:00:00Z',
  role: 'member',
  ...overrides,
});

describe('LeagueMembershipsViewModel', () => {
  it('maps memberships into LeagueMemberViewModel instances', () => {
    const dto = {
      memberships: [
        createMembershipDto({ driverId: 'driver-1' }),
        createMembershipDto({ driverId: 'driver-2' }),
      ],
    };

    const viewModel = new LeagueMembershipsViewModel(dto, 'driver-1');

    expect(viewModel.memberships).toHaveLength(2);
    expect(viewModel.memberships[0]).toBeInstanceOf(LeagueMemberViewModel);
    expect(viewModel.memberships[0].driverId).toBe('driver-1');
    expect(viewModel.memberships[1].driverId).toBe('driver-2');
  });

  it('computes memberCount and hasMembers correctly when there are members', () => {
    const dto = {
      memberships: [
        createMembershipDto({ driverId: 'driver-1' }),
        createMembershipDto({ driverId: 'driver-2' }),
      ],
    };

    const viewModel = new LeagueMembershipsViewModel(dto, 'driver-1');

    expect(viewModel.memberCount).toBe(2);
    expect(viewModel.hasMembers).toBe(true);
  });

  it('computes memberCount and hasMembers correctly when empty', () => {
    const dto = {
      memberships: [] as LeagueMemberDTO[],
    };

    const viewModel = new LeagueMembershipsViewModel(dto, 'driver-1');

    expect(viewModel.memberCount).toBe(0);
    expect(viewModel.hasMembers).toBe(false);
  });
});
