import AsyncStorage from '@react-native-async-storage/async-storage';
import axios from 'axios';
import { Alert } from 'react-native';
import { toastError } from '../components/Toast';



// Call this api function without token only for login
export const request = async (url, formData) => {
  const header = {
    'Content-Type': 'application/json',
  };

  return axios.post(url, formData, { headers: header }) 
    .then(response => {
      return response.data;
    })
    .catch(error => {
      console.log(error);
      return error;
    });
};



// Call this api function with token (POST type)
// export const post = async (url: string, formData: any) => {
//   const header = {
//     Authorization: 'Bearer ',
//     Accept: 'application/json',
//   };
//   return AsyncStorage.getItem('token').then(async value => {
  
//     const loginToken = JSON.parse(value as any);

//     console.log('loginToken----->',loginToken);
    
//     header.Authorization = `Bearer ${loginToken}`;

//     try {
//       const response = await axios
//         .post(url, formData, { headers: header });
//       return response.data;
//     } catch (error) {
//       // console.log(url);
//       // console.log(formData);
//       console.log('AXIOS ERROR status: ', error);
//     }
//   });
// };

export const post = async (url, formData) => {
  try {
    // const tokenValue = await AsyncStorage.getItem('token');
    // if (!tokenValue) {
    //   console.error('No token found in AsyncStorage');
    //   return null;
    // }

    // const loginToken = JSON.parse(tokenValue);
    // console.log('loginToken----->', loginToken);

    const headers = {
      Authorization: ``,
      Accept: 'application/json',
        "X-API-Key":"f7fa9b09-ced8-4862-8cb7-5e7599d90fa2"
    };

  //  console.log('URL => ',url);
  //  console.log('PARAMS => ',formData);
   
   
    const response = await axios.post(url, formData, { headers });
    return response.data;
    
  } catch (error) {
    console.error('API Error:', error.response?.data || error.message);
    Alert.alert(`Alert`,`${error.response?.data?.message ?error.response?.data?.message:'Server Error'}`)
    // toastError('Alert',`${error.response?.data?.message}`)
    return null;
  }
};


// CALL THIS API FUNCTION FOR GET TYPE API
export const get = async (url) => {
  const header = {
    Authorization: 'Bearer ',
    Accept: 'application/json',
  };
  return AsyncStorage.getItem('token').then(async value => {
    const loginToken = JSON.parse(value);
    
    // console.log('URL---->',url);
    // console.log('loginToken---->',loginToken);

    header.Authorization = `Bearer ${loginToken}`;

    try {
      const response = await axios
        .get(url, { headers: header });
      return response.data;
    } catch (error) {
      if (error) {
        console.log('AXIOS ERROR status: ', error);
      }
    }
  });
};


export const uploadImage = async (url, formData) => {
  const header = {
    Authorization: 'Bearer ',
    Accept: 'application/json',
    'Content-Type': `multipart/form-data`,
  };

  return AsyncStorage.getItem('userToken').then(value => {
    const token = JSON.parse(value);
    header.Authorization = `Bearer ${token}`;

    let dataf = {
      uri: formData.path,
      type: formData.mime,
      name: formData.path,
    };
    var data = new FormData();
    data.append('image', dataf);
    console.log('header', header);
    return fetch(url, {
      headers: header,
      method: 'POST',
      body: data,
    })
      .then(response => response.json())
      .then(json => {
        console.log('API', url);
        console.log('formData', formData);
        console.log('json data', json);
        return json;
      })
      .catch(error => {
        console.log('API', url);
        console.log('formData', formData);
        console.log('error ', error);
      });
  });
};


export const cityData = async () => {
  return AsyncStorage.getItem('cityshortname').then(e => {
    const user = JSON.parse(e);
    console.log('user,user', user);
    return user;
  });
};
