import { requireUser } from "../../utils/auth";
import { computeProtectionCoverage } from "../../db/protectionStateLog";

/**
 * GET /api/protection/coverage
 *
 * Returns protection coverage and streak metrics computed read-time from
 * the protection_state_log table (no cron, no materialized state).
 *
 * Response shape (spec §3):
 * {
 *   firstProtectionAt: string | null,  // ISO-8601, null = never activated
 *   protectedDays:     number,
 *   unprotectedDays:   number,
 *   currentStreakDays: number,
 *   longestStreakDays: number,
 * }
 *
 * All values are 0 / null when the user has never activated protection.
 */
export default defineEventHandler(async (event) => {
  const user = await requireUser(event);
  const coverage = await computeProtectionCoverage(user.id);
  return { success: true, data: coverage };
});
