import { http } from "@/shared/api/httpClient";
import {
  CreateSessionResponseSchema,
  ICreateSessionRequest,
  ICreateSessionResponse,
  IUpdateSessionRequest,
  IUpdateSessionResponse,
  UpdateSessionResponseSchema,
  IGetPixelsRequest,
  IGetPixelsResponse,
  GetPixelsResponseSchema,
} from "./types";
import { API_ROUTES } from "@/shared/constants/api-routes";

export const createSession = async (
  payload: ICreateSessionRequest
): Promise<ICreateSessionResponse> => {
  return http.post<ICreateSessionResponse>(API_ROUTES.session(), payload, {
    tags: ["session", "create"],
    schema: CreateSessionResponseSchema,
    revalidate: 0,
  });
};

export const updateSession = async (
  payload: IUpdateSessionRequest
): Promise<IUpdateSessionResponse> => {
  // Отправляем только data без вложенности
  return http.patch<IUpdateSessionResponse>(
    API_ROUTES.session(payload.sessionId),
    payload.data,
    {
      tags: ["session", "update"],
      schema: UpdateSessionResponseSchema,
      revalidate: 0,
    }
  );
};

export const getPixels = async (
  payload: IGetPixelsRequest
): Promise<IGetPixelsResponse> => {
  return http.get<IGetPixelsResponse>(API_ROUTES.sessionPixels(), {
    tags: ["session", "pixels"],
    schema: GetPixelsResponseSchema,
    revalidate: 3600, // Cache for 1 hour
    query: payload,
  });
};
