import { describe, it, expect } from 'vitest';
import { TeamMemberViewModel } from './TeamMemberViewModel';
import type { TeamMemberDTO } from '@/lib/types/generated/GetTeamMembersOutputDTO';

const createTeamMemberDto = (overrides: Partial<TeamMemberDTO> = {}): TeamMemberDTO => ({
  driverId: 'driver-1',
  driverName: 'Test Driver',
  role: 'member',
  joinedAt: '2024-01-01T00:00:00Z',
  isActive: true,
  avatarUrl: 'https://example.com/avatar.png',
  ...overrides,
});

describe('TeamMemberViewModel', () => {
  it('maps fields from DTO', () => {
    const dto = createTeamMemberDto({ driverId: 'driver-123', driverName: 'Driver 123', role: 'owner' });

    const vm = new TeamMemberViewModel(dto, 'current-user', 'owner-1');

    expect(vm.driverId).toBe('driver-123');
    expect(vm.driverName).toBe('Driver 123');
    expect(vm.role).toBe('owner');
    expect(vm.joinedAt).toBe('2024-01-01T00:00:00Z');
    expect(vm.isActive).toBe(true);
    expect(vm.avatarUrl).toBe('https://example.com/avatar.png');
  });

  it('derives roleBadgeVariant based on role', () => {
    const ownerVm = new TeamMemberViewModel(createTeamMemberDto({ role: 'owner' }), 'current-user', 'owner-1');
    const managerVm = new TeamMemberViewModel(createTeamMemberDto({ role: 'manager' }), 'current-user', 'owner-1');
    const memberVm = new TeamMemberViewModel(createTeamMemberDto({ role: 'member' }), 'current-user', 'owner-1');

    expect(ownerVm.roleBadgeVariant).toBe('primary');
    expect(managerVm.roleBadgeVariant).toBe('secondary');
    expect(memberVm.roleBadgeVariant).toBe('default');
  });

  it('identifies owner correctly based on teamOwnerId', () => {
    const dto = createTeamMemberDto({ driverId: 'owner-1', role: 'owner' });

    const ownerVm = new TeamMemberViewModel(dto, 'some-user', 'owner-1');
    const nonOwnerVm = new TeamMemberViewModel(dto, 'some-user', 'another-owner');

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

  it('determines canManage only for team owner and non-self members', () => {
    const memberDto = createTeamMemberDto({ driverId: 'member-1' });

    const ownerManagingMember = new TeamMemberViewModel(memberDto, 'owner-1', 'owner-1');
    const ownerSelf = new TeamMemberViewModel(createTeamMemberDto({ driverId: 'owner-1' }), 'owner-1', 'owner-1');
    const nonOwner = new TeamMemberViewModel(memberDto, 'another-user', 'owner-1');

    expect(ownerManagingMember.canManage).toBe(true);
    expect(ownerSelf.canManage).toBe(false);
    expect(nonOwner.canManage).toBe(false);
  });

  it('identifies current user correctly', () => {
    const dto = createTeamMemberDto({ driverId: 'current-user' });

    const vm = new TeamMemberViewModel(dto, 'current-user', 'owner-1');

    expect(vm.isCurrentUser).toBe(true);
  });

  it('formats joinedAt as a localized date string', () => {
    const dto = createTeamMemberDto({ joinedAt: '2024-01-01T00:00:00Z' });
    const vm = new TeamMemberViewModel(dto, 'current-user', 'owner-1');

    const formatted = vm.formattedJoinedAt;

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