import apiClient from "../api-client";
import { PickupAddressResponse } from "../types/models/address";
import { getStoreData } from "../utils/storagedata";

export default class addressService {
  static getPickupAddress = async () => {
    try {
      const response = await apiClient.get<PickupAddressResponse>(
        `/api/pickup-addresses`
      );
      return response.data;
    } catch (error) {
      console.error(`Error in addressService:getPickupAddress`, error);
      throw error;
    }
  };

  static getAddress = async () => {
    try {
      const response = await apiClient.get(`/api/address`, {
        headers: {
          token: await getStoreData<string>("token"),
        },
      });
      return response.data;
    } catch (error) {
      console.error(`Error in addressService:getaddress`, error);
      throw error;
    }
  };

  static checkValidAddress = async (payload: Record<string, unknown>) => {
    try {
      const response = await apiClient.post(`/api/external/addressvalidation`, payload);
      return response.data;
    } catch (error) {
      console.error(`Error in addressService:checkValidAddress`, error);
      throw error;
    }
  };

  static addAddress = async (payload: Record<string, unknown>) => {
    try {
      const response = await apiClient.post(`/api/address`, payload);
      return response.data;
    } catch (error) {
      console.error(`Error in addressService:addAddress`, error);
      throw error;
    }
  };
  static updateAddress = async (id: string, payload: Record<string, unknown>) => {
    try {
      const response = await apiClient.put(`/api/address/${id}`, payload);
      return response.data;
    } catch (error) {
      console.error(`Error in addressService:updateAddress`, error);
      throw error;
    }
  };

  static deleteAddress = async (id: string) => {
    try {
      const response = await apiClient.delete(`/api/address/${id}`);
      return response.data;
    } catch (error) {
      console.error(`Error in addressService:removeAddress`, error);
      throw error;
    }
  };

  static makeDefaultAddress = async (payload: Record<string, unknown>) => {
    try {
      const response = await apiClient.post(`/api/address/make-default`, payload);
      return response.data;
    } catch (error) {
      console.error(`Error in addressService:makeDefaultAddress`, error);
      throw error;
    }
  };

  static editUserProfile = async (payload: Record<string, unknown>) => {
    try {
      const response = await apiClient.post(`/api/auth/edit-profile`, payload);
      return response.data;
    } catch (error) {
      console.error(`Error in addressService:editUserProfile`, error);
      throw error;
    }
  };

  static getCountries = async () => {
    try {
      const response = await apiClient.get(`/api/countries?status=true`);
      return response.data;
    } catch (error) {
      console.error(`Error in addressService:getCountries`, error);
      throw error;
    }
  };

  static getStates = async (country?: string) => {
    try {
      const url = country
        ? `/api/states?status=true&country=${encodeURIComponent(country)}`
        : `/api/states?status=true`;
      const response = await apiClient.get(url);
      return response.data;
    } catch (error) {
      console.error(`Error in addressService:getStates`, error);
      throw error;
    }
  };
}