import { afterEach, describe, expect, it } from "vitest";
import { createConfig } from "../../config.js";
import type { Secrets } from "../../secrets.js";

const baseSecrets: Secrets = {
  adminTokenSigningSecret: "admin-token-signing-secret",
  clientAuthJwtSecret: "client-auth-jwt-secret",
  supabaseServiceRoleKey: "service-role-from-secret-manager",
  supabaseConnectionString: "postgres://secret-manager",
  supabaseCaCert: "ca-cert",
  emailClientSecret: "email-client-secret",
  emailRefreshToken: "email-refresh-token",
  r2AccessKeyId: "r2-access-key-id",
  r2SecretAccessKey: "r2-secret-access-key",
  stripeSecretKey: "sk_live_secret_manager",
  stripeWebhookSecret: "whsec_live_secret_manager",
  stripeSecretKeyStaging: "sk_live_staging_secret_manager",
  stripeWebhookSecretStaging: "whsec_live_staging_secret_manager",
};

const originalEnv = { ...process.env };

const setRequiredBaseEnv = () => {
  process.env.NODE_ENV = "test";
  process.env.SUPABASE_URL = "https://example.supabase.co";
  process.env.SUPABASE_SERVICE_ROLE_KEY = "service-role-key-for-tests";
  process.env.SUPABASE_CONNECTION_STRING = "postgres://localhost/test";
  process.env.SUPABASE_CA_CERT = "test-ca-cert";
  process.env.STRIPE_SECRET_KEY = "sk_test_env_override";
  process.env.STRIPE_WEBHOOK_SECRET = "whsec_test_env_override";
  process.env.STRIPE_SOLO_PRICE_ID = "price_solo";
  process.env.STRIPE_TEAM_PRICE_ID = "price_team";
  process.env.STRIPE_FOUNDER_PRICE_ID = "price_founder";
  process.env.EMAIL_USER = "test@xtablo.com";
  process.env.EMAIL_CLIENT_ID = "client-id";
  process.env.EMAIL_CLIENT_SECRET = "email-client-secret";
  process.env.EMAIL_REFRESH_TOKEN = "email-refresh-token";
  process.env.R2_ACCOUNT_ID = "r2-account";
  process.env.R2_ACCESS_KEY_ID = "r2-access-key";
  process.env.R2_SECRET_ACCESS_KEY = "r2-secret";
  process.env.ADMIN_TOKEN_SIGNING_SECRET = "admin-signing-secret";
  process.env.REVENUECAT_WEBHOOK_AUTH_HEADER = "Bearer revenuecat-secret";
  process.env.REVENUECAT_SOLO_PRODUCT_ID = "solo_ios_monthly";
  process.env.REVENUECAT_ANNUAL_PRODUCT_ID = "annual_ios";
};

afterEach(() => {
  process.env = { ...originalEnv };
});

describe("createConfig revenuecat env", () => {
  it("loads revenuecat webhook auth and product ids in test mode", () => {
    setRequiredBaseEnv();

    const config = createConfig(baseSecrets);

    expect(config.REVENUECAT_WEBHOOK_AUTH_HEADER).toBe("Bearer revenuecat-secret");
    expect(config.REVENUECAT_SOLO_PRODUCT_ID).toBe("solo_ios_monthly");
    expect(config.REVENUECAT_ANNUAL_PRODUCT_ID).toBe("annual_ios");
  });

  it("defaults revenuecat env to empty strings when unset", () => {
    setRequiredBaseEnv();
    process.env.REVENUECAT_WEBHOOK_AUTH_HEADER = "";
    process.env.REVENUECAT_SOLO_PRODUCT_ID = "";
    process.env.REVENUECAT_ANNUAL_PRODUCT_ID = "";

    const config = createConfig(baseSecrets);

    expect(config.REVENUECAT_WEBHOOK_AUTH_HEADER).toBe("");
    expect(config.REVENUECAT_SOLO_PRODUCT_ID).toBe("");
    expect(config.REVENUECAT_ANNUAL_PRODUCT_ID).toBe("");
  });
});
