-- ═══════════════════════════════════════════════════════
-- 知习 会员基础数据 Seed (幂等)
-- 
-- 执行方式: mysql -u root -p < seed.sql
-- 幂等: 使用 INSERT IGNORE + ON DUPLICATE KEY UPDATE
-- 
-- 生产执行前必须: mysqldump zhixi_prod > backup_$(date +%Y%m%d).sql
-- ═══════════════════════════════════════════════════════

-- ── 1. MembershipPlan ──
-- NOTE: maxStorageBytes is BIGINT; PlanQuota.limit is INT (max 2147483647)
-- Storage bytes quota uses INT_MAX for "unlimited" in current schema

INSERT INTO MembershipPlan (id, code, name, priceMonthly, priceYearly,
  maxKnowledgeBases, maxStorageBytes, maxFileSizeBytes,
  monthlyOcrPages, monthlyVisionPages, monthlyChatCount, monthlyAiAnalysisCount,
  monthlyRecallCount, monthlyCardGenCount, monthlyQuizCount,
  isActive, updatedAt)
VALUES
  ('free_prod_plan', 'free', 'Free',
   0, 0,
   1, 104857600, 10485760,           -- 1 KB, 100MB, 10MB
   0, 0, 10, 3,
   5, 3, 0,
   1, NOW()),
  ('premium_prod_plan', 'premium', 'Premium',
   0, 0,
   10, 10737418240, 209715200,        -- 10 KB, 10GB, 200MB
   0, 0, 100, 10,
   30, 10, 5,
   1, NOW())
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  monthlyChatCount = VALUES(monthlyChatCount),
  monthlyAiAnalysisCount = VALUES(monthlyAiAnalysisCount),
  monthlyRecallCount = VALUES(monthlyRecallCount),
  monthlyCardGenCount = VALUES(monthlyCardGenCount),
  monthlyQuizCount = VALUES(monthlyQuizCount),
  maxKnowledgeBases = VALUES(maxKnowledgeBases),
  maxStorageBytes = VALUES(maxStorageBytes),
  maxFileSizeBytes = VALUES(maxFileSizeBytes),
  updatedAt = NOW();

-- ── 2. PlanQuota (idempotent) ──
-- NOTE: PlanQuota.limit 是 INT，最大 2147483647 (约 2GB)。
-- 超出此值的存储配额由 BIGINT maxStorageBytes 控制，不在此表体现。

INSERT IGNORE INTO PlanQuota (id, planId, quotaType, `limit`, unit, windowType, isUnlimited, updatedAt)
VALUES
  -- Free plan quotas
  (UUID(), 'free_prod_plan', 'active_recall',        5,  'count',  'daily',    false, NOW()),
  (UUID(), 'free_prod_plan', 'feynman_evaluation',    2,  'count',  'daily',    false, NOW()),
  (UUID(), 'free_prod_plan', 'quiz_generation',       1,  'count',  'daily',    false, NOW()),
  (UUID(), 'free_prod_plan', 'learning_analysis',     1,  'count',  'daily',    false, NOW()),
  (UUID(), 'free_prod_plan', 'flashcard_generation',  1,  'count',  'daily',    false, NOW()),
  (UUID(), 'free_prod_plan', 'ai_chat_messages',     10,  'count',  'monthly',  false, NOW()),
  (UUID(), 'free_prod_plan', 'knowledge_base_count',  1,  'count',  'lifetime', false, NOW()),
  (UUID(), 'free_prod_plan', 'storage_bytes',        1073741824, 'bytes', 'lifetime', false, NOW()),      -- 1GB (INT safe)
  (UUID(), 'free_prod_plan', 'file_size_bytes',      10485760,   'bytes', 'per_request', false, NOW()),

  -- Premium plan quotas
  (UUID(), 'premium_prod_plan', 'active_recall',         30,  'count', 'daily',    false, NOW()),
  (UUID(), 'premium_prod_plan', 'feynman_evaluation',    10,  'count', 'daily',    false, NOW()),
  (UUID(), 'premium_prod_plan', 'quiz_generation',        5,  'count', 'daily',    false, NOW()),
  (UUID(), 'premium_prod_plan', 'learning_analysis',      5,  'count', 'daily',    false, NOW()),
  (UUID(), 'premium_prod_plan', 'flashcard_generation',   5,  'count', 'daily',    false, NOW()),
  (UUID(), 'premium_prod_plan', 'ai_chat_messages',     100,  'count', 'monthly',  false, NOW()),
  (UUID(), 'premium_prod_plan', 'knowledge_base_count',  10,  'count', 'lifetime', false, NOW()),
  (UUID(), 'premium_prod_plan', 'storage_bytes',        2147483647, 'bytes', 'lifetime', false, NOW()),    -- INT_MAX ≈ 2GB
  (UUID(), 'premium_prod_plan', 'file_size_bytes',      209715200,  'bytes', 'per_request', false, NOW());

-- ── 3. StoreProduct ──

INSERT IGNORE INTO StoreProduct (id, platform, productId, planId, billingPeriod, isActive, updatedAt)
VALUES
  (UUID(), 'apple', 'zhixi.premium.monthly',  'premium_prod_plan', 'monthly', true, NOW()),
  (UUID(), 'apple', 'zhixi.premium.yearly',   'premium_prod_plan', 'yearly',  true, NOW());
