import { IHttpResponse, IObject } from "@/types/interface";
import http from "../utils/http"; // Assuming http is a function that takes a config object and returns a Promise

/**
 * `baseService` provides a standardized interface for common CRUD operations
 * by wrapping the core `http` utility. It ensures consistent request configuration
 * and handles Promise outcomes for various HTTP methods.
 */
export default {
  /**
   * Performs a **DELETE** HTTP request.
   * While DELETE typically uses path parameters, this method allows for an optional
   * request body (`data`), commonly used for batch deletion (e.g., an array of IDs).
   *
   * @param path The API endpoint path (e.g., '/api/users/1' or '/api/users/batch').
   * @param data Optional request body for the DELETE request (e.g., an array of IDs for batch delete).
   * @param headers Optional custom HTTP headers to merge with default headers.
   * @returns A Promise that resolves with the HTTP response or rejects with an error.
   */
  delete(path: string, data?: IObject | any[], headers?: IObject): Promise<IHttpResponse> {
    return http({
      url: path,
      data: data, // Used as request body, often for batch deletes. Be mindful of backend support.
      method: "DELETE",
      headers: {
        // Explicitly setting Content-Type for DELETE if a body is expected.
        // This ensures consistency across methods that send data.
        "Content-Type": "application/json;charset=UTF-8",
        ...headers
      }
    });
  },

  /**
   * Performs a **GET** HTTP request.
   * Used for fetching resources. Parameters are sent as URL query strings.
   *
   * @param path The API endpoint path.
   * @param params Optional query parameters (e.g., `{ userId: 1, status: 'active' }`).
   * @param headers Optional custom HTTP headers.
   * @returns A Promise that resolves with the HTTP response or rejects with an error.
   */
  get(path: string, params?: IObject, headers?: IObject): Promise<IHttpResponse> {
    // Direct Promise return: The `http` utility is expected to return a Promise.
    // Removed the manual Promise creation and the specific `if (error !== "-999")` check.
    // Error handling (like ignoring specific error codes) should ideally be handled
    // at a lower level within the `http` utility's interceptors for consistency.
    return http({
      url: path,
      params, // Query parameters for GET requests
      headers,
      method: "GET"
    });
  },

  /**
   * Performs a **PUT** HTTP request.
   * Typically used for full updates of existing resources. The request body contains the full resource representation.
   *
   * @param path The API endpoint path.
   * @param data Optional request body (JSON payload) for the resource update.
   * @param headers Optional custom HTTP headers.
   * @returns A Promise that resolves with the HTTP response or rejects with an error.
   */
  put(path: string, data?: IObject, headers?: IObject): Promise<IHttpResponse> {
    return http({
      url: path,
      data: data, // Request body for PUT requests
      method: "PUT",
      headers: {
        "Content-Type": "application/json;charset=UTF-8",
        ...headers
      }
    });
  },

  /**
   * Performs a **PATCH** HTTP request.
   * Typically used for partial updates of existing resources. The request body contains only the fields to be updated.
   *
   * @param path The API endpoint path.
   * @param data Optional request body (JSON payload) for the partial resource update.
   * @param headers Optional custom HTTP headers.
   * @returns A Promise that resolves with the HTTP response or rejects with an error.
   */
  patch(path: string, data?: IObject, headers?: IObject): Promise<IHttpResponse> {
    return http({
      url: path,
      data: data, // Request body for PATCH requests
      method: "PATCH",
      headers: {
        "Content-Type": "application/json;charset=UTF-8",
        ...headers
      }
    });
  },

  /**
   * Performs a **POST** HTTP request.
   * Typically used for creating new resources or submitting form data.
   *
   * @param path The API endpoint path.
   * @param data Optional request body (JSON payload) for resource creation/submission.
   * @param headers Optional custom HTTP headers.
   * @returns A Promise that resolves with the HTTP response or rejects with an error.
   */
  post(path: string, data?: IObject, headers?: IObject): Promise<IHttpResponse> {
    return http({
      url: path,
      method: "POST", // Changed from 'post' to 'POST' for consistency with HTTP method standards.
      headers: {
        "Content-Type": "application/json;charset=UTF-8",
        ...headers
      },
      data: data // Request body for POST requests
    });
  }
};
please optimize the code