import axios from "axios";
import dotenv from "dotenv";

dotenv.config();

const YV_BASE_URL = process.env.YV_BASE_URL; // e.g. https://api.youverify.co
const YV_API_KEY = process.env.YV_API_KEY;

class YouverifyService {
  /**
   * Stage 1: BVN Verification
   */
  static async verifyBvn({ id, isSubjectConsent = true, premiumBVN = false, metadata = {} }) {
    const url = `${YV_BASE_URL}/v2/api/identity/ng/bvn`;
    const payload = {
      id,
      isSubjectConsent,
      premiumBVN,
      metadata,
    };

    try {
      const response = await axios.post(url, payload, {
        headers: {
          "Content-Type": "application/json",
          "token": YV_API_KEY, // Youverify requires token header
        },
      });

      return response.data;
    } catch (error) {
      console.error("❌ Youverify BVN error:", error.response?.data || error.message);
      throw error;
    }
  }

  /**
   * Stage 2: NIN ID Verification (Without selfie match)
   */
  static async verifyNin({
    id,
    isSubjectConsent = true,
    premiumNin = true,
    metadata = {},
  }) {
    const url = `${YV_BASE_URL}/v2/api/identity/ng/nin`;
    const payload = {
      id,
      isSubjectConsent,
      premiumNin,
      metadata,
    };

    try {
      const response = await axios.post(url, payload, {
        headers: {
          "Content-Type": "application/json",
          "token": YV_API_KEY,
        },
      });

      return response.data;
    } catch (error) {
      console.error("❌ Youverify NIN error:", error.response?.data || error.message);
      throw error;
    }
  }
}

export default YouverifyService;
