import { z } from "zod/v4";
import type { BannersIdbanner } from "~/schemas/public/Banners";
import {
	adminProcedure,
	createTRPCRouter,
	publicProcedure,
} from "~/server/api/trpc";
import { db } from "~/server/db";
import {
	addBanner,
	deleteBanner,
	getAllBanners,
	getBanner,
	updateBanner,
} from "~/server/services/banners.service";

export const bannersRouter = createTRPCRouter({
	addBanner: adminProcedure
		.input(
			z.object({
				color: z.string().nullable(),
				cta_href: z.string().nullable(),
				cta_icon: z.string().nullable(),
				cta_text: z.string().nullable(),
				has_cta: z.boolean(),
				hide_duration: z.number().nullable(),
				idbanner: z.custom<BannersIdbanner>(),
				is_active: z.boolean(),
				is_unskippable: z.boolean(),
				show_private: z.boolean(),
				show_public: z.boolean(),
				testo: z.string().nullable(),
				titolo: z.string().nullable(),
			}),
		)
		.mutation(async ({ input }) => {
			return await addBanner({ db, input });
		}),
	getBannerAllData: publicProcedure.query(async () => {
		return await getAllBanners({ db });
	}),
	deleteBanner: adminProcedure
		.input(z.object({ idbanner: z.custom<BannersIdbanner>() }))
		.mutation(async ({ input }) => {
			return await deleteBanner({ db, input });
		}),
	updateBanner: adminProcedure
		.input(
			z.object({
				color: z.string().nullable(),
				cta_href: z.string().nullable(),
				cta_icon: z.string().nullable(),
				cta_text: z.string().nullable(),
				has_cta: z.boolean(),
				hide_duration: z.number().nullable(),
				idbanner: z.custom<BannersIdbanner>(),
				is_active: z.boolean(),
				is_unskippable: z.boolean(),
				show_private: z.boolean(),
				show_public: z.boolean(),
				testo: z.string().nullable(),
				titolo: z.string().nullable(),
			}),
		)
		.mutation(async ({ input }) => {
			return await updateBanner({ db, input });
		}),

	getBannerData: publicProcedure
		.input(
			z.object({
				area_riservata: z.boolean(),
			}),
		)
		.query(async ({ input }) => {
			return await getBanner({
				area_riservata: input.area_riservata,
				db,
			});
		}),
});
