import axios from "axios";

const BASE_URL = process.env.PAYSCRIBE_BASE_URL;
const API_KEY  = process.env.PAYSCRIBE_API_KEY;

const headers = () => ({
  Authorization: `Bearer ${API_KEY}`,
  "Content-Type": "application/json",
});

class PayscribeService {
  // ────────────────────────────────────────────────────────
  // Customer Onboarding
  // ────────────────────────────────────────────────────────

  /**
   * Register a customer as a PayScribe cardholder (KYC onboarding).
   * @param {object} payload - { firstName, lastName, phone, email, dob, address, bvn, selfieUrl, idDocument }
   */
  static async registerCustomerProfile(payload) {
    try {
      const { data } = await axios.post(`${BASE_URL}/customers/create/full`, payload, { headers: headers() });
      return data;
    } catch (err) {
      console.error("❌ Payscribe registerCustomerProfile error:", err.response?.data || err.message);
      throw err;
    }
  }

  // ────────────────────────────────────────────────────────
  // Card Provisioning
  // ────────────────────────────────────────────────────────

  /**
   * Create a new virtual card.
   * @param {object} payload - { customer_id, amount, brand, reference }
   */
  static async createVirtualCard(payload) {
    try {
      const { data } = await axios.post(`${BASE_URL}/cards`, payload, { headers: headers() });
      return data;
    } catch (err) {
      console.error("❌ Payscribe createVirtualCard error:", err.response?.data || err.message);
      throw err;
    }
  }

  // ────────────────────────────────────────────────────────
  // Card Funding / Top-up
  // ────────────────────────────────────────────────────────

  /**
   * Top-up an existing virtual card.
   * @param {string} cardId - PayScribe card_id
   * @param {object} payload - { amount, reference }
   */
  static async topUpCard(cardId, payload) {
    try {
      const { data } = await axios.post(`${BASE_URL}/cards/${cardId}/topup`, payload, { headers: headers() });
      return data;
    } catch (err) {
      console.error("❌ Payscribe topUpCard error:", err.response?.data || err.message);
      throw err;
    }
  }

  // ────────────────────────────────────────────────────────
  // Card Withdrawal
  // ────────────────────────────────────────────────────────

  /**
   * Withdraw funds from a virtual card back to the customer's wallet.
   * @param {string} cardId - PayScribe card_id
   * @param {object} payload - { amount }
   */
  static async withdrawFromCard(cardId, payload) {
    try {
      const { data } = await axios.patch(`${BASE_URL}/cards/${cardId}/withdraw`, payload, { headers: headers() });
      return data;
    } catch (err) {
      console.error("❌ Payscribe withdrawFromCard error:", err.response?.data || err.message);
      throw err;
    }
  }

  // ────────────────────────────────────────────────────────
  // Lifecycle Management
  // ────────────────────────────────────────────────────────

  /**
   * Freeze a virtual card.
   * @param {string} cardId - PayScribe card_id
   */
  static async freezeCard(cardId) {
    try {
      const { data } = await axios.patch(`${BASE_URL}/cards/${cardId}/freeze`, {}, { headers: headers() });
      return data;
    } catch (err) {
      console.error("❌ Payscribe freezeCard error:", err.response?.data || err.message);
      throw err;
    }
  }

  /**
   * Unfreeze a virtual card.
   * @param {string} cardId - PayScribe card_id
   */
  static async unfreezeCard(cardId) {
    try {
      const { data } = await axios.post(`${BASE_URL}/cards/${cardId}/unfreeze`, {}, { headers: headers() });
      return data;
    } catch (err) {
      console.error("❌ Payscribe unfreezeCard error:", err.response?.data || err.message);
      throw err;
    }
  }

  /**
   * Permanently terminate a virtual card.
   * @param {string} cardId - PayScribe card_id
   */
  static async terminateCard(cardId) {
    try {
      const { data } = await axios.post(`${BASE_URL}/cards/${cardId}/terminate`, {}, { headers: headers() });
      return data;
    } catch (err) {
      console.error("❌ Payscribe terminateCard error:", err.response?.data || err.message);
      throw err;
    }
  }

  // ────────────────────────────────────────────────────────
  // Bill Payments (existing)
  // ────────────────────────────────────────────────────────

  /**
   * Pay utility bills via PayScribe.
   * @param {object} payload
   */
  static async payBill(payload) {
    try {
      const { data } = await axios.post(`${BASE_URL}/bills`, payload, { headers: headers() });
      return data;
    } catch (err) {
      console.error("❌ Payscribe payBill error:", err.response?.data || err.message);
      throw err;
    }
  }
}

export default PayscribeService;
