-- CreateTable: subscription_tiers
CREATE TABLE "subscription_tiers" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "slug" VARCHAR(50) NOT NULL,
    "name" VARCHAR(100) NOT NULL,
    "description" TEXT,
    "monthly_price" DECIMAL(10,2) NOT NULL,
    "annual_price" DECIMAL(10,2) NOT NULL,
    "credit_allocation" INTEGER NOT NULL,
    "rate_limit" INTEGER NOT NULL,
    "allowed_models" JSONB NOT NULL DEFAULT '[]',
    "features" JSONB NOT NULL DEFAULT '[]',
    "paypal_plan_monthly" VARCHAR(100),
    "paypal_plan_annual" VARCHAR(100),
    "display_order" INTEGER NOT NULL DEFAULT 0,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "subscription_tiers_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "subscription_tiers_slug_key" ON "subscription_tiers"("slug");
CREATE INDEX "subscription_tiers_is_active_display_order_idx" ON "subscription_tiers"("is_active", "display_order");

-- Seed default tiers
INSERT INTO "subscription_tiers" ("id", "slug", "name", "description", "monthly_price", "annual_price", "credit_allocation", "rate_limit", "allowed_models", "features", "paypal_plan_monthly", "paypal_plan_annual", "display_order", "is_active", "updated_at")
VALUES
  (gen_random_uuid(), 'starter', 'Starter', 'Basic translation tier', 29.00, 290.00, 100000, 60, '["4b"]', '["100K tokens/month", "4B model", "Email support"]', NULL, NULL, 0, true, NOW()),
  (gen_random_uuid(), 'professional', 'Professional', 'Advanced translation tier', 99.00, 990.00, 500000, 120, '["4b", "27b"]', '["500K tokens/month", "All models", "Priority support", "Higher rate limits"]', NULL, NULL, 1, true, NOW()),
  (gen_random_uuid(), 'enterprise', 'Enterprise', 'Enterprise translation tier', 299.00, 2990.00, 2000000, 0, '["4b", "27b"]', '["2M tokens/month", "All models", "SLA", "Unlimited rate limits", "Dedicated support"]', NULL, NULL, 2, true, NOW());

-- Convert plan_tier columns from enum to varchar
ALTER TABLE "subscriptions" ALTER COLUMN "plan_tier" TYPE VARCHAR(50) USING "plan_tier"::VARCHAR;
ALTER TABLE "licenses" ALTER COLUMN "plan_tier" TYPE VARCHAR(50) USING "plan_tier"::VARCHAR;

-- Drop the PlanTier enum type
DROP TYPE IF EXISTS "PlanTier";
