import { describe, it, expect } from 'vitest';
import { LeagueCardViewModel } from './LeagueCardViewModel';
import type { LeagueSummaryDTO } from '@/lib/types/generated/LeagueSummaryDTO';

const createSummaryDto = (overrides: Partial<LeagueSummaryDTO & { description?: string }> = {}): LeagueSummaryDTO & { description?: string } => ({
  id: 'league-1',
  name: 'Test League',
  description: 'Custom description',
  ...overrides,
});

describe('LeagueCardViewModel', () => {
  it('maps id, name and description from simple DTO', () => {
    const dto = { id: 'simple-id', name: 'Simple League', description: 'Simple desc' };

    const vm = new LeagueCardViewModel(dto);

    expect(vm.id).toBe('simple-id');
    expect(vm.name).toBe('Simple League');
    expect(vm.description).toBe('Simple desc');
  });

  it('supports LeagueSummaryDTO without description and applies default description', () => {
    const dto: LeagueSummaryDTO = { id: 'sum-1', name: 'Summary League' };

    const vm = new LeagueCardViewModel(dto);

    expect(vm.id).toBe('sum-1');
    expect(vm.name).toBe('Summary League');
    expect(vm.description).toBe('Competitive iRacing league');
  });

  it('prefers provided description when using LeagueSummaryDTO with extra description', () => {
    const dto = createSummaryDto({ id: 'sum-2', name: 'Summary With Description', description: 'Provided description' });

    const vm = new LeagueCardViewModel(dto);

    expect(vm.id).toBe('sum-2');
    expect(vm.name).toBe('Summary With Description');
    expect(vm.description).toBe('Provided description');
  });

  it('falls back to default description when description is undefined', () => {
    const dto = createSummaryDto({ description: undefined });

    const vm = new LeagueCardViewModel(dto);

    expect(vm.description).toBe('Competitive iRacing league');
  });
});
