export type SsoProviderId = 'workos' | 'keycloak';

export interface SsoProvider {
  id: SsoProviderId;
  buttonLabel: string;
  description: string;
  enabled: boolean;
  href: string;
}

const PROVIDER_METADATA: Record<SsoProviderId, Omit<SsoProvider, 'enabled' | 'href'>> = {
  workos: {
    id: 'workos',
    buttonLabel: 'Continue with WorkOS',
    description: 'Enterprise SSO via WorkOS',
  },
  keycloak: {
    id: 'keycloak',
    buttonLabel: 'Continue with Keycloak',
    description: 'Enterprise SSO via Keycloak',
  },
};

export function getEnabledSsoProviders(): SsoProvider[] {
  return (Object.keys(PROVIDER_METADATA) as SsoProviderId[])
    .map((providerId) => getSsoProvider(providerId))
    .filter((provider): provider is SsoProvider => provider?.enabled === true);
}

export function getSsoProvider(providerId: string): SsoProvider | null {
  if (!isSsoProviderId(providerId)) {
    return null;
  }

  return {
    ...PROVIDER_METADATA[providerId],
    enabled: isSsoProviderEnabled(providerId),
    href: `/auth/provider/${providerId}`,
  };
}

function isSsoProviderId(value: string): value is SsoProviderId {
  return value === 'workos' || value === 'keycloak';
}

function isSsoProviderEnabled(providerId: SsoProviderId): boolean {
  switch (providerId) {
    case 'workos':
      return process.env['NEXT_PUBLIC_WORKOS_ENABLED'] === 'true';
    case 'keycloak':
      return process.env['NEXT_PUBLIC_KEYCLOAK_ENABLED'] === 'true';
  }
}
