import type {
  GetAllNewsCategory,
  GetDetailNewsCategory,
  NewsAll,
  NewsDetail,
} from "@/pages/news/lib/type";
import httpClient from "@/shared/config/api/httpClient";
import { NEWS, NEWS_CATEGORY } from "@/shared/config/api/URLs";
import type { AxiosResponse } from "axios";

const getAllNews = async ({
  page,
  page_size,
}: {
  page: number;
  page_size: number;
}): Promise<AxiosResponse<NewsAll>> => {
  const response = await httpClient.get(NEWS, { params: { page, page_size } });
  return response;
};

const getDetailNews = async (
  id: number,
): Promise<AxiosResponse<NewsDetail>> => {
  const response = await httpClient.get(`${NEWS}${id}/`);
  return response;
};

const addNews = async (body: FormData) => {
  const response = await httpClient.post(NEWS, body, {
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
  return response;
};

const updateNews = async ({ body, id }: { id: number; body: FormData }) => {
  const response = await httpClient.patch(`${NEWS}${id}/`, body, {
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
  return response;
};

const deleteNews = async (id: number) => {
  const response = await httpClient.delete(`${NEWS}${id}/`);
  return response;
};

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

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

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

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

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

export {
  addNews,
  addNewsCategory,
  deleteNews,
  deleteNewsCategory,
  getAllNews,
  getAllNewsCategory,
  getDetailNews,
  getDetailNewsCategory,
  updateNews,
  updateNewsCategory,
};
