/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

'use client'

import { Alert, Button, Flex, Typography } from 'antd'
import { useState, useEffect } from 'react'
import { oauthProviderFactory } from '@/lib/auth/providers/factory'

const { Text } = Typography

function OidcLogin() {
  const [isLoading, setIsLoading] = useState(true)
  const [userManager, setUserManager] = useState(null)
  const [initError, setInitError] = useState(null)
  const [redirectError, setRedirectError] = useState(null)

  useEffect(() => {
    const initializeOidc = async () => {
      try {
        // Get the provider directly - it handles config fetching internally
        const provider = await oauthProviderFactory.getProvider()

        if (provider.getType() !== 'oidc') {
          setInitError('OIDC provider not configured')
          setIsLoading(false)

          return
        }

        const sharedUserManager = provider.getUserManager()
        if (!sharedUserManager) {
          throw new Error('Failed to get UserManager from OIDC provider')
        }

        setUserManager(sharedUserManager)
        setInitError(null)
        setIsLoading(false)
      } catch (error) {
        setInitError(error.message || 'Failed to initialize OIDC')
        setIsLoading(false)
      }
    }

    initializeOidc()
  }, [])

  if (isLoading) {
    return (
      <Flex justify='center' className='my-4'>
        <Text>Initializing authentication...</Text>
      </Flex>
    )
  }

  if (initError) {
    return (
      <Flex justify='center' className='my-4'>
        <Alert message={initError} type='error' showIcon />
      </Flex>
    )
  }

  return (
    <Flex vertical align='center' gap={24} className='mt-4'>
      {redirectError && <Alert message={redirectError} type='error' showIcon style={{ width: '100%' }} />}
      <OidcLoginButton userManager={userManager} onError={setRedirectError} />
    </Flex>
  )
}

function OidcLoginButton({ userManager, onError }) {
  const [isLoggingIn, setIsLoggingIn] = useState(false)

  const handleLogin = async () => {
    if (!userManager) {
      return
    }

    try {
      onError(null)
      setIsLoggingIn(true)
      await userManager.signinRedirect()
    } catch (error) {
      onError(error.message || 'Failed to redirect to the identity provider')
      setIsLoggingIn(false)
    }
  }

  return (
    <Button
      type='primary'
      size='large'
      onClick={handleLogin}
      disabled={!userManager || isLoggingIn}
      style={{ minWidth: 200, height: 48 }}
    >
      {isLoggingIn ? 'Redirecting...' : 'Sign In'}
    </Button>
  )
}

export default OidcLogin
