import { describe, it, expect } from 'vitest';
import { MembershipFeeViewModel } from './MembershipFeeViewModel';
import type { MembershipFeeDto } from '../types/generated';

const createMembershipFeeDto = (overrides: Partial<MembershipFeeDto> = {}): MembershipFeeDto => ({
  id: 'fee-1',
  leagueId: 'league-1',
  seasonId: 'season-1',
  type: 'season',
  amount: 49.99,
  enabled: true,
  createdAt: new Date('2024-01-01T10:00:00Z'),
  updatedAt: new Date('2024-01-02T10:00:00Z'),
  ...overrides,
});

describe('MembershipFeeViewModel', () => {
  it('maps fields from DTO', () => {
    const dto = createMembershipFeeDto({ id: 'custom-fee', amount: 19.5 });

    const vm = new MembershipFeeViewModel(dto);

    expect(vm.id).toBe('custom-fee');
    expect(vm.leagueId).toBe('league-1');
    expect(vm.seasonId).toBe('season-1');
    expect(vm.amount).toBe(19.5);
    expect(vm.enabled).toBe(true);
  });

  it('formats amount with euro currency and two decimals', () => {
    const dto = createMembershipFeeDto({ amount: 10 });
    const vm = new MembershipFeeViewModel(dto);

    expect(vm.formattedAmount).toBe('€10.00');
  });

  it('maps type to human-readable display', () => {
    const seasonVm = new MembershipFeeViewModel(createMembershipFeeDto({ type: 'season' }));
    const monthlyVm = new MembershipFeeViewModel(createMembershipFeeDto({ type: 'monthly' }));
    const perRaceVm = new MembershipFeeViewModel(createMembershipFeeDto({ type: 'per_race' }));
    const otherVm = new MembershipFeeViewModel(createMembershipFeeDto({ type: 'custom' as any }));

    expect(seasonVm.typeDisplay).toBe('Per Season');
    expect(monthlyVm.typeDisplay).toBe('Monthly');
    expect(perRaceVm.typeDisplay).toBe('Per Race');
    expect(otherVm.typeDisplay).toBe('custom');
  });

  it('derives statusDisplay and statusColor from enabled flag', () => {
    const enabledVm = new MembershipFeeViewModel(createMembershipFeeDto({ enabled: true }));
    const disabledVm = new MembershipFeeViewModel(createMembershipFeeDto({ enabled: false }));

    expect(enabledVm.statusDisplay).toBe('Enabled');
    expect(enabledVm.statusColor).toBe('green');

    expect(disabledVm.statusDisplay).toBe('Disabled');
    expect(disabledVm.statusColor).toBe('red');
  });

  it('formats createdAt and updatedAt as localized strings', () => {
    const dto = createMembershipFeeDto({
      createdAt: new Date('2024-01-01T10:00:00Z'),
      updatedAt: new Date('2024-01-02T10:00:00Z'),
    });

    const vm = new MembershipFeeViewModel(dto);

    expect(typeof vm.formattedCreatedAt).toBe('string');
    expect(vm.formattedCreatedAt.length).toBeGreaterThan(0);

    expect(typeof vm.formattedUpdatedAt).toBe('string');
    expect(vm.formattedUpdatedAt.length).toBeGreaterThan(0);
  });
});
