import { eq, type Database } from "@pkg/db";
import { paymentInfo } from "@pkg/db/schema";
import { Logger } from "@pkg/logger";
import type { Result } from "@pkg/result";
import { paymentInfoModel, type PaymentInfo } from "./data";

export class PaymentInfoRepository {
	constructor(private db: Database) {}

	async getPaymentInfoByOrderID(oid: number): Promise<Result<PaymentInfo>> {
		Logger.info(`Getting payment info with id ${oid}`);
		const out = await this.db.query.paymentInfo.findFirst({
			where: eq(paymentInfo.orderId, oid),
		});
		const parsed = paymentInfoModel.safeParse(out);
		if (parsed.error) {
			Logger.error(parsed.error);
			return {};
		}
		return { data: parsed.data };
	}
}
