import { getCustomerInfoUseCases } from "$lib/domains/customerinfo/usecases";
import { PaymentInfoRepository } from "$lib/domains/paymentinfo/repository";
import { db } from "@pkg/db";
import { getError } from "@pkg/logger";
import { ERROR_CODES } from "@pkg/result";
import type { PageServerLoad } from "./$types";

export const load: PageServerLoad = async ({ params }) => {
	const uid = parseInt(params.uid);
	if (!uid || isNaN(uid) || uid < 0 || uid > 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,
			}),
		};
	}

	const cinfo = await getCustomerInfoUseCases().getAllCustomerInfo();
	const piRes = await new PaymentInfoRepository(db).getPaymentInfo(uid);

	return {
		customerInfo: cinfo.data,
		paymentInfo: piRes.data,
		error: piRes.error || cinfo.error,
	};
};
