import {CorsOriginError} from '@sanity/client'
import {AuthStateType, getCorsErrorProjectId, isImportError} from '@sanity/sdk'
import {isDashboardEnvironment, isStudioConfig} from '@sanity/sdk/_internal'
import {useEffect, useMemo, useState} from 'react'
import {ErrorBoundary, type FallbackProps} from 'react-error-boundary'

import {ComlinkTokenRefreshProvider} from '../../context/ComlinkTokenRefresh'
import {DashboardTokenRefreshProvider} from '../../context/DashboardTokenRefresh'
import {useAuthState} from '../../hooks/auth/useAuthState'
import {useLoginUrl} from '../../hooks/auth/useLoginUrl'
import {useOAuthAuthorize} from '../../hooks/auth/useOAuthAuthorize'
import {useVerifyOrgProjects} from '../../hooks/auth/useVerifyOrgProjects'
import {useSanityInstance} from '../../hooks/context/useSanityInstance'
import {ChunkLoadError} from '../errors/ChunkLoadError'
import {CorsErrorComponent} from '../errors/CorsErrorComponent'
import {isInIframe} from '../utils'
import {AuthError} from './AuthError'
import {ConfigurationError} from './ConfigurationError'
import {LoginCallback} from './LoginCallback'
import {LoginError, type LoginErrorProps} from './LoginError'

// Only import bridge if we're in an iframe. This assumes that the app is
// running within SanityOS if it is in an iframe and that the bridge hasn't already been loaded
if (isInIframe() && !document.querySelector('[data-sanity-core]')) {
  const parsedUrl = new URL(window.location.href)
  const mode = new URLSearchParams(parsedUrl.hash.slice(1)).get('mode')
  const script = document.createElement('script')
  script.src =
    mode === 'core-ui--staging'
      ? 'https://core.sanity-cdn.work/bridge.js'
      : 'https://core.sanity-cdn.com/bridge.js'
  script.type = 'module'
  script.async = true
  document.head.appendChild(script)
}

/**
 * @internal
 */
export interface AuthBoundaryProps {
  /**
   * Custom component to render the login screen.
   * Receives all props. Defaults to {@link Login}.
   */
  LoginComponent?: React.ComponentType<{
    header?: React.ReactNode
    footer?: React.ReactNode
  }>

  /**
   * Custom component to render during OAuth callback processing.
   * Receives all props. Defaults to {@link LoginCallback}.
   */
  CallbackComponent?: React.ComponentType<{
    header?: React.ReactNode
    footer?: React.ReactNode
  }>

  /**
   * Custom component to render when authentication errors occur.
   * Receives error boundary props and layout props. Defaults to
   * {@link LoginError}
   */
  LoginErrorComponent?: React.ComponentType<LoginErrorProps>

  /** Header content to display */
  header?: React.ReactNode

  /**
   * The project IDs to use for organization verification.
   */
  projectIds?: string[]

  /** Footer content to display */
  footer?: React.ReactNode

  /** Protected content to render when authenticated */
  children?: React.ReactNode

  /**
   * Whether to verify that the project belongs to the organization specified in the dashboard context.
   * By default, organization verification is enabled when running in a dashboard context.
   *
   * WARNING: Disabling organization verification is NOT RECOMMENDED and may cause your application
   * to break in the future. This should never be disabled in production environments.
   */
  verifyOrganization?: boolean
}

/**
 * A component that handles authentication flow and error boundaries for a
 * protected section of the application.
 *
 * @remarks
 * This component manages different authentication states and renders the
 * appropriate components based on that state.
 *
 * @example
 * ```tsx
 * function App() {
 *   return (
 *     <AuthBoundary header={<MyLogo />}>
 *       <ProtectedContent />
 *     </AuthBoundary>
 *   )
 * }
 * ```
 *
 * @internal
 */
