-- Payment Kit: IAP foundation
-- Adds schema needed for App Store + Google Play subscription channels.
-- Mirrors blocklets/core/api/src/store/migrations/20260526-iap-foundation.ts.
--
-- NOTE: SQLite does not support `ALTER TABLE ADD COLUMN IF NOT EXISTS`.
-- Wrangler tracks applied migrations in the `d1_migrations` table and skips
-- re-application by filename, so re-running this file via `wrangler d1
-- migrations apply` is safe. Do not invoke via `wrangler d1 execute` more
-- than once on the same database.

-- 1. Customer: per-channel UUID for IAP appAccountToken / obfuscatedAccountId mapping (D-004)
ALTER TABLE customers ADD COLUMN app_store_uuid VARCHAR(36);
ALTER TABLE customers ADD COLUMN google_play_uuid VARCHAR(36);
CREATE UNIQUE INDEX IF NOT EXISTS idx_customers_app_store_uuid
  ON customers(app_store_uuid)
  WHERE app_store_uuid IS NOT NULL;
CREATE UNIQUE INDEX IF NOT EXISTS idx_customers_google_play_uuid
  ON customers(google_play_uuid)
  WHERE google_play_uuid IS NOT NULL;

-- 2. Subscription: channel + environment (D-005)
ALTER TABLE subscriptions ADD COLUMN channel VARCHAR(20);
ALTER TABLE subscriptions ADD COLUMN environment VARCHAR(20) DEFAULT 'production';

-- 3. Invoice: three-segment amounts (D-001 A)
ALTER TABLE invoices ADD COLUMN gross_amount VARCHAR(32);
ALTER TABLE invoices ADD COLUMN platform_fee VARCHAR(32) DEFAULT '0';
ALTER TABLE invoices ADD COLUMN net_amount VARCHAR(32);
UPDATE invoices SET gross_amount = total, net_amount = total WHERE gross_amount IS NULL;

-- 4. Refund: origin source (merchant_initiated | platform_initiated)
ALTER TABLE refunds ADD COLUMN source VARCHAR(30) DEFAULT 'merchant_initiated';

-- 5. Entitlement tables (D-003 B)
CREATE TABLE IF NOT EXISTS `entitlements` (
  `id` VARCHAR(30) NOT NULL PRIMARY KEY,
  `livemode` TINYINT(1) NOT NULL,
  `key` VARCHAR(64) NOT NULL UNIQUE,
  `name` VARCHAR(255),
  `description` TEXT,
  `metadata` JSON,
  `created_at` DATETIME NOT NULL,
  `updated_at` DATETIME NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS `idx_entitlements_key` ON `entitlements` (`key`);

CREATE TABLE IF NOT EXISTS `entitlement_products` (
  `entitlement_id` VARCHAR(30) NOT NULL,
  `product_id` VARCHAR(30) NOT NULL,
  `created_at` DATETIME NOT NULL,
  `updated_at` DATETIME NOT NULL,
  PRIMARY KEY (`entitlement_id`, `product_id`)
);

CREATE TABLE IF NOT EXISTS `entitlement_grants` (
  `id` VARCHAR(30) NOT NULL PRIMARY KEY,
  `livemode` TINYINT(1) NOT NULL,
  `entitlement_id` VARCHAR(30) NOT NULL,
  `customer_id` VARCHAR(18) NOT NULL,
  `source_subscription_id` VARCHAR(30),
  `source_channel` VARCHAR(20) NOT NULL,
  `active_from` INTEGER NOT NULL,
  `active_until` INTEGER NOT NULL,
  `status` VARCHAR(20) NOT NULL DEFAULT 'active',
  `metadata` JSON,
  `created_at` DATETIME NOT NULL,
  `updated_at` DATETIME NOT NULL
);
CREATE INDEX IF NOT EXISTS `idx_entitlement_grants_lookup`
  ON `entitlement_grants` (`customer_id`, `entitlement_id`, `status`);
CREATE INDEX IF NOT EXISTS `idx_entitlement_grants_source_sub`
  ON `entitlement_grants` (`source_subscription_id`);
