import axios from "axios";

const WEWIRE_BASE_URL = process.env.WEWIRE_BASE_URL;
const WEWIRE_API_KEY = process.env.WEWIRE_API_KEY;

class WeWireService {
  /**
   * Get all currency exchange rates from WeWire
   */
  static async getRates() {
    const url = `${WEWIRE_BASE_URL}/v1/rates`;
    try {
      const response = await axios.get(url, {
        headers: {
          "ww-api-key": WEWIRE_API_KEY,
          "Accept": "*/*",
        },
      });
      return response.data;
    } catch (error) {
      console.error("❌ WeWire getRates error:", error.response?.data || error.message);
      throw error;
    }
  }

  /**
   * Get exchange rate for a specific currency pair from WeWire
   */
  static async getPairRate(from, to) {
    const url = `${WEWIRE_BASE_URL}/v1/rates/pair`;
    try {
      const response = await axios.get(url, {
        headers: {
          "ww-api-key": WEWIRE_API_KEY,
          "Accept": "*/*",
        },
        params: { from, to },
      });
      return response.data; // { from, to, rate, buyRate, sellRate }
    } catch (error) {
      console.error(`❌ WeWire getPairRate error (${from} to ${to}):`, error.response?.data || error.message);
      throw error;
    }
  }
}

export default WeWireService;
