import { describe, it, expect, beforeEach, afterEach } from "vitest";

describe("loadEnv", () => {
  const originalEnv = { ...process.env };
  beforeEach(async () => {
    process.env = { ...originalEnv };
    const mod = await import("@/lib/env");
    mod.resetEnvCacheForTests();
  });
  afterEach(() => { process.env = originalEnv; });

  it("parses a valid env", async () => {
    process.env.DATABASE_URL = "postgres://u:p@127.0.0.1:5432/db";
    process.env.NEXTAUTH_SECRET = "x".repeat(32);
    process.env.NEXTAUTH_URL = "http://localhost:3000";
    process.env.APPROVAL_TOKEN_SECRET = "y".repeat(32);
    process.env.ADMIN_NOTIFY_EMAILS = "a@x.com,b@y.com";
    process.env.EMAIL_FROM = "OG <hello@orthoglobe.org>";
    const { loadEnv } = await import("@/lib/env");
    const env = loadEnv();
    expect(env.adminNotifyEmails).toEqual(["a@x.com", "b@y.com"]);
    expect(env.databaseUrl).toContain("postgres://");
    expect(env.resendApiKey).toBe("");
  });

  it("throws on missing DATABASE_URL", async () => {
    delete process.env.DATABASE_URL;
    const { loadEnv } = await import("@/lib/env");
    expect(() => loadEnv()).toThrow();
  });

  it("trims whitespace in admin emails", async () => {
    process.env.DATABASE_URL = "postgres://u:p@127.0.0.1:5432/db";
    process.env.NEXTAUTH_SECRET = "x".repeat(32);
    process.env.NEXTAUTH_URL = "http://localhost:3000";
    process.env.APPROVAL_TOKEN_SECRET = "y".repeat(32);
    process.env.ADMIN_NOTIFY_EMAILS = "a@x.com , b@y.com , c@z.com";
    process.env.EMAIL_FROM = "OG <hello@orthoglobe.org>";
    const { loadEnv } = await import("@/lib/env");
    const env = loadEnv();
    expect(env.adminNotifyEmails).toEqual(["a@x.com", "b@y.com", "c@z.com"]);
  });

  it("accepts missing NEXT_PUBLIC_SANITY_PROJECT_ID and returns undefined", async () => {
    process.env.DATABASE_URL = "postgres://u:p@127.0.0.1:5432/db";
    process.env.NEXTAUTH_SECRET = "x".repeat(32);
    process.env.NEXTAUTH_URL = "http://localhost:3000";
    process.env.APPROVAL_TOKEN_SECRET = "y".repeat(32);
    process.env.ADMIN_NOTIFY_EMAILS = "a@x.com";
    process.env.EMAIL_FROM = "OG <hello@orthoglobe.org>";
    delete process.env.NEXT_PUBLIC_SANITY_PROJECT_ID;
    delete process.env.SANITY_API_READ_TOKEN;
    delete process.env.SANITY_WEBHOOK_SECRET;
    const { loadEnv } = await import("@/lib/env");
    const env = loadEnv();
    expect(env.sanityProjectId).toBeUndefined();
    expect(env.sanityApiReadToken).toBeUndefined();
    expect(env.sanityWebhookSecret).toBeUndefined();
  });

  it("defaults sanity dataset to 'production' and apiVersion to '2026-03-01'", async () => {
    process.env.DATABASE_URL = "postgres://u:p@127.0.0.1:5432/db";
    process.env.NEXTAUTH_SECRET = "x".repeat(32);
    process.env.NEXTAUTH_URL = "http://localhost:3000";
    process.env.APPROVAL_TOKEN_SECRET = "y".repeat(32);
    process.env.ADMIN_NOTIFY_EMAILS = "a@x.com";
    process.env.EMAIL_FROM = "OG <hello@orthoglobe.org>";
    delete process.env.NEXT_PUBLIC_SANITY_DATASET;
    delete process.env.NEXT_PUBLIC_SANITY_API_VERSION;
    const { loadEnv } = await import("@/lib/env");
    const env = loadEnv();
    expect(env.sanityDataset).toBe("production");
    expect(env.sanityApiVersion).toBe("2026-03-01");
  });

  it("reads configured sanity project id and token when present", async () => {
    process.env.DATABASE_URL = "postgres://u:p@127.0.0.1:5432/db";
    process.env.NEXTAUTH_SECRET = "x".repeat(32);
    process.env.NEXTAUTH_URL = "http://localhost:3000";
    process.env.APPROVAL_TOKEN_SECRET = "y".repeat(32);
    process.env.ADMIN_NOTIFY_EMAILS = "a@x.com";
    process.env.EMAIL_FROM = "OG <hello@orthoglobe.org>";
    process.env.NEXT_PUBLIC_SANITY_PROJECT_ID = "abc123";
    process.env.SANITY_API_READ_TOKEN = "sk_test_token";
    process.env.SANITY_WEBHOOK_SECRET = "whsec_test";
    const { loadEnv } = await import("@/lib/env");
    const env = loadEnv();
    expect(env.sanityProjectId).toBe("abc123");
    expect(env.sanityApiReadToken).toBe("sk_test_token");
    expect(env.sanityWebhookSecret).toBe("whsec_test");
  });
});
