export type PlanLimitKey =
  | 'gates'
  | 'estateAdmins'
  | 'supervisors'
  | 'guards'
  | 'residents'
  | 'visitorPassesPerMonth'
  | 'announcementsPerMonth'
  | 'announcementsPerWeek'
  | 'entryExitNotificationsPerResidentPerWeek'
  | 'sosAlertsPerMonth'
  | 'managedEstates'
  | 'storageMb';

export function getNumericPlanLimit(limits: unknown, key: PlanLimitKey): number | null {
  if (!limits || typeof limits !== 'object' || Array.isArray(limits)) {
    return null;
  }
  const value = (limits as Record<string, unknown>)[key];
  if (value === null || value === undefined) {
    return null;
  }
  const numberValue = Number(value);
  return Number.isFinite(numberValue) ? numberValue : null;
}

export function startOfCurrentMonth(now = new Date()) {
  return new Date(now.getFullYear(), now.getMonth(), 1);
}

export function startOfCurrentWeek(now = new Date()) {
  const date = new Date(now);
  const day = date.getDay();
  const diff = date.getDate() - day + (day === 0 ? -6 : 1);
  return new Date(date.getFullYear(), date.getMonth(), diff);
}
