import { z } from "zod/v4";
import type { AnnunciUpdate } from "~/schemas/public/Annunci";
import {
	adminProcedure,
	createTRPCRouter,
	protectedProcedure,
	publicProcedure,
} from "~/server/api/trpc";
import {
	editAnnuncioHandler,
	get_AnnunciPositionsHandler,
	getAnnunciByCod,
	getAnnunciById_rawImgUrls,
	getAnnunciListHandler,
	getAnnunciMetaByCod,
	getAnnunciRicerca,
	getCodici_AnnunciHandler,
	getOptions_AnnunciHandler,
	getProprietarioDataHandler,
} from "~/server/controllers/annunci.controller";
import { zAnnuncioId, zServizioId } from "~/server/utils/zod_types";

export const annunciRouter = createTRPCRouter({
	editAnnuncio: adminProcedure
		.input(
			z.object({ annuncioId: zAnnuncioId, data: z.custom<AnnunciUpdate>() }),
		)
		.mutation(async ({ input }) => {
			return await editAnnuncioHandler({
				...input,
			});
		}),
	getAnnunciList: publicProcedure.query(async () => {
		return await getAnnunciListHandler();
	}),
	getAnnunciOptions: publicProcedure.query(async () => {
		return await getOptions_AnnunciHandler();
	}),
	getAnnuncio: publicProcedure
		.input(
			z.object({
				cod: z.string(),
			}),
		)
		.query(async ({ input }) => {
			return await getAnnunciByCod({ ...input });
		}),
	getAnnuncioById_rawImgUrls: publicProcedure
		.input(
			z.object({
				id: zAnnuncioId,
			}),
		)
		.query(async ({ input }) => {
			return await getAnnunciById_rawImgUrls({ ...input });
		}),

	getAnnuncioMeta: publicProcedure
		.input(
			z.object({
				cod: z.string(),
			}),
		)
		.query(async ({ input }) => {
			return await getAnnunciMetaByCod({ ...input });
		}),
	getCodici: publicProcedure.query(async () => {
		return await getCodici_AnnunciHandler();
	}),
	getMapping: publicProcedure
		.input(
			z.object({
				comune: z.string().optional(),
				consegna: z.number().optional(),
				tipologia: z.string().optional(),
			}),
		)
		.query(async ({ input }) => {
			return await get_AnnunciPositionsHandler({
				...input,
			});
		}),

	getProprietarioData: protectedProcedure
		.input(z.object({ annuncioId: zAnnuncioId, servizioId: zServizioId }))
		.query(async ({ input }) => {
			return await getProprietarioDataHandler({
				annuncioId: input.annuncioId,
				servizioId: input.servizioId,
			});
		}),

	annunciRicerca: publicProcedure
		.input(
			z.object({
				comune: z.string().optional(),
				consegna: z.number().optional(),
				sort: z.enum(["Recenti", "Prezzo", "Consegna", "Mq"]).optional(),
				tipologia: z.string().optional(),
			}),
		)
		.query(async ({ input }) => {
			return await getAnnunciRicerca({
				...input,
			});
		}),
});
