import { httpClient } from '../../../core/infrastructure/httpClient';
import type { Chat, Message } from '../../../core/domain/types';

export class ChatApi {
  static async getChats() {
    return httpClient.request<Chat[]>('/chats');
  }

  static async createPersonalChat(userId: string) {
    return httpClient.request<Chat>('/chats/personal', {
      method: 'POST',
      body: JSON.stringify({ userId }),
    });
  }

  static async createGroupChat(name: string, memberIds: string[]) {
    return httpClient.request<Chat>('/chats/group', {
      method: 'POST',
      body: JSON.stringify({ name, memberIds }),
    });
  }

  static async getMessages(chatId: string, cursor?: string, pivot?: number, limit?: number) {
    const params = new URLSearchParams();
    if (cursor) params.append('cursor', cursor);
    if (pivot) params.append('pivot', pivot.toString());
    if (limit) params.append('limit', limit.toString());
    const query = params.toString() ? `?${params.toString()}` : '';
    return httpClient.request<Message[]>(`/messages/chat/${chatId}${query}`);
  }

  static async uploadFile(file: File) {
    const formData = new FormData();
    formData.append('file', file);
    return httpClient.request<{ url: string; filename: string; size: number }>('/messages/upload', {
      method: 'POST',
      body: formData,
      timeout: 120_000,
    });
  }

  static async updateGroup(chatId: string, data: { name?: string; description?: string }) {
    return httpClient.request<Chat>(`/chats/${chatId}`, {
      method: 'PUT',
      body: JSON.stringify(data),
    });
  }

  static async uploadGroupAvatar(chatId: string, file: File) {
    const formData = new FormData();
    formData.append('avatar', file);
    return httpClient.request<Chat>(`/chats/${chatId}/avatar`, {
      method: 'POST',
      body: formData,
      timeout: 120_000,
    });
  }

  static async cropGroupAvatar(chatId: string, file: File, cropData: { x: number; y: number; width: number; height: number }) {
    const formData = new FormData();
    formData.append('avatar', file);
    formData.append('x', cropData.x.toString());
    formData.append('y', cropData.y.toString());
    formData.append('width', cropData.width.toString());
    formData.append('height', cropData.height.toString());

    return httpClient.request<Chat>(`/chats/${chatId}/avatar/crop`, {
      method: 'POST',
      body: formData,
      timeout: 120_000,
    });
  }

  static async removeGroupAvatar(chatId: string) {
    return httpClient.request<Chat>(`/chats/${chatId}/avatar`, { method: 'DELETE' });
  }

  static async addGroupMembers(chatId: string, userIds: string[]) {
    return httpClient.request<Chat>(`/chats/${chatId}/members`, {
      method: 'POST',
      body: JSON.stringify({ userIds }),
    });
  }

  static async removeGroupMember(chatId: string, userId: string) {
    return httpClient.request<Chat>(`/chats/${chatId}/members/${userId}`, {
      method: 'DELETE',
    });
  }

  static async clearChat(chatId: string) {
    return httpClient.request<{ message: string }>(`/chats/${chatId}/clear`, { method: 'POST' });
  }

  static async deleteChat(chatId: string) {
    return httpClient.request<{ message: string }>(`/chats/${chatId}`, { method: 'DELETE' });
  }

  static async togglePinChat(chatId: string) {
    return httpClient.request<{ isPinned: boolean }>(`/chats/${chatId}/pin`, { method: 'POST' });
  }

  static async searchMessages(query: string, chatId?: string) {
    const params = new URLSearchParams({ q: query });
    if (chatId) params.append('chatId', chatId);
    return httpClient.request<Message[]>(`/messages/search?${params}`);
  }

  static async getSharedMedia(chatId: string, type: 'media' | 'gifs' | 'files' | 'links') {
    return httpClient.request<any[]>(`/messages/chat/${chatId}/shared?type=${type}`);
  }

  static async getOrCreateFavorites() {
    return httpClient.request<Chat>('/chats/favorites', { method: 'POST' });
  }
}
