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

const createTeamJoinRequestDto = (overrides: Partial<TeamJoinRequestDTO> = {}): TeamJoinRequestDTO => ({
  requestId: 'request-1',
  teamId: 'team-1',
  driverId: 'driver-1',
  driverName: 'Driver One',
  status: 'pending',
  requestedAt: '2024-01-01T12:00:00Z',
  avatarUrl: 'https://example.com/avatar.jpg',
  ...overrides,
});

describe('TeamJoinRequestViewModel', () => {
  it('maps fields from DTO', () => {
    const dto = createTeamJoinRequestDto({ requestId: 'req-123', driverId: 'driver-123' });

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

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

  it('allows approval only for owners', () => {
    const dto = createTeamJoinRequestDto();

    const ownerVm = new TeamJoinRequestViewModel(dto, 'owner-user', true);
    const nonOwnerVm = new TeamJoinRequestViewModel(dto, 'regular-user', false);

    expect(ownerVm.canApprove).toBe(true);
    expect(nonOwnerVm.canApprove).toBe(false);
  });

  it('exposes a pending status with yellow color', () => {
    const dto = createTeamJoinRequestDto({ status: 'pending' });
    const vm = new TeamJoinRequestViewModel(dto, 'owner-user', true);

    expect(vm.status).toBe('Pending');
    expect(vm.statusColor).toBe('yellow');
  });

  it('provides approve and reject button labels', () => {
    const dto = createTeamJoinRequestDto();
    const vm = new TeamJoinRequestViewModel(dto, 'owner-user', true);

    expect(vm.approveButtonText).toBe('Approve');
    expect(vm.rejectButtonText).toBe('Reject');
  });

  it('formats requestedAt as localized date-time', () => {
    const dto = createTeamJoinRequestDto({ requestedAt: '2024-01-01T12:00:00Z' });
    const vm = new TeamJoinRequestViewModel(dto, 'owner-user', true);

    const formatted = vm.formattedRequestedAt;

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