import {AuthStateType} from '@sanity/sdk'
import {installMessageBus} from '@sanity/sdk/_internal'
import {render, screen, waitFor} from '@testing-library/react'
import React from 'react'
import {type FallbackProps} from 'react-error-boundary'
import {beforeEach, describe, expect, it, type MockInstance, vi} from 'vitest'

import {DashboardTokenRefreshProvider} from '../../context/DashboardTokenRefresh'
import {ResourceProvider} from '../../context/ResourceProvider'
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 {AuthBoundary} from './AuthBoundary'

// Mock hooks
vi.mock('../../hooks/auth/useAuthState', () => ({
  useAuthState: vi.fn(() => 'logged-out'),
}))
vi.mock('../../hooks/auth/useLoginUrl')
vi.mock('../../hooks/auth/useOAuthAuthorize', () => ({
  useOAuthAuthorize: vi.fn(() => vi.fn().mockResolvedValue(undefined)),
}))
vi.mock('../../hooks/auth/useVerifyOrgProjects')
vi.mock('../../hooks/auth/useHandleAuthCallback', () => ({
  useHandleAuthCallback: vi.fn(() => async () => {}),
}))
vi.mock('../../hooks/auth/useLogOut', () => ({
  useLogOut: vi.fn(() => async () => {}),
}))
vi.mock('../../hooks/comlink/useWindowConnection', () => ({
  useWindowConnection: vi.fn(() => ({fetch: vi.fn()})),
}))

// Mock AuthError throwing scenario
vi.mock('./AuthError', async (importOriginal) => {
  const actual = await importOriginal<typeof import('./AuthError')>()
  return {
    ...actual,
    AuthError: class MockAuthError extends Error {
      constructor(error: unknown) {
        super(error instanceof Error ? error.message : undefined)
        this.name = 'AuthError'
        this.cause = error
      }
    },
  }
})

// Mock ErrorBoundary with a functional component and state simulation
vi.mock('react-error-boundary', async (importOriginal) => {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const original = await importOriginal<any>()

  // Functional mock that catches render errors
  class MockErrorBoundaryComponent extends React.Component<
    {
      children: React.ReactNode
      FallbackComponent?: React.ComponentType<FallbackProps>
      onError?: (error: Error, errorInfo: React.ErrorInfo) => void
      // Add any other props your actual ErrorBoundary might use
    },
    {error: Error | null}
  > {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    constructor(props: any) {
      super(props)
      this.state = {error: null}
    }

    // Static methods don't use override
    static getDerivedStateFromError(error: Error) {
      // Update state so the next render will show the fallback UI.
      return {error}
    }

    override componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
      // You can also log the error to an error reporting service
      this.props.onError?.(error, errorInfo)
    }

    override render() {
      if (this.state.error && this.props.FallbackComponent) {
        // You can render any custom fallback UI
        return (
          <this.props.FallbackComponent
            error={this.state.error}
            resetErrorBoundary={() => this.setState({error: null})}
          />
        )
      }
      if (this.state.error && !this.props.FallbackComponent) {
        return <div>Caught Error (No Fallback Provided)</div>
      }

      return this.props.children
    }
  }

  return {
    ...original,
    ErrorBoundary: MockErrorBoundaryComponent, // Use the class component mock
    useErrorHandler: vi.fn(),
  }
})

// Mock isInIframe
vi.mock('../utils', () => ({
  isInIframe: vi.fn(() => false),
}))

