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

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

describe('LeagueMemberViewModel', () => {
  it('maps basic fields from DTO and sets defaults', () => {
    const dto = createLeagueMemberDto({ driverId: 'driver-123' });

    const vm = new LeagueMemberViewModel(dto, 'current-user');

    expect(vm.driverId).toBe('driver-123');
    expect(vm.role).toBe('member');
    expect(typeof vm.joinedAt).toBe('string');
  });

  it('computes formattedJoinedAt as a localized date string', () => {
    const dto = createLeagueMemberDto({ joinedAt: '2024-01-01T00:00:00Z' });
    const vm = new LeagueMemberViewModel(dto, 'current-user');

    const formatted = vm.formattedJoinedAt;

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

  it('derives roleBadgeVariant based on current role value', () => {
    const vm = new LeagueMemberViewModel(createLeagueMemberDto(), 'current-user');

    vm.role = 'owner';
    expect(vm.roleBadgeVariant).toBe('primary');

    vm.role = 'admin';
    expect(vm.roleBadgeVariant).toBe('secondary');

    vm.role = 'member';
    expect(vm.roleBadgeVariant).toBe('default');

    vm.role = 'something-else' as any;
    expect(vm.roleBadgeVariant).toBe('default');
  });

  it('identifies owner and current user correctly based on role and driverId', () => {
    const dto = createLeagueMemberDto({ driverId: 'driver-1' });

    const vmForOwner = new LeagueMemberViewModel(dto, 'driver-1');
    vmForOwner.role = 'owner';

    const vmForAnotherUser = new LeagueMemberViewModel(dto, 'driver-2');
    vmForAnotherUser.role = 'owner';

    expect(vmForOwner.isOwner).toBe(true);
    expect(vmForOwner.isCurrentUser).toBe(true);

    expect(vmForAnotherUser.isOwner).toBe(true);
    expect(vmForAnotherUser.isCurrentUser).toBe(false);
  });
});
