/* eslint-disable no-console */
"use server";

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

import {
  GenerationResponse,
  GenerationResponseSchema,
  StartGenerationRequest,
} from "./types";

export async function startGeneration(
  payload: StartGenerationRequest
): Promise<ActionResponse<GenerationResponse>> {
  try {
    const response = await http.post<GenerationResponse>(
      API_ROUTES.startGeneration(),
      payload,
      {
        schema: GenerationResponseSchema,
        revalidate: 0,
      }
    );

    return { data: response, error: null };
  } catch (error) {
    console.error("Failed to start generation:", error);
    const errorMessage =
      error instanceof Error ? error.message : "Something went wrong.";
    return { data: null, error: errorMessage };
  }
}

export async function fetchGenerationStatus(
  id: string
): Promise<ActionResponse<GenerationResponse>> {
  try {
    const response = await http.get<GenerationResponse>(
      API_ROUTES.statusGeneration(id),
      {
        schema: GenerationResponseSchema,
        revalidate: 0,
      }
    );
    return { data: response, error: null };
  } catch (error) {
    console.error("Failed to fetch generation status:", error);
    const errorMessage =
      error instanceof Error ? error.message : "Something went wrong.";
    return { data: null, error: errorMessage };
  }
}
