import { prisma } from "./db";
import { APP_LIMITS } from "./plans";

export async function getQuotaInfo(userId: string) {
  const user = await prisma.user.findUniqueOrThrow({ where: { id: userId } });

  return {
    used: user.createdVideoCount,
    createdVideoCount: user.createdVideoCount,
    maxBatchSize: APP_LIMITS.maxBatchSize,
    maxResolutionHeight: APP_LIMITS.maxResolutionHeight,
    watermarkOptional: true,
    customWatermark: true,
    perItemImages: true,
    artTrackLayouts: true,
    selfHosted: true,
    unlimited: true,
  };
}

export async function checkQuota(userId: string, requestedCount: number) {
  const info = await getQuotaInfo(userId);
  if (requestedCount > info.maxBatchSize) {
    return {
      ok: false as const,
      error: `Batch limit exceeded. Max ${info.maxBatchSize} files per batch.`,
      ...info,
    };
  }

  return { ok: true as const, ...info };
}

export async function reserveQuota(userId: string, count: number) {
  if (count > APP_LIMITS.maxBatchSize) {
    throw new Error(`Batch limit exceeded. Max ${APP_LIMITS.maxBatchSize} files per batch.`);
  }
  await prisma.user.findUniqueOrThrow({ where: { id: userId } });
}
