import { LoginDTO, CreateUserDTO, AuthResponse } from "@calchat/shared";
import { ApiClient } from "./ApiClient";
import { useAuthStore } from "../stores";

export const AuthService = {
  login: async (credentials: LoginDTO): Promise<AuthResponse> => {
    const response = await ApiClient.post<AuthResponse>(
      "/auth/login",
      credentials,
      { skipAuth: true },
    );
    await useAuthStore.getState().login(response.user);
    return response;
  },

  register: async (data: CreateUserDTO): Promise<AuthResponse> => {
    const response = await ApiClient.post<AuthResponse>(
      "/auth/register",
      data,
      { skipAuth: true },
    );
    await useAuthStore.getState().login(response.user);
    return response;
  },

  logout: async (): Promise<void> => {
    await useAuthStore.getState().logout();
  },
};
