import {
  CANCEL_ORDER_URL,
  CREATE_ORDER_URL,
  DISCOUNT_URL,
  ORDER_DETAILS_URL,
  ORDERS_URL,
  PRODUCTS_AND_CATEGORIES_URL,
  RATE_ORDER_URL,
  RESTAURANT_DETAILS_URL,
  TABLES_URL,
  USER_DETAILS_URL,
  EGIFT_CARDS_URL,
  REDEEM_DETAILS_URL,
  LOYALTY_HISTORY_URL,
} from "utils/constants";

import { OrderDetails } from "pages/checkout/hooks/types";
import menuParser from "pages/menu/helper";
import {
  DiscountResultType,
  RestaurantDetails,
  UserType,
} from "utils/types/appTypes";
import { baseApi } from "./apiSlice";
import { EGiftCard } from "pages/EGiftCards/type";
import { RedeemResponse } from "pages/redeem/types";

export const branchApi = baseApi.injectEndpoints({
  endpoints: (builder) => ({
    getRestaurantDetails: builder.query<RestaurantDetails, string | void>({
      query: (restaurantSubdomain: string) => ({
        url: RESTAURANT_DETAILS_URL,
        method: "POST",
        body: {
          subdomain: restaurantSubdomain,
        },
      }),
      transformResponse: (response: any) => {
        return response?.result?.restaurants?.[0];
      },
      providesTags: ["Restaurant"],
    }),
    getMenu: builder.query<any, string | void>({
      query: (restaurantId: string) =>
        `${PRODUCTS_AND_CATEGORIES_URL}${restaurantId}`,
      transformResponse: (response: any) => {
        return menuParser(response);
      },
    }),
    getOrders: builder.query<any, void>({
      query: () => ({
        url: ORDERS_URL,
        method: "POST",
      }),
      transformResponse: (response: any) => {
        return response.result.data.orders;
      },
      providesTags: ["Orders"],
    }),
    createOrder: builder.mutation({
      query: (body: any) => ({
        url: CREATE_ORDER_URL,
        method: "POST",
        body,
      }),
      invalidatesTags: ["Orders", "Restaurant"],
    }),
    cancelOrder: builder.mutation({
      query: ({ orderID }: { orderID: string }) => ({
        url: CANCEL_ORDER_URL,
        method: "POST",
        body: {
          order_id: orderID,
        },
      }),
      invalidatesTags: ["Orders", "Restaurant"],
    }),
    getTables: builder.query<any, { restaurantID: string; tableType: string }>({
      query: ({
        restaurantID,
        tableType,
      }: {
        restaurantID: string;
        tableType: string;
      }) => ({
        url: TABLES_URL,
        method: "POST",
        body: {
          restaurantID,
          tableType,
        },
      }),
      transformResponse: (response: any) => {
        return response.result.data;
      },
    }),
    getOrderDetails: builder.query<
      OrderDetails,
      { orderID: string; restaurantID: string }
    >({
      query: ({
        orderID,
        restaurantID,
      }: {
        orderID: string;
        restaurantID: string;
      }) => ({
        url: `${ORDER_DETAILS_URL}/${orderID}`,
        method: "POST",
        body: {
          restaurantID,
        },
      }),
      transformResponse: (response: any) => {
        return response.result;
      },
    }),
    getDiscount: builder.mutation<
      DiscountResultType,
      { discountCode: string; restaurantID: string }
    >({
      query: ({
        discountCode,
        restaurantID,
      }: {
        discountCode: string;
        restaurantID: string;
      }) => ({
        url: `${DISCOUNT_URL}/${discountCode}/${restaurantID}`,
        method: "GET",
      }),
      transformResponse: (response: any) => {
        return response.result;
      },
    }),
    getUserDetails: builder.query<UserType, void>({
      query: () => ({
        url: `${USER_DETAILS_URL}`,
        method: "GET",
      }),
      transformResponse: (response: any) => {
        return response.result;
      },
    }),
    rateOrder: builder.mutation({
      query: ({
        orderID,
        rating,
        comment,
        userID,
      }: {
        orderID: string;
        rating: number;
        comment: string;
        userID: string;
      }) => ({
        url: RATE_ORDER_URL,
        method: "POST",
        body: {
          order_id: orderID,
          rating,
          comment,
          user_id: userID,
        },
      }),
      invalidatesTags: ["Orders", "Restaurant"],
    }),
    getEGiftCards: builder.query<EGiftCard[], void>({
      query: () => ({
        url: EGIFT_CARDS_URL,
        method: "POST",
      }),
      transformResponse: (response: any) => {
        return response.result;
      },
    }),
    getRedeemDetails: builder.query<RedeemResponse, string>({
      query: (voucherId: string) => ({
        url: `${REDEEM_DETAILS_URL}/${voucherId}`,
        method: "GET",
      }),
      transformResponse: (response: any) => {
        return response.result;
      },
    }),
    getLoyaltyHistory: builder.query<
      {
        restaurant_id: number;
        restaurant_name: string;
        restaurant_icon: string;
        total_points: string;
        loyalty_stamp_image: string;
        loyalty_stamps: number;
      }[],
      void
    >({
      query: () => ({
        url: LOYALTY_HISTORY_URL,
        method: "GET",
      }),
      transformResponse: (response: any) => {
        return response.result.data.orders;
      },
    }),
  }),
});
export const {
  useGetRestaurantDetailsQuery,
  useGetMenuQuery,
  useCreateOrderMutation,
  useGetOrdersQuery,
  useGetTablesQuery,
  useGetOrderDetailsQuery,
  useCancelOrderMutation,
  useGetDiscountMutation,
  useRateOrderMutation,
  useGetUserDetailsQuery,
  useGetEGiftCardsQuery,
  useGetRedeemDetailsQuery,
  useGetLoyaltyHistoryQuery,
} = branchApi;
