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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import { getPlanIdForStripePrice, isGrandfatheredProPrice } from "@/server/billing-plans";
import { getPrismaClient } from "@/server/prisma";
import { getServerStripeClient } from "@/server/stripe";
import type { BillingPlanId } from "@/lib/billing-plans";
import Stripe from "stripe";

// past_due stays entitled: Stripe is still retrying the invoice. unpaid and
// incomplete are not — retries are over, or the first invoice was never paid.
const ENTITLED_SUBSCRIPTION_STATUSES = new Set(["active", "trialing", "past_due"]);

// An upgrade invoice that is still open, or a subscription that is not in good
// standing, must not unlock a higher tier. The tier already paid for stays.
const UPGRADE_BLOCKING_SUBSCRIPTION_STATUSES = new Set([
  "incomplete",
  "incomplete_expired",
  "past_due",
  "unpaid",
  "canceled",
]);

type PaidPlan = BillingPlanId;

function toDate(unixSeconds: number | null | undefined): Date | null {
  if (!unixSeconds) {
    return null;
  }
  return new Date(unixSeconds * 1000);
}

function planRank(plan: string | null | undefined): number {
  if (plan === "scale") return 2;
  if (plan === "pro") return 1;
  return 0;
}

function asPaidPlan(plan: string | null | undefined): PaidPlan | null {
  if (plan === "pro" || plan === "scale") return plan;
  return null;
}

function higherPlan(current: PaidPlan | null, next: PaidPlan | null): PaidPlan | null {
  if (!next) return current;
  if (!current || planRank(next) > planRank(current)) return next;
  return current;
}

export function getPaidSubscriptionPlan(subscription: Stripe.Subscription): PaidPlan | null {
  if (!ENTITLED_SUBSCRIPTION_STATUSES.has(subscription.status)) {
    return null;
  }

  return pricePlanOnSubscription(subscription);
}

function priceIdOf(price: string | Stripe.Price | null | undefined): string | null {
  if (!price) return null;
  return typeof price === "string" ? price : price.id;
}

function planFromPriceId(priceId: string | null | undefined): PaidPlan | null {
  return asPaidPlan(getPlanIdForStripePrice(priceId));
}

/** Plan mapped from subscription item prices, ignoring subscription status. */
function pricePlanOnSubscription(subscription: Stripe.Subscription): PaidPlan | null {
  let plan: PaidPlan | null = null;
  for (const item of subscription.items?.data ?? []) {
    plan = higherPlan(plan, planFromPriceId(item.price?.id));
  }
  for (const item of subscription.pending_update?.subscription_items ?? []) {
    plan = higherPlan(plan, planFromPriceId(item.price?.id));
  }
  return plan;
}

function getSubscriptionPeriod(subscription: Stripe.Subscription): {
  currentPeriodStart: number | null;
  currentPeriodEnd: number | null;
} {
  const starts = subscription.items.data
    .map((item) => item.current_period_start)
    .filter((value): value is number => typeof value === "number");
  const ends = subscription.items.data
    .map((item) => item.current_period_end)
    .filter((value): value is number => typeof value === "number");

  return {
    currentPeriodStart: starts.length > 0 ? Math.min(...starts) : null,
    currentPeriodEnd: ends.length > 0 ? Math.max(...ends) : null,
  };
}

function customerIdOf(
  customer: string | { id: string } | null | undefined,
): string | null {
  if (!customer) return null;
  return typeof customer === "string" ? customer : customer.id;
}

type SubscriptionWriteOptions = {
  /** Persist this plan instead of the price currently on the subscription. */
  planOverride?: PaidPlan | "free";
  /** Only update a row still on this plan, so a paid grant cannot be overwritten. */
  onlyIfPlan?: string;
  /** Unpaid upgrades must not take over an App Store or affiliate entitlement. */
  restrictToStripeProvider?: boolean;
  statusOverride?: string;
  pendingPlan?: PaidPlan | null;
  pendingPlanInvoiceId?: string | null;
  /** Leave the paid billing window alone until the upgrade invoice is paid. */
  preserveBillingPeriod?: boolean;
};

