"use server";

export interface AuthorizeParams {
  client_id: string;
  redirect_uri: string;
  response_type: string;
  scope: string;
  state: string;
}

export interface AuthorizeResponse {
  success: boolean;
  message: string;
  code?: string;
  state?: string;
}

export const authorize = async (params: AuthorizeParams, token: string): Promise<AuthorizeResponse> => {
  if (!process.env.SECNEX_OAUTH2_API_HOST) {
    return { success: false, message: "SecNex OAuth2 API host is not set" };
  }

  const response = await fetch(`${process.env.SECNEX_OAUTH2_API_HOST}/authorize`, {
    method: "POST",
    body: JSON.stringify(params),
    headers: {
      "Content-Type": "application/json",
      "Authorization": `Bearer ${token}`,
    },
  });
  const data = await response.json();
  return { success: true, message: data.message, code: data.code, state: data.state };
};