import { getStoredLocale } from '@/lib/getLocale';
import axios from "axios";
export async function getCountries(token: string): Promise<any> {
  try {
    const locale = getStoredLocale();
    const url = `${process.env.NEXT_PUBLIC_API_URL}/countries`;
    const headers: Record<string, string> = {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${token}`,
      'Accept-Language': locale,
    };
    const response = await axios.get(url, { headers });
    const result = response?.data?.data?.data;
    return {
      success: true,
      message: "get countries success",
      countryData: result,
    };
  } catch (error: any) {
    throw error;
  }
}

export async function getStates(token?: string, countryId: string = "231"): Promise<any> {
    try {      
        const locale = getStoredLocale();
        const url = `${process.env.NEXT_PUBLIC_API_URL}/states?country_id=${countryId}`;        
        const headers: Record<string, string> = {
          'Content-Type': 'application/json',
          'Accept-Language': locale,
        };

        if(token) {
            headers['Authorization'] = `Bearer ${token}`
        }

        const response = await axios.get(url, { headers });      
        const result = response?.data?.data?.data;
        return {
          success: true,
          message: "get states success",
          stateData: result,
        };
    } catch (error:any) {
        throw error;
    }
}


export async function getCities(token: string, stateId: string): Promise<any> {
    try {      
        const locale = getStoredLocale();
        const url = `${process.env.NEXT_PUBLIC_API_URL}/cities?state_id=${stateId}`;
        const headers: Record<string, string> = {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${token}`,
          'Accept-Language': locale,
        };

        const response = await axios.get(url, { headers });        
        const result = response?.data?.data?.data;
        return {
          success: true,
          message: "get cities success",
          cityData: result,
        };
    } catch (error:any) {
        throw error;
    }
}


export async function getAllCities({page, limit = 100, search = ""}: { page: number; limit?: number, search?: string }): Promise<any> {
  try {
    const locale = getStoredLocale();
    const url = `${process.env.NEXT_PUBLIC_API_URL}/countries/101/cities?page=${page}&limit=${limit}&name=${search}`;
    const headers: Record<string, string> = {
      'Content-Type': 'application/json',
      'Accept-Language': locale,
    };
    const response = await axios.get(url, { headers });
    const result = response?.data?.data?.cities;
    return {
      success: true,
      message: "City Fetch Successfully!",
      cityData: result,
      totalPages: response?.data?.data?.pagination?.total,
      currentPage: response?.data?.data?.pagination?.page
    };
  } catch (error: any) {
    throw error;
  }
}
