import { getMonthRecord, setMonthRecord } from './db';

const MAX_COUNT = 3;
const SALT = 'my_secret_salt'; // 防篡改用，可选

export async function checkLimit(): Promise<{ allowed: boolean; remaining: number }> {
  const month = new Date().toISOString().slice(0, 7); // YYYY-MM
  const record = await getMonthRecord(month);
  const count = record?.count || 0;
  return {
    allowed: count < MAX_COUNT,
    remaining: MAX_COUNT - count,
  };
}

export async function incrementCount(): Promise<void> {
  const month = new Date().toISOString().slice(0, 7);
  const record = await getMonthRecord(month);
  const count = record?.count || 0;
  await setMonthRecord(month, count + 1);
}
