import { describe, expect, it } from "vitest";
import { assertProductionReadyConfig, type ServerEnv } from "@/server/env";

const base: ServerEnv = {
  NODE_ENV: "production",
  APP_ENV: "production",
  APP_URL: "https://zirabet.example",
  APP_NAME: "ZiraBet",
  DATABASE_URL: "postgresql://user:pass@localhost:5432/db",
  REDIS_URL: "redis://localhost:6379",
  JWT_ACCESS_SECRET: "a".repeat(64),
  JWT_REFRESH_SECRET: "b".repeat(64),
  COOKIE_DOMAIN: "zirabet.example",
  DEFAULT_CURRENCY: "EUR",
  SUPPORTED_CURRENCIES: "EUR,USD,USDT",
  ALLOWED_COUNTRIES: "BZ,CH",
  BLOCKED_COUNTRIES: "US",
  LICENSE_JURISDICTION: "Belize",
  LICENSE_BADGE_TEXT: "Licensed",
  ODDS_PROVIDER: "real-odds",
  ODDS_PROVIDER_BASE_URL: "https://odds.example",
  ODDS_PROVIDER_API_KEY: "odds-key",
  ODDS_PROVIDER_BOOKMAKERS: "winamax_fr,unibet_fr",
  ODDS_PROVIDER_REGIONS: "fr",
  ODDS_PROVIDER_MARKETS: "h2h,spreads,totals",
  ODDS_PROVIDER_SPORTS: "upcoming",
  ODDS_PROVIDER_WEBHOOK_SECRET: "odds-secret",
  ODDS_PREMATCH_WINDOW_DAYS: 5,
  ODDS_SYNC_ADVANCED_MARKETS: false,
  ODDS_PRICING_SOURCE_BOOKMAKER: "winamax_fr",
  ODDS_PRICING_UPLIFT_PERCENT: 5,
  ODDS_PRICING_MIN_MARGIN_PERCENT: 1.5,
  ODDS_PRICING_MAX_ODDS: 100,
  PAYMENT_PROVIDER: "real-psp",
  PAYMENT_PROVIDER_BASE_URL: "https://psp.example",
  PAYMENT_PROVIDER_API_KEY: "psp-key",
  PAYMENT_WEBHOOK_SECRET: "c".repeat(64),
  CRYPTO_CUSTODY_PROVIDER: "hd",
  CRYPTO_WALLET_MNEMONIC: "legal winner thank year wave sausage worth useful legal winner thank yellow",
  CRYPTO_PRICE_PROVIDER: "kraken",
  CRYPTO_PRICE_FALLBACK_PROVIDER: "coinbase",
  CRYPTO_ETHEREUM_RPC_URL: "https://eth-rpc.example",
  CRYPTO_BITCOIN_INDEXER_URL: "https://btc-indexer.example",
  CRYPTO_WEBHOOK_SECRET: "d".repeat(64),
  CRYPTO_DEV_ALLOW_SIMULATION: false,
  KYC_PROVIDER: "real-kyc",
  KYC_PROVIDER_BASE_URL: "https://kyc.example",
  KYC_PROVIDER_API_KEY: "kyc-key",
  KYC_WEBHOOK_SECRET: "kyc-secret",
  SENTRY_DSN: undefined,
  ADMIN_BOOTSTRAP_EMAIL: undefined,
  ADMIN_BOOTSTRAP_PASSWORD: undefined
};

describe("production preflight", () => {
  it("blocks local providers in production", () => {
    expect(() => assertProductionReadyConfig({ ...base, ODDS_PROVIDER: "local" })).toThrow(/local providers/);
  });

  it("blocks development crypto mnemonic in production", () => {
    expect(() =>
      assertProductionReadyConfig({
        ...base,
        CRYPTO_WALLET_MNEMONIC: "test test test test test test test test test test test junk"
      })
    ).toThrow(/development mnemonic/);
  });

  it("accepts configured real providers", () => {
    expect(() => assertProductionReadyConfig(base)).not.toThrow();
  });
});
