import { Request, Response, NextFunction } from "express";
import { isProKey } from "../services/keys.js";

interface UsageRecord {
  count: number;
  monthKey: string;
}

const usage = new Map<string, UsageRecord>();
const FREE_TIER_LIMIT = 100;

function getMonthKey(): string {
  const d = new Date();
  return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}`;
}

export function usageMiddleware(
  req: Request,
  res: Response,
  next: NextFunction
): void {
  const key = req.headers.authorization?.slice(7) || "unknown";
  const monthKey = getMonthKey();

  // Pro keys have no limit
  if (isProKey(key)) {
    trackUsage(key, monthKey);
    next();
    return;
  }

  // Free tier limit check
  const record = usage.get(key);
  if (record && record.monthKey === monthKey && record.count >= FREE_TIER_LIMIT) {
    res.status(429).json({
      error: "Free tier limit reached",
      limit: FREE_TIER_LIMIT,
      used: record.count,
      upgrade: "Upgrade to Pro for unlimited conversions: https://docfast.dev/pricing",
    });
    return;
  }

  trackUsage(key, monthKey);
  next();
}

function trackUsage(key: string, monthKey: string): void {
  const record = usage.get(key);
  if (!record || record.monthKey !== monthKey) {
    usage.set(key, { count: 1, monthKey });
  } else {
    record.count++;
  }
}

export function getUsageStats(): Record<string, { count: number; month: string }> {
  const stats: Record<string, { count: number; month: string }> = {};
  for (const [key, record] of usage) {
    const masked = key.slice(0, 8) + "...";
    stats[masked] = { count: record.count, month: record.monthKey };
  }
  return stats;
}