export async function upsertSubscriptionState(
  subscription: Stripe.Subscription,
  options?: SubscriptionWriteOptions,
) {
  const prisma = getPrismaClient();
  const customerId = customerIdOf(subscription.customer);
  if (!customerId) return;

  const subscriptionId = subscription.id;
  const status = options?.statusOverride ?? subscription.status;
  const { currentPeriodStart, currentPeriodEnd } = getSubscriptionPeriod(subscription);
  // Portal cancellations set cancel_at_period_end (and usually cancel_at too).
  const cancelAt =
    subscription.cancel_at ?? (subscription.cancel_at_period_end ? currentPeriodEnd : null);

  const paidPlan =
    options?.planOverride === "free"
      ? null
      : options?.planOverride ?? getPaidSubscriptionPlan(subscription);
  const plan = options?.planOverride ?? paidPlan ?? "free";
  // Subscribers still on the pre-2026-10 Pro price keep 1080p, even if they
  // were past due when the flag was backfilled.
  const onGrandfatheredPrice =
    plan === "pro" && subscription.items.data.some((item) => isGrandfatheredProPrice(item.price?.id));

  // A non-paid Stripe state must not clobber an entitlement owned by the App
  // Store; only a paid Stripe grant may take over from an Apple subscription.
  const writingPaidGrant = plan === "pro" || plan === "scale";
  const providerFilter =
    options?.restrictToStripeProvider || !writingPaidGrant
      ? { OR: [{ subscription_provider: "stripe" }, { subscription_provider: null }] }
      : {};

  await prisma.user.updateMany({
    where: {
      stripe_customer_id: customerId,
      ...(options?.onlyIfPlan ? { plan: options.onlyIfPlan } : {}),
      ...providerFilter,
    },
    data: {
      plan,
      subscription_status: status,
      subscription_provider: "stripe",
      stripe_subscription_id: subscriptionId,
      ...(options?.preserveBillingPeriod
        ? {}
        : {
            billing_period_start: toDate(currentPeriodStart),
            billing_period_end: toDate(currentPeriodEnd),
          }),
      trial_ends_at: toDate(subscription.trial_end),
      subscription_cancel_at: toDate(cancelAt),
      pending_plan: options?.pendingPlan ?? null,
      pending_plan_invoice_id: options?.pendingPlanInvoiceId ?? null,
      // Grandfathered 1080p ends once the subscription is cancelled; a failed
      // payment (past_due/unpaid) keeps it so recovering customers retain it.
      ...(onGrandfatheredPrice
        ? { legacy_hd: true }
        : status === "canceled" || status === "incomplete_expired"
          ? { legacy_hd: false }
          : {}),
    },
  });
}

/**
 * Status to store while a higher tier is waiting on payment.
 * `unpaid` and `canceled` would drop access in billing even if `plan` stays
 * on the tier already paid for, so those stay `past_due` (access continues,
 * and the billing screen still asks for a payment update).
 */
function statusWhileUpgradeUnpaid(
  incoming: string,
  previous: string | null | undefined,
  keptPlan: PaidPlan | "free",
): string {
  if (keptPlan === "free") return incoming;
  if (incoming === "active" || incoming === "trialing" || incoming === "past_due") return incoming;
  if (previous === "trialing") return "trialing";
  return "past_due";
}

/** Paid, or nothing left to collect. Void and uncollectible invoices are not. */
function invoiceSettlesUpgrade(invoice: Stripe.Invoice | null | undefined): boolean {
  if (!invoice) return false;
  if (invoice.status === "void" || invoice.status === "uncollectible") return false;
  if (invoice.status === "paid") return true;
  return invoice.amount_due === 0;
}

async function resolveLatestInvoice(subscription: Stripe.Subscription): Promise<Stripe.Invoice | null> {
  const latest = subscription.latest_invoice;
  if (!latest) return null;
  if (typeof latest !== "string") return latest;
  return getServerStripeClient().invoices.retrieve(latest);
}

function invoiceIdOf(invoice: Stripe.Invoice | string | null | undefined): string | null {
  if (!invoice) return null;
  return typeof invoice === "string" ? invoice : invoice.id;
}

/**
 * `customer.subscription.updated`. Downgrades and same-tier changes are written
 * immediately. A higher tier is written only when its latest invoice is paid
 * (or amount_due is 0) and the subscription is active or trialing. Otherwise
 * the previous paid tier stays, and the unpaid tier is stored as pending.
 */
