"use client";

import { api } from "@/app/services/config/api.config";
import axios from "axios";

export const settingsServices = {
  updateGeneralSetting: async (data) => {
    const response = await api.patch("/admin/settings/general", { data });

    return response.data;
  },

  setDefaultGeneralSetting: async () => {
    const response = await api.patch(
      "/admin/settings/general?setDefault=1",
      {}
    );

    return response.data;
  },

  getAuthorization: async (url, ID) => {
    const protocol = window.location.protocol.replace(":", "");
    url = url.replace(protocol + "://", "");
    // only check if url is valid, must return status 200
    const response = await axios.post(`${protocol}://${url}/api/bridge/auth`, {
      data: { ID },
    });

    return { ...response.data?.data, url: `${protocol}://${url}` };
  },

  updateBridgeSetting: async (data) => {
    const response = await api.patch("/admin/settings/join-bridge", { data });

    return response.data;
  },

  testBridgeUrl: async (url, api_key) => {
    const protocol = window.location.protocol.replace(":", "");
    url = url.replace(protocol + "://", "");
    // only check if url is valid, must return status 200
    await axios.get(`${protocol}://${url}/api/bridge`, {
      headers: {
        Authorization: `Bearer ${api_key}`,
      },
    });

    return `${protocol}://${url}`;
  },
};
