-- CreateTable
CREATE TABLE IF NOT EXISTS "PlanEntitlement" (
    "id" TEXT NOT NULL,
    "tier" "SubscriptionTier" NOT NULL,
    "feature" TEXT NOT NULL,
    "limitValue" INTEGER,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "PlanEntitlement_pkey" PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX IF NOT EXISTS "PlanEntitlement_tier_feature_key" ON "PlanEntitlement"("tier", "feature");
CREATE INDEX IF NOT EXISTS "PlanEntitlement_tier_idx" ON "PlanEntitlement"("tier");

-- Seed defaults (only when table is empty)
INSERT INTO "PlanEntitlement" ("id", "tier", "feature", "limitValue")
SELECT v.id, v.tier::"SubscriptionTier", v.feature, v."limitValue"
FROM (VALUES
  ('pe-basic-semantic_search', 'BASIC', 'semantic_search', 30),
  ('pe-basic-auto_tag', 'BASIC', 'auto_tag', 15),
  ('pe-basic-auto_title', 'BASIC', 'auto_title', 5),
  ('pe-basic-brainstorm_create', 'BASIC', 'brainstorm_create', 1),
  ('pe-basic-brainstorm_expand', 'BASIC', 'brainstorm_expand', 10),
  ('pe-basic-brainstorm_enrich', 'BASIC', 'brainstorm_enrich', 20),
  ('pe-basic-suggest_charts', 'BASIC', 'suggest_charts', 5),
  ('pe-basic-ai_flashcard', 'BASIC', 'ai_flashcard', 5),
  ('pe-basic-voice_transcribe', 'BASIC', 'voice_transcribe', 20),
  ('pe-pro-semantic_search', 'PRO', 'semantic_search', 200),
  ('pe-pro-auto_tag', 'PRO', 'auto_tag', 500),
  ('pe-pro-auto_title', 'PRO', 'auto_title', 200),
  ('pe-pro-reformulate', 'PRO', 'reformulate', 50),
  ('pe-pro-chat', 'PRO', 'chat', 50),
  ('pe-pro-brainstorm_create', 'PRO', 'brainstorm_create', 5),
  ('pe-pro-brainstorm_expand', 'PRO', 'brainstorm_expand', 100),
  ('pe-pro-brainstorm_enrich', 'PRO', 'brainstorm_enrich', 200),
  ('pe-pro-suggest_charts', 'PRO', 'suggest_charts', 50),
  ('pe-pro-slide_generate', 'PRO', 'slide_generate', 20),
  ('pe-pro-excalidraw_generate', 'PRO', 'excalidraw_generate', 20),
  ('pe-pro-ai_flashcard', 'PRO', 'ai_flashcard', 100),
  ('pe-pro-voice_transcribe', 'PRO', 'voice_transcribe', 500),
  ('pe-business-semantic_search', 'BUSINESS', 'semantic_search', 1000),
  ('pe-business-auto_tag', 'BUSINESS', 'auto_tag', 1000),
  ('pe-business-auto_title', 'BUSINESS', 'auto_title', 1000),
  ('pe-business-reformulate', 'BUSINESS', 'reformulate', 500),
  ('pe-business-chat', 'BUSINESS', 'chat', 500),
  ('pe-business-brainstorm_create', 'BUSINESS', 'brainstorm_create', NULL),
  ('pe-business-brainstorm_expand', 'BUSINESS', 'brainstorm_expand', 500),
  ('pe-business-brainstorm_enrich', 'BUSINESS', 'brainstorm_enrich', 1000),
  ('pe-business-suggest_charts', 'BUSINESS', 'suggest_charts', 200),
  ('pe-business-slide_generate', 'BUSINESS', 'slide_generate', 100),
  ('pe-business-excalidraw_generate', 'BUSINESS', 'excalidraw_generate', 100),
  ('pe-business-ai_flashcard', 'BUSINESS', 'ai_flashcard', NULL),
  ('pe-business-voice_transcribe', 'BUSINESS', 'voice_transcribe', NULL),
  ('pe-enterprise-semantic_search', 'ENTERPRISE', 'semantic_search', NULL),
  ('pe-enterprise-auto_tag', 'ENTERPRISE', 'auto_tag', NULL),
  ('pe-enterprise-auto_title', 'ENTERPRISE', 'auto_title', NULL),
  ('pe-enterprise-reformulate', 'ENTERPRISE', 'reformulate', NULL),
  ('pe-enterprise-chat', 'ENTERPRISE', 'chat', NULL),
  ('pe-enterprise-brainstorm_create', 'ENTERPRISE', 'brainstorm_create', NULL),
  ('pe-enterprise-brainstorm_expand', 'ENTERPRISE', 'brainstorm_expand', NULL),
  ('pe-enterprise-brainstorm_enrich', 'ENTERPRISE', 'brainstorm_enrich', NULL),
  ('pe-enterprise-suggest_charts', 'ENTERPRISE', 'suggest_charts', NULL),
  ('pe-enterprise-slide_generate', 'ENTERPRISE', 'slide_generate', NULL),
  ('pe-enterprise-excalidraw_generate', 'ENTERPRISE', 'excalidraw_generate', NULL),
  ('pe-enterprise-ai_flashcard', 'ENTERPRISE', 'ai_flashcard', NULL),
  ('pe-enterprise-voice_transcribe', 'ENTERPRISE', 'voice_transcribe', NULL)
) AS v(id, tier, feature, "limitValue")
WHERE NOT EXISTS (SELECT 1 FROM "PlanEntitlement" LIMIT 1);
