"use server";

import { http } from "@/shared/api/httpClient";
import { API_ROUTES } from "@/shared/constants/api-routes";
import { ActionResponse } from "@/types";

import {
  SingleCheckoutRequest,
  SingleCheckoutResponse,
  SingleCheckoutResponseSchema,
} from "./types";

export async function performSingleCheckout(
  payload: SingleCheckoutRequest
): Promise<ActionResponse<SingleCheckoutResponse>> {
  try {
    const response = await http.post<SingleCheckoutResponse>(
      API_ROUTES.paymentSingleCheckout(),
      payload,
      {
        schema: SingleCheckoutResponseSchema,
        revalidate: 0,
      }
    );

    return { data: response, error: null };
  } catch (error) {
    // eslint-disable-next-line no-console
    console.error("Failed to perform single checkout:", error);
    const errorMessage =
      error instanceof Error ? error.message : "Something went wrong.";
    return { data: null, error: errorMessage };
  }
}
