import { z } from "zod/v4";
import {
	adminProcedure,
	createTRPCRouter,
	protectedProcedure,
} from "~/server/api/trpc";
import {
	deleteChatHandler,
	getActiveInfos,
	getChatByUser,
	getChatByUserIdHandler,
	getChatInfo,
	getUserByChatHandler,
} from "~/server/controllers/chat.controller";
import {
	addChatEtichette,
	getChatEtichette,
	removeChatEtichette,
} from "~/server/controllers/etichette.controller";
import { db } from "~/server/db";
import { zChatId, zEtichettaId, zUserId } from "~/server/utils/zod_types";

export const chatRouter = createTRPCRouter({
	addEtichettaToChat: protectedProcedure
		.input(z.object({ chatId: zChatId, etichettaId: zEtichettaId }))
		.mutation(async ({ input }) => {
			return await addChatEtichette({
				chatid: input.chatId,
				db,
				etichettaid: input.etichettaId,
			});
		}),
	deleteChat: adminProcedure
		.input(
			z.object({
				chatId: zChatId,
			}),
		)
		.mutation(async ({ input }) => {
			return await deleteChatHandler({ chatId: input.chatId });
		}),

	getActiveChats: adminProcedure.query(async ({ ctx }) => {
		return await getActiveInfos({ userId: ctx.session.id });
	}),
	getChatEtichette: protectedProcedure
		.input(z.object({ chatId: zChatId }))
		.query(async ({ input }) => {
			return await getChatEtichette({
				chatid: input.chatId,
				db,
			});
		}),
	getChatIdByUser: adminProcedure
		.input(z.object({ userId: zUserId }))
		.query(async ({ input }) => {
			return await getChatByUserIdHandler({ userId: input.userId });
		}),

	getChatInfobyId: protectedProcedure
		.input(
			z.object({
				chatId: zChatId,
			}),
		)
		.query(async ({ input }) => {
			return await getChatInfo({ chatId: input.chatId });
		}),
	getSessionChats: protectedProcedure.query(async ({ ctx }) => {
		return await getChatByUser({
			userId: ctx.session.id,
		});
	}),

	getUserInfosByChat: protectedProcedure
		.input(
			z.object({
				chatId: zChatId,
			}),
		)
		.query(async ({ input }) => {
			return await getUserByChatHandler({ chatId: input.chatId });
		}),
	removeEtichettaFromChat: protectedProcedure
		.input(z.object({ chatId: zChatId, etichettaId: zEtichettaId }))
		.mutation(async ({ input }) => {
			return await removeChatEtichette({
				chatid: input.chatId,
				db,
				etichettaid: input.etichettaId,
			});
		}),
});
