import type { TeamMemberDTO } from '@/lib/types/generated/TeamMemberDTO';

type TeamMemberRole = 'owner' | 'manager' | 'member';

function normalizeTeamRole(role: string): TeamMemberRole {
  if (role === 'owner' || role === 'manager' || role === 'member') return role;
  // Backwards compatibility
  if (role === 'admin') return 'manager';
  return 'member';
}

export class TeamMemberViewModel {
  driverId: string;
  driverName: string;
  role: TeamMemberRole;
  joinedAt: string;
  isActive: boolean;
  avatarUrl: string;

  private currentUserId: string;
  private teamOwnerId: string;

  constructor(dto: TeamMemberDTO, currentUserId: string, teamOwnerId: string) {
    this.driverId = dto.driverId;
    this.driverName = dto.driverName;
    this.role = normalizeTeamRole(dto.role);
    this.joinedAt = dto.joinedAt;
    this.isActive = dto.isActive;
    this.avatarUrl = dto.avatarUrl || '';
    this.currentUserId = currentUserId;
    this.teamOwnerId = teamOwnerId;
  }

  /** UI-specific: Role badge variant */
  get roleBadgeVariant(): string {
    switch (this.role) {
      case 'owner': return 'primary';
      case 'manager': return 'secondary';
      case 'member': return 'default';
      default: return 'default';
    }
  }

  /** UI-specific: Whether this member is the owner */
  get isOwner(): boolean {
    return this.driverId === this.teamOwnerId;
  }

  /** UI-specific: Whether current user can manage this member */
  get canManage(): boolean {
    return this.currentUserId === this.teamOwnerId && this.driverId !== this.currentUserId;
  }

  /** UI-specific: Whether this is the current user */
  get isCurrentUser(): boolean {
    return this.driverId === this.currentUserId;
  }

  /** UI-specific: Formatted joined date */
  get formattedJoinedAt(): string {
    return new Date(this.joinedAt).toLocaleDateString();
  }
}
