import { logError } from '@/utils/errorHandler';
import Config from '@/config/config';
const apiServer = Config.apiServer;

async function findUser(jwt: string, userId: number) {
  const fetchOptions = {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${jwt}`,
    },
  };

  const url = `${apiServer}/users/find/${userId}`;

  try {
    const response = await fetch(url, fetchOptions);
    if (!response.ok) {
      throw new Error(`HTTP error! Status: ${response.status}`);
    }
    const data = await response.json();
    return data;
  } catch (error: unknown) {
    logError('Error fetching data:', error);
  }
}

async function searchUsers(jwt: string, projectId: number, searchText: string) {
  const fetchOptions = {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${jwt}`,
    },
  };

  const url = `${apiServer}/users/search?projectId=${projectId}&search=${searchText}`;

  try {
    const response = await fetch(url, fetchOptions);
    if (!response.ok) {
      throw new Error(`HTTP error! Status: ${response.status}`);
    }
    const data = await response.json();
    return data;
  } catch (error: unknown) {
    logError('Error fetching data:', error);
  }
}

async function updateUserRole(jwt: string, userId: number, newRole: number) {
  const updateUserData = {
    newRole,
  };

  const fetchOptions = {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${jwt}`,
    },
    body: JSON.stringify(updateUserData),
  };

  const url = `${apiServer}/users/${userId}/role`;

  try {
    const response = await fetch(url, fetchOptions);
    if (!response.ok) {
      throw new Error(`HTTP error! Status: ${response.status}`);
    }
    const data = await response.json();
    return data;
  } catch (error: unknown) {
    logError('Error fetching data:', error);
  }
}

async function updateUsername(jwt: string, username: string) {
  const updateData = {
    username,
  };

  const fetchOptions = {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${jwt}`,
    },
    body: JSON.stringify(updateData),
  };

  const url = `${apiServer}/users/username`;

  try {
    const response = await fetch(url, fetchOptions);
    if (!response.ok) {
      const errorText = await response.text();
      throw new Error(errorText || `HTTP error! Status: ${response.status}`);
    }
    const data = await response.json();
    return data;
  } catch (error: unknown) {
    logError('Error updating username:', error);
    throw error;
  }
}

async function updatePassword(jwt: string, currentPassword: string, newPassword: string) {
  const updateData = {
    currentPassword,
    newPassword,
  };

  const fetchOptions = {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${jwt}`,
    },
    body: JSON.stringify(updateData),
  };

  const url = `${apiServer}/users/password`;

  try {
    const response = await fetch(url, fetchOptions);
    if (!response.ok) {
      const errorText = await response.text();
      throw new Error(errorText || `HTTP error! Status: ${response.status}`);
    }
    const data = await response.json();
    return data;
  } catch (error: unknown) {
    logError('Error updating password:', error);
    throw error;
  }
}

async function uploadAvatar(jwt: string, file: File) {
  const formData = new FormData();
  formData.append('avatar', file);

  const fetchOptions = {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${jwt}`,
    },
    body: formData,
  };

  const url = `${apiServer}/users/avatar`;

  try {
    const response = await fetch(url, fetchOptions);
    if (!response.ok) {
      const errorText = await response.text();
      throw new Error(errorText || `HTTP error! Status: ${response.status}`);
    }
    const data = await response.json();
    return data;
  } catch (error: unknown) {
    logError('Error uploading avatar:', error);
    throw error;
  }
}

async function deleteAvatar(jwt: string) {
  const fetchOptions = {
    method: 'DELETE',
    headers: {
      Authorization: `Bearer ${jwt}`,
    },
  };

  const url = `${apiServer}/users/avatar`;

  try {
    const response = await fetch(url, fetchOptions);
    if (!response.ok) {
      const errorText = await response.text();
      throw new Error(errorText || `HTTP error! Status: ${response.status}`);
    }
    const data = await response.json();
    return data;
  } catch (error: unknown) {
    logError('Error deleting avatar:', error);
    throw error;
  }
}

async function adminResetPassword(jwt: string, userId: number, newPassword: string) {
  const updateData = {
    newPassword,
  };

  const fetchOptions = {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${jwt}`,
    },
    body: JSON.stringify(updateData),
  };

  const url = `${apiServer}/users/${userId}/password`;

  try {
    const response = await fetch(url, fetchOptions);
    if (!response.ok) {
      const errorText = await response.text();
      throw new Error(errorText || `HTTP error! Status: ${response.status}`);
    }
    const data = await response.json();
    return data;
  } catch (error: unknown) {
    logError('Error admin resetting password:', error);
    throw error;
  }
}

export {
  findUser,
  searchUsers,
  updateUserRole,
  updateUsername,
  updatePassword,
  uploadAvatar,
  deleteAvatar,
  adminResetPassword,
};
