import { HttpClient } from '@/clients/base/http-client';
import { Folder } from '@/services/folders';

export interface CreateFolderData {
  name: string;
  tagId?: string;
  parentId?: string;
  order?: number;
}

export interface UpdateFolderData {
  name?: string;
  tagId?: string;
  parentId?: string;
  order?: number;
}

export interface FoldersResponse {
  folders: Folder[];
}

export interface FolderResponse {
  folder: Folder;
}

/**
 * Client HTTP pour les dossiers
 */
export class FoldersClient extends HttpClient {
  constructor() {
    super('/api/folders');
  }

  /**
   * Récupère tous les dossiers de l'utilisateur
   */
  async getFolders(): Promise<Folder[]> {
    const response = await this.get<FoldersResponse>('');
    return response.folders;
  }

  /**
   * Récupère un dossier par son ID
   */
  async getFolderById(id: string): Promise<Folder> {
    const response = await this.get<FolderResponse>(`/${id}`);
    return response.folder;
  }

  /**
   * Crée un nouveau dossier
   */
  async createFolder(data: CreateFolderData): Promise<Folder> {
    const response = await this.post<FolderResponse>('', data);
    return response.folder;
  }

  /**
   * Met à jour un dossier existant
   */
  async updateFolder(id: string, data: UpdateFolderData): Promise<Folder> {
    const response = await this.put<FolderResponse>(`/${id}`, data);
    return response.folder;
  }

  /**
   * Supprime un dossier
   */
  async deleteFolder(id: string): Promise<void> {
    await this.delete(`/${id}`);
  }

  /**
   * Réorganise l'ordre des dossiers
   */
  async reorderFolders(
    folderOrders: Array<{ id: string; order: number }>
  ): Promise<void> {
    await this.post('/reorder', { folderOrders });
  }
}

// Instance singleton
export const foldersClient = new FoldersClient();
