import type { IShareData } from "@ax/api";

import { differenceInCalendarDays, isBefore } from "date-fns";

import { formatDate } from "./dates";

const TOKEN_CAN_BE_RENEWED_DAYS = 14;
const TOKEN_EXPIRING_SOON_DAYS = 3;

// DEV ONLY — set a future/past date to simulate token states, then revert to undefined
export const DEV_NOW: Date | undefined = undefined; // new Date("2026-08-06");

export const getShareTokenInfo = (shareData: IShareData, now = new Date()) => {
	const expirationDate = new Date(shareData.endDate);
	const tokenHasExpired = isBefore(expirationDate, now);
	const validTokenDaysUntilExpires = Math.max(0, differenceInCalendarDays(expirationDate, now));

	return {
		tokenExpirationDate: formatDate(expirationDate),
		tokenHasExpired,
		tokenCanBeRenewed: !tokenHasExpired && validTokenDaysUntilExpires < TOKEN_CAN_BE_RENEWED_DAYS,
		tokenExpiringSoon: !tokenHasExpired && validTokenDaysUntilExpires <= TOKEN_EXPIRING_SOON_DAYS,
		validTokenDaysUntilExpires,
	};
};

export type ShareTokenInfo = ReturnType<typeof getShareTokenInfo>;
