import { OrderRepository } from "$lib/domains/order/data/repository.js";
import { OrderController } from "$lib/domains/order/domain/controller.js";
import { db } from "@pkg/db";
import { getError } from "@pkg/logger";
import { ERROR_CODES } from "@pkg/result";
import type { PageServerLoad } from "./$types.js";

export const load: PageServerLoad = async ({ params }) => {
	const oid = parseInt(params.oid);
	if (!oid || isNaN(oid) || oid < 0 || oid > Number.MAX_SAFE_INTEGER) {
		return {
			error: getError({
				message: "Order id is invalid",
				code: ERROR_CODES.INPUT_ERROR,
				detail: "Order id is invalid",
				userHint: "Provide a valid order id",
				actionable: false,
			}),
		};
	}
	return await new OrderController(new OrderRepository(db)).getOrder(oid);
};
