import { initTRPC, TRPCError } from "@trpc/server";
import { validateSession } from "../userAuth.js";
import { getSessionCookie, setSessionCookie, clearSessionCookie, } from "../sessionCookies.js";
import { Label, logger } from "../logger.js";
import { z } from "zod";
// Context creator function
export async function createContext({ req, res, }) {
    const sessionId = getSessionCookie(req);
    let user = null;
    if (sessionId) {
        // Validate the session and get user if valid
        const validatedUser = await validateSession(sessionId);
        if (validatedUser) {
            user = {
                id: validatedUser.id,
                username: validatedUser.username,
            };
        }
    }
    return {
        req,
        res,
        user,
        setSession: (sessionId) => setSessionCookie(res, sessionId),
        deleteSession: () => clearSessionCookie(res),
    };
}
// Initialize tRPC
const t = initTRPC.context().create();
// Base router and procedure helpers
export const router = t.router;
export const unauthedProcedure = t.procedure;
// Procedure that requires authentication
export const authedProcedure = t.procedure.use(async ({ ctx, next }) => {
    if (!ctx.user) {
        logger.warn({
            label: Label.AUTH,
            message: `Unauthorized access attempt to protected procedure`,
        });
        throw new TRPCError({ code: "UNAUTHORIZED" });
    }
    return next({ ctx: { ...ctx, user: ctx.user } });
});
// Input validators
export const loginInputSchema = z.object({
    username: z.string().min(1, "Username is required"),
    password: z.string().min(1, "Password is required"),
});
export const setupInputSchema = z.object({
    username: z.string().min(1, "Username is required"),
    password: z.string().min(8, "Password must be at least 8 characters"),
});
//# sourceMappingURL=index.js.map