describe('AuthBoundary', () => {
  let consoleErrorSpy: MockInstance
  const mockUseAuthState = vi.mocked(useAuthState)
  const mockUseLoginUrl = vi.mocked(useLoginUrl)
  const mockUseVerifyOrgProjects = vi.mocked(useVerifyOrgProjects)
  const testProjectIds = ['proj-test'] // Example project ID for tests

  beforeEach(() => {
    vi.clearAllMocks()
    consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
    // Default mocks
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    mockUseAuthState.mockReturnValue({type: AuthStateType.LOGGED_IN} as any)
    mockUseLoginUrl.mockReturnValue('http://example.com/login')
    // Default mock for useVerifyOrgProjects - returns null (no error)
    mockUseVerifyOrgProjects.mockImplementation(() => null)
  })

  afterEach(() => {
    consoleErrorSpy?.mockRestore()
  })

  it.skip('redirects to the sanity.io/login url when authState="logged-out"', async () => {
    vi.mocked(useAuthState).mockReturnValue({
      type: AuthStateType.LOGGED_OUT,
      isDestroyingSession: false,
    })
    render(
      <ResourceProvider fallback={null}>
        <AuthBoundary projectIds={testProjectIds}>Protected Content</AuthBoundary>
      </ResourceProvider>,
    )

    // Wait for the redirect to happen
    await waitFor(() => {
      expect(window.location.href).toBe('https://sanity.io/login')
    })
  })

  it('does not redirect when a host bus is installed but the provider has not connected yet', () => {
    // Workbench remotes start LOGGED_OUT (the host mints the token over the bus) and are not
    // in an iframe, so the only thing standing between them and a login redirect is the
    // dashboard check. AuthSwitch's effect runs before its parent provider's, so the check
    // must not depend on the provider having connected first.
    vi.mocked(useAuthState).mockReturnValue({
      type: AuthStateType.LOGGED_OUT,
      isDestroyingSession: false,
    })
    const originalLocation = window.location
    const location = {href: 'http://remote.test/'}
    Object.defineProperty(window, 'location', {value: location, writable: true})
    vi.stubGlobal('__SANITY_APP_ID__', 'remote')
    const globals = globalThis as {[key: symbol]: unknown}
    const busKey = Symbol.for('sanity.os.bus')
    installMessageBus({appId: 'dashboard'})
    try {
      render(
        <ResourceProvider projectId="p" dataset="d" fallback={null}>
          <DashboardTokenRefreshProvider>
            <AuthBoundary projectIds={testProjectIds}>Protected Content</AuthBoundary>
          </DashboardTokenRefreshProvider>
        </ResourceProvider>,
      )
      expect(location.href).toBe('http://remote.test/')
    } finally {
      delete globals[busKey]
      vi.unstubAllGlobals()
      Object.defineProperty(window, 'location', {value: originalLocation, writable: true})
    }
  })

  describe('oauth mode', () => {
    const oauth = {
      clientId: 'client-abc',
      redirectUri: 'https://app.example.com/callback',
      organizationId: 'org123',
    }

    it('starts the OAuth authorization flow when authState="logged-out"', async () => {
      const authorize = vi.fn().mockResolvedValue(undefined)
      vi.mocked(useOAuthAuthorize).mockReturnValue(authorize)
      vi.mocked(useAuthState).mockReturnValue({
        type: AuthStateType.LOGGED_OUT,
        isDestroyingSession: false,
      })
      render(
        <ResourceProvider projectId="p" dataset="d" auth={{oauth}} fallback={null}>
          <AuthBoundary projectIds={testProjectIds}>Protected Content</AuthBoundary>
        </ResourceProvider>,
      )

      await waitFor(() => expect(authorize).toHaveBeenCalledTimes(1))
      expect(screen.queryByText('Protected Content')).not.toBeInTheDocument()
    })

    it('does not start the OAuth flow when logged out without oauth config', async () => {
      const authorize = vi.fn().mockResolvedValue(undefined)
      vi.mocked(useOAuthAuthorize).mockReturnValue(authorize)
      vi.mocked(useAuthState).mockReturnValue({
        type: AuthStateType.LOGGED_OUT,
        isDestroyingSession: false,
      })
      render(
        <ResourceProvider projectId="p" dataset="d" fallback={null}>
          <AuthBoundary projectIds={testProjectIds}>Protected Content</AuthBoundary>
        </ResourceProvider>,
      )

      await waitFor(() => expect(screen.queryByText('Protected Content')).not.toBeInTheDocument())
      expect(authorize).not.toHaveBeenCalled()
    })

    it('renders the error fallback when starting the OAuth flow rejects', async () => {
      // A falsy rejection reason must still surface as an error, not a blank screen.
      vi.mocked(useOAuthAuthorize).mockReturnValue(vi.fn().mockRejectedValue(undefined))
      vi.mocked(useAuthState).mockReturnValue({
        type: AuthStateType.LOGGED_OUT,
        isDestroyingSession: false,
      })
      render(
        <ResourceProvider projectId="p" dataset="d" auth={{oauth}} fallback={null}>
          <AuthBoundary projectIds={testProjectIds}>Protected Content</AuthBoundary>
        </ResourceProvider>,
      )

      await waitFor(() => expect(screen.getByText('Authentication Error')).toBeInTheDocument())
    })

    it('renders the error fallback without restarting the flow when authState="error"', async () => {
      const authorize = vi.fn().mockResolvedValue(undefined)
      vi.mocked(useOAuthAuthorize).mockReturnValue(authorize)
      vi.mocked(useAuthState).mockReturnValue({
        type: AuthStateType.ERROR,
        error: new Error('access_denied'),
      })
      render(
        <ResourceProvider projectId="p" dataset="d" auth={{oauth}} fallback={null}>
          <AuthBoundary projectIds={testProjectIds}>Protected Content</AuthBoundary>
        </ResourceProvider>,
      )

      await waitFor(() => expect(screen.getByText('Authentication Error')).toBeInTheDocument())
      expect(authorize).not.toHaveBeenCalled()
    })
  })

  it('renders the empty LoginCallback component when authState="logging-in"', () => {
    vi.mocked(useAuthState).mockReturnValue({
      type: AuthStateType.LOGGING_IN,
      isExchangingToken: false,
    })
    const {container} = render(
      <ResourceProvider projectId="p" dataset="d" fallback={null}>
        <AuthBoundary projectIds={testProjectIds}>Protected Content</AuthBoundary>
      </ResourceProvider>,
    )

    // The callback screen renders null check that it renders nothing
    expect(container.innerHTML).toBe('')
    expect(screen.queryByText('Protected Content')).not.toBeInTheDocument()
  })

  it('renders children when authState="logged-in"', () => {
    vi.mocked(useAuthState).mockReturnValue({
      type: AuthStateType.LOGGED_IN,
      currentUser: null,
      token: 'exampleToken',
    })
    render(
      <ResourceProvider projectId="p" dataset="d" fallback={null}>
        <AuthBoundary projectIds={testProjectIds}>Protected Content</AuthBoundary>
      </ResourceProvider>,
    )

    expect(screen.getByText('Protected Content')).toBeInTheDocument()
  })

  it('shows the LoginError (via ErrorBoundary) when authState="error"', async () => {
    vi.mocked(useAuthState).mockReturnValue({
      type: AuthStateType.ERROR,
      error: new Error('test error'),
    })
    render(
      <ResourceProvider projectId="p" dataset="d" fallback={null}>
        <AuthBoundary projectIds={testProjectIds}>Protected Content</AuthBoundary>
      </ResourceProvider>,
    )

    // The AuthBoundary should throw an AuthError internally
    // and then display the LoginError component as the fallback.
    await waitFor(() => {
      expect(screen.getByText('Authentication Error')).toBeInTheDocument()
      expect(
        screen.getByText('Please try again or contact support if the problem persists.'),
      ).toBeInTheDocument()
    })
  })

  it('renders children when logged in and org verification passes', () => {
    render(
      <ResourceProvider projectId="p" dataset="d" fallback={null}>
        <AuthBoundary projectIds={testProjectIds}>Protected Content</AuthBoundary>
      </ResourceProvider>,
    )
    expect(screen.getByText('Protected Content')).toBeInTheDocument()
  })

  it('throws AuthError via AuthSwitch when org verification fails (verifyOrganization=true)', async () => {
    const orgErrorMessage = 'Organization mismatch!'
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    mockUseAuthState.mockReturnValue({type: AuthStateType.LOGGED_IN} as any)
    // Mock specific return value for this test
    mockUseVerifyOrgProjects.mockImplementation((disabled, pIds) => {
      // Expect verification to be enabled (disabled=false) and projectIds to match
      if (!disabled && pIds === testProjectIds) {
        return orgErrorMessage
      }
      return null // Default case
    })

    // Need to catch the error thrown during render. ErrorBoundary mock handles this.
    render(
      <ResourceProvider projectId="p" dataset="d" fallback={null}>
        <AuthBoundary verifyOrganization={true} projectIds={testProjectIds}>
          <div>Protected Content</div>
        </AuthBoundary>
      </ResourceProvider>,
    )

    // The ErrorBoundary's FallbackComponent should be rendered
    // Check if the text rendered by the mocked LoginError component is present
    await waitFor(() => {
      // AuthSwitch throws ConfigurationError, ErrorBoundary catches and renders LoginErrorComponent mock
      // Check for title and description separately as rendered by LoginError
      expect(screen.getByText('Configuration Error')).toBeInTheDocument() // Check title
      expect(screen.getByText(orgErrorMessage)).toBeInTheDocument() // Check description (the error message)
    })
  })

  it('does NOT throw AuthError when org verification fails but verifyOrganization=false', () => {
    const orgErrorMessage = 'Organization mismatch!'
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    mockUseAuthState.mockReturnValue({type: AuthStateType.LOGGED_IN} as any)
    // Mock specific return value for this test
    mockUseVerifyOrgProjects.mockImplementation((disabled, pIds) => {
      // Expect verification to be disabled (disabled=true) and projectIds to match
      if (disabled && pIds === testProjectIds) {
        // Hook should return null when disabled, but we mock based on call
        return orgErrorMessage
      }
      return null // Default case
    })

    render(
      <ResourceProvider projectId="p" dataset="d" fallback={null}>
        <AuthBoundary verifyOrganization={false} projectIds={testProjectIds}>
          <div>Protected Content</div>
        </AuthBoundary>
      </ResourceProvider>,
    )

    // Should render children because verification is disabled
    expect(screen.getByText('Protected Content')).toBeInTheDocument()
    // Error fallback should not be rendered
    expect(screen.queryByText(/Login Error/)).not.toBeInTheDocument()
  })

  it('throws AuthError via AuthSwitch when auth state is ERROR', async () => {
    const authErrorMessage = 'Some authentication error'

    mockUseAuthState.mockReturnValue({
      type: AuthStateType.ERROR,
      error: new Error(authErrorMessage),
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
    } as any)
    mockUseVerifyOrgProjects.mockReturnValue(null) // Org verification passes or is irrelevant
    mockUseVerifyOrgProjects.mockImplementation(() => null)

    render(
      <ResourceProvider projectId="p" dataset="d" fallback={null}>
        <AuthBoundary projectIds={testProjectIds}>
          <div>Protected Content</div>
        </AuthBoundary>
      </ResourceProvider>,
    )

    await waitFor(() => {
      // AuthSwitch throws AuthError, ErrorBoundary catches and renders LoginErrorComponent mock
      // Check for the generic title and description rendered by LoginError for AuthError
      expect(screen.getByText('Authentication Error')).toBeInTheDocument()
      expect(
        screen.getByText('Please try again or contact support if the problem persists.'),
      ).toBeInTheDocument()
    })
  })

  // Add more tests for logged out state, redirects, etc.
})