export async function syncSubscriptionUpdate(subscription: Stripe.Subscription) {
  const prisma = getPrismaClient();
  const customerId = customerIdOf(subscription.customer);
  if (!customerId) {
    await upsertSubscriptionState(subscription);
    return;
  }

  const user = await prisma.user.findFirst({
    where: { stripe_customer_id: customerId },
    select: {
      plan: true,
      subscription_status: true,
      subscription_provider: true,
    },
  });

  // No linked user yet (checkout links the customer before the first paid
  // event). Apply the subscription as-is; checkout.session.completed does too.
  if (!user) {
    await upsertSubscriptionState(subscription);
    return;
  }

  const targetPlan = pricePlanOnSubscription(subscription);
  const currentPlan = asPaidPlan(user.plan) ?? "free";
  const isUpgrade = Boolean(targetPlan && planRank(targetPlan) > planRank(currentPlan));

  if (!isUpgrade) {
    await upsertSubscriptionState(subscription);
    return;
  }

  // App Store and affiliate Pro stay put until the upgrade invoice is paid.
  // invoice.paid grants the higher tier and switches the provider then.
  if (user.subscription_provider === "apple" || user.subscription_provider === "affiliate") {
    const invoice = await resolveLatestInvoice(subscription);
    if (
      invoiceSettlesUpgrade(invoice) &&
      !UPGRADE_BLOCKING_SUBSCRIPTION_STATUSES.has(subscription.status)
    ) {
      await upsertSubscriptionState(subscription, { planOverride: targetPlan! });
    }
    return;
  }

  const invoice = await resolveLatestInvoice(subscription);
  const blockedBySubscription = UPGRADE_BLOCKING_SUBSCRIPTION_STATUSES.has(subscription.status);
  const blockedByInvoice = !invoiceSettlesUpgrade(invoice);

  if (!blockedBySubscription && !blockedByInvoice) {
    await upsertSubscriptionState(subscription, { planOverride: targetPlan! });
    return;
  }

  const keptPlan = currentPlan === "free" ? "free" : currentPlan;
  await upsertSubscriptionState(subscription, {
    planOverride: keptPlan,
    onlyIfPlan: user.plan,
    restrictToStripeProvider: true,
    preserveBillingPeriod: true,
    statusOverride: statusWhileUpgradeUnpaid(subscription.status, user.subscription_status, keptPlan),
    pendingPlan: targetPlan!,
    pendingPlanInvoiceId: invoiceIdOf(invoice) ?? invoiceIdOf(subscription.latest_invoice),
  });
}

function priceIdFromInvoiceLine(line: Stripe.InvoiceLineItem): string | null {
  const priced = line.pricing?.price_details?.price;
  const fromPricing = priceIdOf(priced);
  if (fromPricing) return fromPricing;
  // Webhook endpoints still on an older API version send `price` on the line.
  const legacy = (line as Stripe.InvoiceLineItem & { price?: string | Stripe.Price | null }).price;
  return priceIdOf(legacy);
}

/** Highest plan on a non-credit line. Proration credits (negative amounts) are the old price. */
function highestPlanOnInvoice(invoice: Stripe.Invoice): PaidPlan | null {
  let plan: PaidPlan | null = null;
  for (const line of invoice.lines?.data ?? []) {
    if (typeof line.amount === "number" && line.amount < 0) continue;
    plan = higherPlan(plan, planFromPriceId(priceIdFromInvoiceLine(line)));
  }
  return plan;
}

function invoiceSubscriptionId(invoice: Stripe.Invoice): string | null {
  const parentSub = invoice.parent?.subscription_details?.subscription;
  if (typeof parentSub === "string") return parentSub;
  if (parentSub && typeof parentSub === "object" && "id" in parentSub) return parentSub.id;

  const legacy = (invoice as Stripe.Invoice & { subscription?: string | Stripe.Subscription | null }).subscription;
  if (typeof legacy === "string") return legacy;
  if (legacy && typeof legacy === "object") return legacy.id;

  for (const line of invoice.lines?.data ?? []) {
    if (typeof line.subscription === "string") return line.subscription;
    if (line.subscription && typeof line.subscription === "object") return line.subscription.id;
  }
  return null;
}

function plansStrictlyBelow(plan: PaidPlan): string[] {
  return plan === "scale" ? ["free", "pro"] : ["free"];
}

/**
 * `invoice.paid` and `invoice.payment_succeeded`. Grants a pending higher tier,
 * or the tier on the invoice when that event arrives before subscription.updated.
 * Applying the same invoice twice does not change a plan that is already granted.
 */
