import db from "db/connection";
import { cheers } from "db/schema";
import { and, between, eq, type SQL } from "drizzle-orm";
import type User from "user";

export async function createCheerRecord(
	user: User,
	amount: number,
): Promise<void> {
	await db.insert(cheers).values({ user: parseInt(user.id, 10), amount });
}

export async function getCheers(user: User, monthData?: string) {
	let condition: SQL<unknown> | undefined = eq(
		cheers.user,
		parseInt(user.id, 10),
	);
	if (monthData) {
		const begin = Date.parse(monthData);
		const end = new Date(begin).setMonth(new Date(begin).getMonth() + 1);
		condition = and(
			condition,
			between(cheers.created, new Date(begin), new Date(end)),
		);
	}
	const data = await db.select().from(cheers).where(condition);
	return data;
}
