import { Path } from "tsoa";
import axios from "axios";
import { addLogSequence } from "./utils";

class CallAPI {
  //Get
  public async GetData(request: any, @Path() path: any, log = true) {
    const token = "Bearer " + request.headers.authorization.replace("Bearer ", "");
    const url = process.env.API_URL + path;
    try {
      const response = await axios.get(url, {
        headers: {
          Authorization: `${token}`,
          "Content-Type": "application/json",
          api_key: process.env.API_KEY,
        },
      });
      addLogSequence(request, {
        action: "request",
        status: "success",
        description: "connected",
        request: {
          method: "GET",
          url: url,
          response: JSON.stringify(response.data.result),
        },
      });
      return response.data.result;
    } catch (error) {
      addLogSequence(request, {
        action: "request",
        status: "error",
        description: "unconnected",
        request: {
          method: "GET",
          url: url,
          response: JSON.stringify(error),
        },
      });
      throw error;
    }
  }
  //Post
  public async PostData(request: any, @Path() path: any, sendData: any, log = true) {
    const token = "Bearer " + request.headers.authorization.replace("Bearer ", "");
    const url = process.env.API_URL + path;
    try {
      const response = await axios.post(url, sendData, {
        headers: {
          Authorization: `${token}`,
          "Content-Type": "application/json",
          api_key: process.env.API_KEY,
        },
      });
      if (log)
        addLogSequence(request, {
          action: "request",
          status: "success",
          description: "connected",
          request: {
            method: "POST",
            url: url,
            payload: JSON.stringify(sendData),
            response: JSON.stringify(response.data.result),
          },
        });
      return response.data.result;
    } catch (error) {
      if (log)
        addLogSequence(request, {
          action: "request",
          status: "error",
          description: "unconnected",
          request: {
            method: "POST",
            url: url,
            payload: JSON.stringify(sendData),
            response: JSON.stringify(error),
          },
        });
      throw error;
    }
  }

  //Post
  public async PostDataKeycloak(@Path() path: any, sendData: any) {
    // const token = request.headers.authorization;
    const url = process.env.KC_URL + path;
    try {
      const response = await axios.post(url, sendData, {
        headers: {
          //   Authorization: `${token}`,
          "Content-Type": "application/x-www-form-urlencoded",
          api_key: process.env.API_KEY,
        },
      });
      return response.data;
    } catch (error) {
      throw error;
    }
  }

  // Put
  public async PutData(request: any, @Path() path: any, sendData: any, log = true) {
    const token = "Bearer " + request.headers.authorization.replace("Bearer ", "");
    const url = process.env.API_URL + path;

    try {
      const response = await axios.put(url, sendData, {
        headers: {
          Authorization: `${token}`,
          "Content-Type": "application/json",
          api_key: process.env.API_KEY,
        },
      });

      if (log)
        addLogSequence(request, {
          action: "request",
          status: "success",
          description: "connected",
          request: {
            method: "PUT",
            url: url,
            payload: JSON.stringify(sendData),
            response: JSON.stringify(response.data.result),
          },
        });

      return response.data.result;
    } catch (error) {
      if (log)
        addLogSequence(request, {
          action: "request",
          status: "error",
          description: "unconnected",
          request: {
            method: "PUT",
            url: url,
            payload: JSON.stringify(sendData),
            response: JSON.stringify(error),
          },
        });

      throw error;
    }
  }

  //Delete File
  public async DeleteFile(request: any, @Path() path: any) {
    const token = "Bearer " + request.headers.authorization.replace("Bearer ", "");
    const url = process.env.API_URL + "/salary/file/" + path;
    try {
      await axios.delete(url, {
        headers: {
          Authorization: `${token}`,
          "Content-Type": "application/json",
          api_key: process.env.API_KEY,
        },
      });
    } catch (error) {
      throw error;
    }
  }
}

export default CallAPI;
