import { z } from "zod/v4";
import {
	adminProcedure,
	createTRPCRouter,
	publicProcedure,
} from "~/server/api/trpc";
import { db } from "~/server/db";
import {
	deleteStringa,
	getStringa,
	getStringhe,
	getStringheFiltered,
	newStringa,
	updateStringhe,
} from "~/server/services/testi_stringhe.service";
import { zTestiEStringheStingaId } from "~/server/utils/zod_types";

export const stringsRouter = createTRPCRouter({
	getStringa: publicProcedure
		.input(
			z.object({
				stringaId: zTestiEStringheStingaId,
			}),
		)
		.query(async ({ input }) => {
			return await getStringa({
				db,
				stringaId: input.stringaId,
			});
		}),
	getStringhe: adminProcedure.query(async () => {
		return await getStringhe({ db });
	}),
	getStringheCondizioni: adminProcedure.query(async () => {
		return await getStringheFiltered({ contains: "CONDIZIONI", db });
	}),
	deleteStringa: adminProcedure
		.input(
			z.object({
				stringaId: zTestiEStringheStingaId,
			}),
		)
		.mutation(async ({ input }) => {
			return await deleteStringa({
				db,
				stringaId: input.stringaId,
			});
		}),
	newStringa: adminProcedure
		.input(
			z.object({
				stringaId: zTestiEStringheStingaId,
				stringaValue: z.string().nullable(),
			}),
		)
		.mutation(async ({ input }) => {
			return await newStringa({
				db,
				stringaId: input.stringaId,
				stringaValue: input.stringaValue,
			});
		}),

	updateStringa: adminProcedure
		.input(
			z.object({
				stringaId: zTestiEStringheStingaId,
				stringaValue: z.string().nullable(),
			}),
		)
		.mutation(async ({ input }) => {
			return await updateStringhe({
				db,
				stringaId: input.stringaId,
				stringaValue: input.stringaValue,
			});
		}),
});
