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

const createLeagueJoinRequestDto = (overrides: Partial<LeagueJoinRequestDTO> = {}): LeagueJoinRequestDTO => ({
  id: 'request-1',
  leagueId: 'league-1',
  driverId: 'driver-1',
  requestedAt: '2024-01-01T12:00:00Z',
  ...overrides,
});

describe('LeagueJoinRequestViewModel', () => {
  it('maps fields from DTO', () => {
    const dto = createLeagueJoinRequestDto({ id: 'req-123', driverId: 'driver-123' });

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

    expect(vm.id).toBe('req-123');
    expect(vm.leagueId).toBe('league-1');
    expect(vm.driverId).toBe('driver-123');
    expect(vm.requestedAt).toBe('2024-01-01T12:00:00Z');
  });

  it('formats requestedAt as a localized date-time string', () => {
    const dto = createLeagueJoinRequestDto({ requestedAt: '2024-01-01T12:00:00Z' });
    const vm = new LeagueJoinRequestViewModel(dto, 'current-user', true);

    const formatted = vm.formattedRequestedAt;

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

  it('allows approval and rejection only for admins', () => {
    const dto = createLeagueJoinRequestDto();

    const adminVm = new LeagueJoinRequestViewModel(dto, 'admin-user', true);
    const nonAdminVm = new LeagueJoinRequestViewModel(dto, 'regular-user', false);

    expect(adminVm.canApprove).toBe(true);
    expect(adminVm.canReject).toBe(true);

    expect(nonAdminVm.canApprove).toBe(false);
    expect(nonAdminVm.canReject).toBe(false);
  });
});
