import { env } from "cloudflare:workers";
import type Stripe from "stripe";
import { all, first } from "../db";
import { accountDb } from "../scoped-db";
import { customDomainCounts } from "../metering/directory";
import { locatedScopes, registerLocator } from "../scoped-db/locators";
import { backfillAccounts } from "../backfill";
import { sitesOwnedBy } from "../sites/store";
import { badRequest, conflict, paymentRequired } from "../errors";
import { billingSettings } from "../metering/quota";
import { CUSTOM_DOMAIN_LIMIT } from "~/lib/pricing";
import { ACTIVE_STATUSES } from "./checkout";
import { stripe, stripeEnabled } from "./config";
import { notifyUsers } from "../notify/deliver";
import { publicUrl } from "../events/emit";
export const DOMAIN_ADDON = "custom_domains";
/** How long custom domains keep serving after the add-on lapses, so a failed card can be fixed first. */
export const DOMAIN_GRACE_MS = 7 * 24 * 60 * 60 * 1000;
export type AddonRow = {
account_id: string;
subscription_id: string | null;
item_id: string | null;
status: string | null;
quantity: number;
lapsed_at: number | null;
};
/** Whether custom domains are sold as the Stripe add-on; otherwise plan flags and admin grants decide. */
export function domainAddonBilled(): boolean {
return stripeEnabled() && Boolean(env.STRIPE_PRICE_CUSTOM_DOMAIN);
}
export function isDomainAddonPrice(price: Pick<Stripe.Price, "id" | "metadata" | "lookup_key"> | null | undefined) {
if (!price) return false;
if (env.STRIPE_PRICE_CUSTOM_DOMAIN && price.id === env.STRIPE_PRICE_CUSTOM_DOMAIN) return true;
return price.metadata?.addon === DOMAIN_ADDON || price.lookup_key === "gitfor_custom_domain_monthly";
}