import { protectedProcedure } from "$lib/server/trpc/t";
import { createTRPCRouter } from "$lib/trpc/t";
import { z } from "zod";
import { createProductPayload, updateProductPayload } from "./data";
import { getProductUseCases } from "./usecases";

export const productRouter = createTRPCRouter({
	getAllProducts: protectedProcedure.query(async ({}) => {
		const controller = getProductUseCases();
		return controller.getAllProducts();
	}),

	getProductById: protectedProcedure
		.input(z.object({ id: z.number() }))
		.query(async ({ input }) => {
			const controller = getProductUseCases();
			return controller.getProductById(input.id);
		}),

	createProduct: protectedProcedure
		.input(createProductPayload)
		.mutation(async ({ input }) => {
			const controller = getProductUseCases();
			return controller.createProduct(input);
		}),

	updateProduct: protectedProcedure
		.input(updateProductPayload)
		.mutation(async ({ input }) => {
			const controller = getProductUseCases();
			return controller.updateProduct(input);
		}),

	deleteProduct: protectedProcedure
		.input(z.object({ id: z.number() }))
		.mutation(async ({ input }) => {
			const controller = getProductUseCases();
			return controller.deleteProduct(input.id);
		}),

	refreshProductLinkId: protectedProcedure
		.input(z.object({ id: z.number() }))
		.mutation(async ({ input }) => {
			const controller = getProductUseCases();
			return controller.refreshProductLinkId(input.id);
		}),
});