export async function applyPaidUpgradeInvoice(invoice: Stripe.Invoice) {
  if (!invoiceSettlesUpgrade(invoice)) return;

  const prisma = getPrismaClient();
  const customerId = customerIdOf(invoice.customer);
  if (!customerId || !invoice.id) return;

  const user = await prisma.user.findFirst({
    where: { stripe_customer_id: customerId },
    select: {
      plan: true,
      pending_plan: true,
      pending_plan_invoice_id: true,
      subscription_provider: true,
    },
  });
  if (!user || user.subscription_provider === "apple") return;

  const invoicePlan = highestPlanOnInvoice(invoice);
  const pendingPlan = asPaidPlan(user.pending_plan);
  // Only the invoice that subscription.updated stored may grant a pending tier
  // when the invoice lines themselves do not name that price. A different paid
  // invoice (a renewal of the current tier) must not.
  const pendingMatchesInvoice =
    pendingPlan !== null && user.pending_plan_invoice_id === invoice.id;

  let grant: PaidPlan | null = null;
  if (invoicePlan && planRank(invoicePlan) > planRank(user.plan)) {
    grant = invoicePlan;
  } else if (pendingMatchesInvoice && pendingPlan && planRank(pendingPlan) > planRank(user.plan)) {
    grant = pendingPlan;
  }

  if (!grant) {
    if (user.pending_plan_invoice_id === invoice.id) {
      await prisma.user.updateMany({
        where: { stripe_customer_id: customerId, pending_plan_invoice_id: invoice.id },
        data: { pending_plan: null, pending_plan_invoice_id: null },
      });
    }
    return;
  }

  const subscriptionId = invoiceSubscriptionId(invoice);
  await prisma.user.updateMany({
    where: {
      stripe_customer_id: customerId,
      plan: { in: plansStrictlyBelow(grant) },
      OR: [
        { subscription_provider: "stripe" },
        { subscription_provider: null },
        { subscription_provider: "affiliate" },
      ],
    },
    data: {
      plan: grant,
      pending_plan: null,
      pending_plan_invoice_id: null,
      subscription_provider: "stripe",
      subscription_status: "active",
      ...(subscriptionId ? { stripe_subscription_id: subscriptionId } : {}),
    },
  });
}

/**
 * `invoice.payment_failed`. Records the unpaid higher tier and leaves the plan
 * the customer already paid for in place. A later paid event can still grant it.
 */
export async function noteUpgradePaymentFailed(invoice: Stripe.Invoice) {
  if (invoice.status === "void") return;

  const prisma = getPrismaClient();
  const customerId = customerIdOf(invoice.customer);
  if (!customerId || !invoice.id) return;

  const user = await prisma.user.findFirst({
    where: { stripe_customer_id: customerId },
    select: {
      plan: true,
      pending_plan: true,
      pending_plan_invoice_id: true,
      subscription_provider: true,
    },
  });
  if (!user || user.subscription_provider === "apple" || user.subscription_provider === "affiliate") {
    return;
  }

  const invoicePlan = highestPlanOnInvoice(invoice);
  const pendingPlan = asPaidPlan(user.pending_plan);
  const failedUpgrade =
    (invoicePlan && planRank(invoicePlan) > planRank(user.plan) ? invoicePlan : null) ??
    (user.pending_plan_invoice_id === invoice.id ? pendingPlan : null);

  // Already on this tier (or a higher one): a failed charge must not revoke it.
  if (!failedUpgrade || planRank(failedUpgrade) <= planRank(user.plan)) return;

  await prisma.user.updateMany({
    where: {
      stripe_customer_id: customerId,
      plan: user.plan,
      OR: [{ subscription_provider: "stripe" }, { subscription_provider: null }],
    },
    data: {
      pending_plan: failedUpgrade,
      pending_plan_invoice_id: invoice.id,
    },
  });
}

/**
 * Rows written before past_due kept its plan still say `free`. The next
 * subscription webhook would fix them; this applies the same sync on read so
 * a customer already inside the retry window is not stuck on Free.
 * Returns true when the stored subscription was rewritten and the caller
 * should reload billing.
 */
export async function restorePastDueEntitlement(userId: string): Promise<boolean> {
  const prisma = getPrismaClient();
  const user = await prisma.user.findUnique({
    where: { id: userId },
    select: {
      plan: true,
      subscription_status: true,
      subscription_provider: true,
      stripe_subscription_id: true,
      pending_plan: true,
    },
  });
  if (
    !user?.stripe_subscription_id ||
    user.subscription_provider !== "stripe" ||
    user.subscription_status !== "past_due" ||
    user.plan === "pro" ||
    user.plan === "scale" ||
    user.pending_plan === "pro" ||
    user.pending_plan === "scale"
  ) {
    return false;
  }

  const subscription = await getServerStripeClient().subscriptions.retrieve(user.stripe_subscription_id);
  await upsertSubscriptionState(subscription);
  return true;
}