"use server";

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

import { IMeResponse, MeResponseSchema } from "./types";

export async function fetchMe(): Promise<ActionResponse<IMeResponse>> {
  try {
    const response = await http.get<IMeResponse>(API_ROUTES.usersMe(), {
      tags: ["user", "me"],
      schema: MeResponseSchema,
      revalidate: 0,
    });
    return { data: response, error: null };
  } catch (error) {
    // eslint-disable-next-line no-console
    console.error("Failed to fetch me:", error);
    const errorMessage =
      error instanceof Error ? error.message : "Something went wrong.";
    return { data: null, error: errorMessage };
  }
}
