import { Injectable } from '@angular/core';
import type { } from '../types/telegram.types';

@Injectable({
  providedIn: 'root'
})
export class TelegramService {
  private tg = typeof window !== 'undefined' ? window.Telegram?.WebApp : null;

  isTelegramApp(): boolean {
    return !!this.tg;
  }

  getUser() {
    return this.tg?.initDataUnsafe?.user || null;
  }

  getUserId(): number | null {
    return this.tg?.initDataUnsafe?.user?.id || null;
  }

  getUsername(): string | null {
    return this.tg?.initDataUnsafe?.user?.username || null;
  }

  getFirstName(): string | null {
    return this.tg?.initDataUnsafe?.user?.first_name || null;
  }

  getFullName(): string | null {
    const user = this.getUser();
    if (!user) return null;
    return `${user.first_name}${user.last_name ? ' ' + user.last_name : ''}`;
  }

  getDisplayName(): string {
    return this.getUsername() || this.getFullName() || 'User';
  }
}
