import { apiClient } from './apiClient';

export const userService = {
  async changePassword(currentPassword: string, newPassword: string): Promise<{ message: string }> {
    const { data } = await apiClient.put('/users/password', {
      currentPassword,
      newPassword,
    });
    return data;
  },

  async getUsers(page?: number, limit?: number): Promise<any> {
    const params = new URLSearchParams();
    if (page) params.append('page', page.toString());
    if (limit) params.append('limit', limit.toString());
    const { data } = await apiClient.get(`/users?${params.toString()}`);
    return data;
  },

  async updateUser(userId: string, isAdmin: boolean): Promise<{ message: string }> {
    const { data } = await apiClient.put(`/users/${userId}`, {
      isAdmin,
    });
    return data;
  },

  async updateUserInfo(userId: string, userData: { username?: string; isAdmin?: boolean; password?: string; displayName?: string }): Promise<{ message: string }> {
    const { data } = await apiClient.put(`/users/${userId}`, userData);
    return data;
  },

  async deleteUser(userId: string): Promise<{ message: string }> {
    const { data } = await apiClient.delete(`/users/${userId}`);
    return data;
  },

  async createUser(username: string, password: string, isAdmin: boolean = false, tenantId?: string, displayName?: string): Promise<{ message: string }> {
    const { data } = await apiClient.post('/users', {
      username,
      password,
      isAdmin,
      tenantId,
      displayName,
    });
    return data;
  },
  
  async exportUsers(): Promise<Blob> {
    return await apiClient.getBlob('/v1/admin/users/export');
  },

  async importUsers(file: File): Promise<any> {
    const formData = new FormData();
    formData.append('file', file);
    const { data } = await apiClient.postMultipart('/v1/admin/users/import', formData);
    return data;
  },
};