export function AuthBoundary({
  LoginErrorComponent = LoginError,
  ...props
}: AuthBoundaryProps): React.ReactNode {
  /**
   * When the session is re-established (e.g. ComlinkTokenRefresh silently mints a
   * fresh token via setAuthToken), the auth store returns to LOGGED_IN but the
   * ErrorBoundary stays latched on its fallback until reset. Keying it on the
   * recovered session lets it clear automatically
   */
  const authState = useAuthState()
  const sessionResetKey =
    authState.type === AuthStateType.LOGGED_IN ? authState.token : authState.type

  const FallbackComponent = useMemo(() => {
    return function LoginComponentWithLayoutProps(fallbackProps: FallbackProps) {
      // Chunk-load errors from any lazy-loaded code beneath this boundary
      // (typically the consumer's app) get the chunk-aware fallback instead
      // of being misreported as auth errors.
      if (isImportError(fallbackProps.error)) {
        return <ChunkLoadError {...fallbackProps} />
      }
      if (fallbackProps.error instanceof CorsOriginError) {
        return (
          <CorsErrorComponent
            {...fallbackProps}
            projectId={getCorsErrorProjectId(fallbackProps.error)}
          />
        )
      }
      return <LoginErrorComponent {...fallbackProps} />
    }
  }, [LoginErrorComponent])

  return (
    <ComlinkTokenRefreshProvider>
      <DashboardTokenRefreshProvider>
        <ErrorBoundary FallbackComponent={FallbackComponent} resetKeys={[sessionResetKey]}>
          <AuthSwitch {...props} />
        </ErrorBoundary>
      </DashboardTokenRefreshProvider>
    </ComlinkTokenRefreshProvider>
  )
}

interface AuthSwitchProps {
  LoginComponent?: React.ComponentType<{
    header?: React.ReactNode
    footer?: React.ReactNode
  }>
  CallbackComponent?: React.ComponentType<{
    header?: React.ReactNode
    footer?: React.ReactNode
  }>
  header?: React.ReactNode
  footer?: React.ReactNode
  children?: React.ReactNode
  verifyOrganization?: boolean
  projectIds?: string[]
}

function AuthSwitch({
  CallbackComponent = LoginCallback,
  children,
  verifyOrganization = true,
  projectIds,
  ...props
}: AuthSwitchProps) {
  const authState = useAuthState()
  const instance = useSanityInstance()
  const isStudio = isStudioConfig(instance.config)
  const disableVerifyOrg =
    !verifyOrganization || isStudio || authState.type !== AuthStateType.LOGGED_IN
  const orgError = useVerifyOrgProjects(disableVerifyOrg, projectIds)

  const isLoggedOut = authState.type === AuthStateType.LOGGED_OUT && !authState.isDestroyingSession
  const isOAuth = !!instance.config.auth?.oauth
  const loginUrl = useLoginUrl()
  const authorize = useOAuthAuthorize()
  const [authorizeError, setAuthorizeError] = useState<{error: unknown} | null>(null)

  useEffect(() => {
    if (isLoggedOut && !isInIframe() && !isStudio && !isDashboardEnvironment(instance)) {
      // We don't want to redirect to login if we're in the Dashboard, in studio
      // mode, or in the workbench (the OS owns the session and mints the token)
      if (isOAuth) {
        // PKCE params and navigation are owned by core. LOGGED_OUT renders
        // null, so a rejection here must be surfaced or the user sees nothing.
        authorize().catch((error) => setAuthorizeError({error}))
      } else {
        window.location.href = loginUrl
      }
    }
  }, [isLoggedOut, isOAuth, authorize, loginUrl, isStudio, instance])

  // Only check the error if verification is enabled
  if (verifyOrganization && orgError) {
    throw new ConfigurationError({message: orgError})
  }

  if (authorizeError) {
    throw new AuthError(authorizeError.error)
  }

  switch (authState.type) {
    case AuthStateType.ERROR: {
      throw new AuthError(authState.error)
    }
    case AuthStateType.LOGGING_IN: {
      return <CallbackComponent {...props} />
    }
    case AuthStateType.LOGGED_IN: {
      return children
    }
    case AuthStateType.LOGGED_OUT: {
      return null
    }
    default: {
      // @ts-expect-error - This state should never happen
      throw new Error(`Invalid auth state: ${authState.type}`)
    }
  }
}
