-- Payment Kit: IAP multi-tenant backfill
-- Backfills metadata.bundle_id / metadata.package_name on existing Prices and
-- payment_details.app_store.bundle_id / payment_details.google_play.package_name
-- on existing Subscriptions, so Payment Kit can be wired into multiple iOS /
-- Android apps without same-SKU collisions across App Store / Play Console
-- namespaces (each store's SKU space is per-app, not global).
--
-- Backend code already filters Price.findOne by (sku, bundle_id) / (sku,
-- package_name); without this backfill, every pre-existing Price would stop
-- resolving the moment the new lookup ships.
--
-- Safety: tenant value is DERIVED from the configured PaymentMethods (which
-- store bundle_id / package_name as plain text under settings JSON — only
-- private keys are encrypted). The migration deliberately refuses to guess in
-- ambiguous setups:
--
--   * For Subscriptions we always use the sub's own
--     `default_payment_method_id` to resolve the tenant, which is a 1:1 map —
--     never ambiguous as long as the row points at a real PaymentMethod.
--   * For Prices we update only when EXACTLY ONE active PaymentMethod of the
--     matching type + livemode exists with a non-null tenant. Multi-tenant
--     installations (two iOS apps sharing one Payment Kit, etc.) skip the
--     Price backfill — admin must set bundle_id / package_name explicitly
--     because the migration can't safely guess which app a Price belongs to.
--
-- Idempotent. The IS NULL guards make re-runs a no-op for already-backfilled
-- rows, and the subquery filters skip rows that can't be resolved safely.

-- 1. Prices with App Store SKU → set bundle_id (only when one active
--    app_store PaymentMethod for the same livemode unambiguously identifies
--    the tenant).
UPDATE prices
SET metadata = json_set(
  metadata,
  '$.bundle_id',
  (SELECT json_extract(pm.settings, '$.app_store.bundle_id')
   FROM payment_methods pm
   WHERE pm.type = 'app_store'
     AND pm.livemode = prices.livemode
     AND pm.active = 1
     AND json_extract(pm.settings, '$.app_store.bundle_id') IS NOT NULL
   LIMIT 1)
)
WHERE json_extract(metadata, '$.app_store_product_id') IS NOT NULL
  AND json_extract(metadata, '$.bundle_id') IS NULL
  AND (
    SELECT COUNT(*) FROM payment_methods pm
    WHERE pm.type = 'app_store'
      AND pm.livemode = prices.livemode
      AND pm.active = 1
      AND json_extract(pm.settings, '$.app_store.bundle_id') IS NOT NULL
  ) = 1;

-- 2. Prices with Google Play SKU → set package_name (same single-tenant guard).
UPDATE prices
SET metadata = json_set(
  metadata,
  '$.package_name',
  (SELECT json_extract(pm.settings, '$.google_play.package_name')
   FROM payment_methods pm
   WHERE pm.type = 'google_play'
     AND pm.livemode = prices.livemode
     AND pm.active = 1
     AND json_extract(pm.settings, '$.google_play.package_name') IS NOT NULL
   LIMIT 1)
)
WHERE json_extract(metadata, '$.google_play_product_id') IS NOT NULL
  AND json_extract(metadata, '$.package_name') IS NULL
  AND (
    SELECT COUNT(*) FROM payment_methods pm
    WHERE pm.type = 'google_play'
      AND pm.livemode = prices.livemode
      AND pm.active = 1
      AND json_extract(pm.settings, '$.google_play.package_name') IS NOT NULL
  ) = 1;

-- 3. App Store Subscriptions → set payment_details.app_store.bundle_id from
--    the sub's own default_payment_method (1:1 — always safe).
UPDATE subscriptions
SET payment_details = json_set(
  payment_details,
  '$.app_store.bundle_id',
  (SELECT json_extract(pm.settings, '$.app_store.bundle_id')
   FROM payment_methods pm
   WHERE pm.id = subscriptions.default_payment_method_id)
)
WHERE channel = 'app_store'
  AND json_extract(payment_details, '$.app_store.bundle_id') IS NULL
  AND default_payment_method_id IS NOT NULL
  AND (
    SELECT json_extract(pm.settings, '$.app_store.bundle_id')
    FROM payment_methods pm
    WHERE pm.id = subscriptions.default_payment_method_id
  ) IS NOT NULL;

-- 4. Google Play Subscriptions → set payment_details.google_play.package_name.
UPDATE subscriptions
SET payment_details = json_set(
  payment_details,
  '$.google_play.package_name',
  (SELECT json_extract(pm.settings, '$.google_play.package_name')
   FROM payment_methods pm
   WHERE pm.id = subscriptions.default_payment_method_id)
)
WHERE channel = 'google_play'
  AND json_extract(payment_details, '$.google_play.package_name') IS NULL
  AND default_payment_method_id IS NOT NULL
  AND (
    SELECT json_extract(pm.settings, '$.google_play.package_name')
    FROM payment_methods pm
    WHERE pm.id = subscriptions.default_payment_method_id
  ) IS NOT NULL;
