"use server";

import { revalidateTag } from "next/cache";

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

import { getMyChatSettings, updateMyChatSettings } from "./chatSettings.api";
import {
  CreateAllChatsResponseSchema,
  GetChatMessagesResponseSchema,
  IChatSettings,
  ICreateAllChatsResponse,
  IGetChatMessagesResponse,
  IGetMyChatSettingsResponse,
  IUpdateMyChatSettingsResponse,
} from "./types";

export async function createAllChats(): Promise<
  ActionResponse<ICreateAllChatsResponse>
> {
  try {
    const response = await http.post<ICreateAllChatsResponse>(
      API_ROUTES.createAllChats(),
      {},
      {
        schema: CreateAllChatsResponseSchema,
        revalidate: 0,
      }
    );

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

export async function fetchChatMessages(
  chatId: string,
  params: { limit?: number; page?: number } = { limit: 50, page: 1 }
): Promise<ActionResponse<IGetChatMessagesResponse>> {
  try {
    const response = await http.get<IGetChatMessagesResponse>(
      API_ROUTES.getChatMessages(chatId),
      {
        tags: ["chats", chatId, "messages"],
        schema: GetChatMessagesResponseSchema,
        query: { limit: params.limit, page: params.page },
        revalidate: 0,
      }
    );
    return { data: response, error: null };
  } catch (error) {
    // eslint-disable-next-line no-console
    console.error("Failed to fetch chat messages:", error);
    const errorMessage =
      error instanceof Error ? error.message : "Something went wrong.";
    return { data: null, error: errorMessage };
  }
}

export async function fetchMyChatSettings(): Promise<
  ActionResponse<IGetMyChatSettingsResponse>
> {
  try {
    const response = await getMyChatSettings();
    return { data: response, error: null };
  } catch (error) {
    // eslint-disable-next-line no-console
    console.error("Failed to fetch chat settings:", error);
    const errorMessage =
      error instanceof Error ? error.message : "Something went wrong.";
    return { data: null, error: errorMessage };
  }
}

export async function updateChatSettings(
  settings: IChatSettings
): Promise<ActionResponse<IUpdateMyChatSettingsResponse>> {
  try {
    const response = await updateMyChatSettings(settings);
    revalidateTag("profile");
    revalidateTag("chat-settings");
    return { data: response, error: null };
  } catch (error) {
    // eslint-disable-next-line no-console
    console.error("Failed to update chat settings:", error);
    const errorMessage =
      error instanceof Error ? error.message : "Something went wrong.";
    return { data: null, error: errorMessage };
  }
}

export async function revalidateChatsPage() {
  revalidateTag("chats-list");
}
