import { AuthenticatedUserDTO } from '../types/generated/AuthenticatedUserDTO';

export class SessionViewModel {
  userId: string;
  email: string;
  displayName: string;
  avatarUrl?: string | null;

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

    const anyDto = dto as unknown as { primaryDriverId?: unknown; driverId?: unknown; avatarUrl?: unknown };
    if (typeof anyDto.primaryDriverId === 'string' && anyDto.primaryDriverId) {
      this.driverId = anyDto.primaryDriverId;
    } else if (typeof anyDto.driverId === 'string' && anyDto.driverId) {
      this.driverId = anyDto.driverId;
    }
    if (anyDto.avatarUrl !== undefined) {
      this.avatarUrl = anyDto.avatarUrl as string | null;
    }
  }

  // Note: The generated DTO doesn't have these fields
  // These will need to be added when the OpenAPI spec is updated
  driverId?: string;
  isAuthenticated: boolean = true;

  /**
   * 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';
  }
}
