import type {
  Faq,
  FaqCategory,
  FaqCategoryDetail,
  FaqOne,
} from "@/pages/faq/lib/type";
import httpClient from "@/shared/config/api/httpClient";
import { FAQ, FAQ_CATEGORIES } from "@/shared/config/api/URLs";
import type { AxiosResponse } from "axios";

const getAllFaq = async (params: {
  page: number;
  page_size: number;
  category: number;
}): Promise<AxiosResponse<Faq>> => {
  const res = await httpClient.get(FAQ, { params });
  return res;
};

const getOneFaq = async (id: number): Promise<AxiosResponse<FaqOne>> => {
  const res = await httpClient.get(`${FAQ}${id}/`);
  return res;
};

const createFaq = async (body: {
  title: string;
  title_ru: string;
  text: string;
  text_ru: string;
  category: number;
}) => {
  const res = await httpClient.post(FAQ, body);
  return res;
};

const updateFaq = async ({
  body,
  id,
}: {
  id: number;
  body: {
    title: string;
    title_ru: string;
    text: string;
    text_ru: string;
    category?: number;
  };
}) => {
  const res = await httpClient.patch(`${FAQ}${id}/`, body);
  return res;
};

const deleteFaq = async ({ id }: { id: number }) => {
  const res = await httpClient.delete(`${FAQ}${id}/`);
  return res;
};

const getAllFaqCategory = async (params: {
  page: number;
  page_size: number;
}): Promise<AxiosResponse<FaqCategory>> => {
  const res = await httpClient.get(FAQ_CATEGORIES, { params });
  return res;
};

const getDetailFaqCategory = async (
  id: number,
): Promise<AxiosResponse<FaqCategoryDetail>> => {
  const res = await httpClient.get(`${FAQ_CATEGORIES}${id}/`);
  return res;
};

const createFaqCategory = async (body: { name: string; name_ru: string }) => {
  const res = await httpClient.post(FAQ_CATEGORIES, body);
  return res;
};

const updateFaqCategory = async ({
  body,
  id,
}: {
  id: number;
  body: { name: string; name_ru: string };
}) => {
  const res = await httpClient.patch(`${FAQ_CATEGORIES}${id}/`, body);
  return res;
};

const deleteFaqCategory = async (id: number) => {
  const res = await httpClient.delete(`${FAQ_CATEGORIES}${id}/`);
  return res;
};

export {
  createFaq,
  createFaqCategory,
  deleteFaq,
  deleteFaqCategory,
  getAllFaq,
  getAllFaqCategory,
  getDetailFaqCategory,
  getOneFaq,
  updateFaq,
  updateFaqCategory,
};
