import axios from "axios";
import { AccessRequest, AccessResponse } from "../types/authz";

const OPA_URL = "http://64.227.108.180:8182/v1/data/authz/access/allow";

export async function opaAllow(payload: AccessRequest): Promise<boolean> {
  console.log("OPA REQUEST:", JSON.stringify(payload, null, 2));

  const res = await axios.post<AccessResponse>(OPA_URL, payload, {
    headers: { "Content-Type": "application/json" },
    timeout: 5000,
  });

  console.log("OPA RESPONSE:", JSON.stringify(res.data, null, 2));
  return Boolean(res.data?.result);
}
