import { AuthenticatedUserDTO } from '@/lib/types/generated/AuthenticatedUserDTO';

export class SessionViewModel {
  userId: string;
  email: string;
  displayName: string;
  avatarUrl?: string | null;
  role?: string;
  driverId?: string;
  isAuthenticated: boolean = true;

  constructor(dto: AuthenticatedUserDTO) {
    this.userId = dto.userId;
    this.email = dto.email;
    this.displayName = dto.displayName;

    // Use the optional fields from the DTO
    if (dto.primaryDriverId) {
      this.driverId = dto.primaryDriverId;
    }
    
    if (dto.avatarUrl !== undefined) {
      this.avatarUrl = dto.avatarUrl;
    }
    
    if (dto.role) {
      this.role = dto.role;
    }
  }

  /**
   * Compatibility accessor.
   * Some legacy components expect `session.user.*`.
   */
  get user(): {
    userId: string;
    email: string;
    displayName: string;
    primaryDriverId?: string | null;
    avatarUrl?: string | null;
  } {
    return {
      userId: this.userId,
      email: this.email,
      displayName: this.displayName,
      primaryDriverId: this.driverId ?? null,
      avatarUrl: this.avatarUrl,
    };
  }

  /** UI-specific: User greeting */
  get greeting(): string {
    return `Hello, ${this.displayName}!`;
  }

  /** UI-specific: Avatar initials */
  get avatarInitials(): string {
    if (this.displayName) {
      return this.displayName.split(' ').map(n => n[0]).join('').toUpperCase();
    }
    return (this.email?.[0] ?? '?').toUpperCase();
  }

  /** UI-specific: Whether has driver profile */
  get hasDriverProfile(): boolean {
    return !!this.driverId;
  }

  /** UI-specific: Authentication status display */
  get authStatusDisplay(): string {
    return this.isAuthenticated ? 'Logged In' : 'Logged Out';
  }
}