import apiClient from "../api-client";
import {
  BannerResponse,
  ContactFormRequest,
  ContactFormResponse,
  ContentPageResponse,
  DeliveryTimeResponse,
  FAQCategory,
  HolidayResponse,
  IndustryResponse,
  ProductMenuResponse,
  SubscribeResponse,
} from "../types/models/common";

export default class commonService {
  static fetchProductMenu = async (
    id: string
  ): Promise<ProductMenuResponse> => {
    try {
      const response = await apiClient.get<ProductMenuResponse>(
        `/api/product-menus/parent/${id}`
      );
      return response.data;
    } catch (error) {
      console.error(`Error in commonService:fetchProductMenu`, error);
      throw error;
    }
  };

  static fetchBanners = async (type: string): Promise<BannerResponse> => {
    try {
      const response = await apiClient.get<BannerResponse>(
        `/api/banners/find/${type}`
      );
      return response.data;
    } catch (error) {
      console.error(`Error in commonService:fetchBanners`, error);
      throw error;
    }
  };

  static fetchHolidaysList = async (): Promise<HolidayResponse> => {
    try {
      const response = await apiClient.get<HolidayResponse>(`/api/holidays`);
      return response.data;
    } catch (error) {
      console.error(`Error in commonService:fetchHolidaysList`, error);
      throw error;
    }
  };

  static fetchDeliveryTimeList = async (): Promise<DeliveryTimeResponse> => {
    try {
      const response = await apiClient.get<DeliveryTimeResponse>(
        `/api/delivery-times`
      );
      return response.data;
    } catch (error) {
      console.error(`Error in commonService:fetchDeliveryTimeList`, error);
      throw error;
    }
  };

  static fetchFaqList = async (): Promise<FAQCategory[]> => {
    try {
      const response = await apiClient.get<{ data: FAQCategory[] }>(
        `/api/faq-categories`
      );
      return response.data?.data || [];
    } catch (error) {
      console.error("Error in FaqService: fetchFaqList", error);
      throw error;
    }
  };

  static subscribeNewsletter = async (
    email: string
  ): Promise<SubscribeResponse> => {
    try {
      const response = await apiClient.post(
        `/subscribe`,
        {},
        {
          headers: {
            email: email,
          },
        }
      );

      return response.data;
    } catch (error) {
      console.error("Error in commonService:subscribeNewsletter", error);
      throw error;
    }
  };

  static getIndustry = async (): Promise<IndustryResponse> => {
    try {
      const response = await apiClient.get(`/api/industries?status=true&popularIndustry=true`);
      return response.data;
    } catch (error) {
      console.error("Error in commonService:getIndustry", error);
      throw error;
    }
  };

  static submitContactForm = async (
    data: ContactFormRequest
  ): Promise<ContactFormResponse> => {
    try {
      const response = await apiClient.post(`/api/contact`, data);

      return response.data;
    } catch (error) {
      console.error("Error in commonService:submitContactForm", error);
      throw error;
    }
  };
}

export async function fetchContentPage(slug: string): Promise<ContentPageResponse> {
  try {
    const response = await apiClient.get<ContentPageResponse>(`/api/content-pages/${slug}`);

    return response.data;
  } catch (error) {
    console.error("Error fetching policy:", error);
    throw error;
  }
}
