import { describe, it, expect } from 'vitest';
import { resolveAuthConfig } from './auth';

describe('resolveAuthConfig', () => {
  it('returns both URLs and sales email from env', () => {
    const cfg = resolveAuthConfig({
      PUBLIC_AUTH_SIGNIN_URL: 'https://app.cameleer.io/sign-in',
      PUBLIC_AUTH_SIGNUP_URL: 'https://app.cameleer.io/sign-in?first_screen=register',
      PUBLIC_SALES_EMAIL: 'sales@cameleer.io',
    });
    expect(cfg.signInUrl).toBe('https://app.cameleer.io/sign-in');
    expect(cfg.signUpUrl).toBe('https://app.cameleer.io/sign-in?first_screen=register');
    expect(cfg.salesEmail).toBe('sales@cameleer.io');
  });

  it('throws if PUBLIC_AUTH_SIGNIN_URL is missing', () => {
    expect(() => resolveAuthConfig({
      PUBLIC_AUTH_SIGNUP_URL: 'https://app.cameleer.io/sign-in?first_screen=register',
      PUBLIC_SALES_EMAIL: 'sales@cameleer.io',
    })).toThrow(/PUBLIC_AUTH_SIGNIN_URL/);
  });

  it('throws if a URL is not https', () => {
    expect(() => resolveAuthConfig({
      PUBLIC_AUTH_SIGNIN_URL: 'http://app.cameleer.io/sign-in',
      PUBLIC_AUTH_SIGNUP_URL: 'https://app.cameleer.io/sign-in?first_screen=register',
      PUBLIC_SALES_EMAIL: 'sales@cameleer.io',
    })).toThrow(/must be https/);
  });

  it('throws if sales email is not a valid mailto target', () => {
    expect(() => resolveAuthConfig({
      PUBLIC_AUTH_SIGNIN_URL: 'https://app.cameleer.io/sign-in',
      PUBLIC_AUTH_SIGNUP_URL: 'https://app.cameleer.io/sign-in?first_screen=register',
      PUBLIC_SALES_EMAIL: 'not-an-email',
    })).toThrow(/PUBLIC_SALES_EMAIL/);
  });

  it('throws if PUBLIC_AUTH_SIGNUP_URL is missing', () => {
    expect(() => resolveAuthConfig({
      PUBLIC_AUTH_SIGNIN_URL: 'https://app.cameleer.io/sign-in',
      PUBLIC_SALES_EMAIL: 'sales@cameleer.io',
    })).toThrow(/PUBLIC_AUTH_SIGNUP_URL/);
  });

  it('throws if PUBLIC_AUTH_SIGNUP_URL is not https', () => {
    expect(() => resolveAuthConfig({
      PUBLIC_AUTH_SIGNIN_URL: 'https://app.cameleer.io/sign-in',
      PUBLIC_AUTH_SIGNUP_URL: 'http://app.cameleer.io/sign-in?first_screen=register',
      PUBLIC_SALES_EMAIL: 'sales@cameleer.io',
    })).toThrow(/must be https/);
  });

  it('exposes signUpUrl distinct from signInUrl', () => {
    const cfg = resolveAuthConfig({
      PUBLIC_AUTH_SIGNIN_URL: 'https://app.cameleer.io/sign-in',
      PUBLIC_AUTH_SIGNUP_URL: 'https://app.cameleer.io/sign-in?first_screen=register',
      PUBLIC_SALES_EMAIL: 'sales@cameleer.io',
    });
    expect(cfg.signUpUrl).not.toBe(cfg.signInUrl);
  });
});
