import axios from "../../../config/axios";
import {
  type CreateProductDetailRequestType,
  type CreateProductDetailResponseType,
  type CreateProductAttributeRequestType,
  type CreateProductAttributeResponseType,
  type SaveProductRequestType,
  type SaveProductResponseType,
  type GetProductsResponseType,
  type GetProductVariantsResponseType,
  type CreateProductVariantRequestType,
  type CreateProductVariantResponseType,
  type GetProductByIdResponseType,
  type UpdateVariantRequestType,
  type UpdateVariantResponseType,
  type GetVariantByIdResponseType,
  type UpdateProductDetailRequestType,
  type UpdateProductDetailResponseType,
  type UpdateProductAttributeRequestType,
  type UpdateProductAttributeResponseType,
  type UpdateProductRequestType,
  type UpdateProductResponseType
} from "../types/Types";

export const createProductDetail = async (
  variables: CreateProductDetailRequestType
): Promise<CreateProductDetailResponseType> => {
  const { data } = await axios.post("/admin/products/creation/detail", variables);
  return data;
};

export const createProductAttribute = async (
  variables: CreateProductAttributeRequestType
): Promise<CreateProductAttributeResponseType> => {
  const { data } = await axios.post("/admin/products/creation/attribute", variables);
  return data;
};

export const saveProduct = async (
  variables: SaveProductRequestType
): Promise<SaveProductResponseType> => {
  const { data } = await axios.post("/admin/products/creation/save", variables);
  return data;
};

export const getProducts = async (page: number = 1, limit: number = 10, search?: string): Promise<GetProductsResponseType> => {
  const params: Record<string, string | number> = { page, limit };
  if (search && search.trim()) {
    params.q = search.trim();
  }

  const { data } = await axios.get(`/admin/products/native`, {
    params
  });
  return data;
};

export const getProductVariants = async (productId: string): Promise<GetProductVariantsResponseType> => {
  const { data } = await axios.get(`/admin/products/variants/${productId}`);
  return data;
};

export const createProductVariant = async (variables: CreateProductVariantRequestType): Promise<CreateProductVariantResponseType> => {
  const { data } = await axios.post("/admin/products/variants/create", variables);
  return data;
};

export const getProductById = async (id: string): Promise<GetProductByIdResponseType> => {
  const { data } = await axios.get(`/admin/products/get/${id}`);
  return data;
};

export const updateVariant = async (variables: UpdateVariantRequestType): Promise<UpdateVariantResponseType> => {
  const { data } = await axios.patch(`/admin/products/variants/update`, variables);
  return data;
};

export const getVariantById = async (productId: string, variantId: string): Promise<GetVariantByIdResponseType> => {
  const { data } = await axios.get(`/admin/products/variant/${productId}/${variantId}`);
  return data;
};

export const updateProductDetail = async (params: UpdateProductDetailRequestType): Promise<UpdateProductDetailResponseType> => {
  const { data } = await axios.patch(`/admin/products/update/detail`, params);
  return data;
};

export const updateProductAttribute = async (params: UpdateProductAttributeRequestType): Promise<UpdateProductAttributeResponseType> => {
  const { data } = await axios.patch(`/admin/products/update/attribute`, params);
  return data;
};

export const updateProduct = async (params: UpdateProductRequestType): Promise<UpdateProductResponseType> => {
  const { data } = await axios.patch(`/admin/products/update/save`, params);
  return data;
};
