import { z } from "zod";
import { SUBSCRIPTION_PLANS } from "@app/shared-types";

/**
 * Klienten skickar kvitto/token efter köp; backend verifierar mot butiken
 * och är source of truth (spec §47, §61.14).
 */
export const verifyPurchaseInputSchema = z.discriminatedUnion("provider", [
  z.object({
    provider: z.literal("apple"),
    /** App Store Server API: signerad transaktion från StoreKit 2. */
    signedTransaction: z.string().min(10),
  }),
  z.object({
    provider: z.literal("google"),
    packageName: z.string().min(3),
    productId: z.string().min(1),
    purchaseToken: z.string().min(10),
  }),
]);
export type VerifyPurchaseInput = z.infer<typeof verifyPurchaseInputSchema>;

export const restorePurchasesInputSchema = z.object({
  provider: z.enum(["apple", "google"]),
  payload: z.string().min(1),
});
export type RestorePurchasesInput = z.infer<typeof restorePurchasesInputSchema>;

/** Store-notiser tas emot rå, signaturverifieras och läggs på kö (spec §47). */
export const storeNotificationSchema = z.object({
  raw: z.unknown(),
});

export const adminGrantInputSchema = z.object({
  userId: z.uuid(),
  plan: z.enum(SUBSCRIPTION_PLANS),
  days: z.number().int().min(1).max(3650),
  reason: z.string().min(3).max(300),
});
export type AdminGrantInput = z.infer<typeof adminGrantInputSchema>;
