/**
 * API для Telegram интеграции (связывание аккаунта, статус, бот).
 */
import apiClient from '@/lib/api-client';

export interface TelegramLinkResponse {
  success: boolean;
  code?: string;
  message?: string;
  instructions?: string;
  error?: string;
}

export interface TelegramStatusResponse {
  success: boolean;
  linked: boolean;
  telegram_id: number | null;
  telegram_username: string;
  notifications_enabled: boolean;
}

export interface TelegramBotInfo {
  success: boolean;
  username: string;
  first_name: string;
  id: number;
  link: string;
}

export async function generateTelegramCode(): Promise<TelegramLinkResponse> {
  const response = await apiClient.post<TelegramLinkResponse>(
    '/notifications/preferences/telegram/generate-code/'
  );
  return response.data ?? response;
}

export async function unlinkTelegram(): Promise<TelegramLinkResponse> {
  const response = await apiClient.post<TelegramLinkResponse>(
    '/notifications/preferences/telegram/unlink/'
  );
  return response.data ?? response;
}

export async function getTelegramStatus(): Promise<TelegramStatusResponse> {
  const response = await apiClient.get<TelegramStatusResponse>(
    '/notifications/preferences/telegram/status/'
  );
  const data = response.data as any;
  return data?.data ?? data ?? response.data;
}

export async function getTelegramBotInfo(): Promise<TelegramBotInfo> {
  const response = await apiClient.get<TelegramBotInfo>(
    '/notifications/preferences/telegram/bot-info/'
  );
  const data = response.data as any;
  return data?.data ?? data ?? response.data;
}
