import { useState } from 'react'
import { isSupabaseConfigured, supabase } from '../lib/supabase'

function GoogleIcon() {
  return (
    <svg width="18" height="18" viewBox="0 0 18 18" aria-hidden="true">
      <path
        fill="#4285F4"
        d="M17.64 9.2045c0-.6381-.0573-1.2518-.1636-1.8409H9v3.4814h4.8436c-.2086 1.125-.8427 2.0782-1.7959 2.7164v2.2582h2.9087c1.7018-1.5668 2.6836-3.874 2.6836-6.6151z"
      />
      <path
        fill="#34A853"
        d="M9 18c2.43 0 4.4673-.806 5.9564-2.1805l-2.9087-2.2582c-.8059.54-1.8368.8618-3.0477.8618-2.3436 0-4.3282-1.5831-5.0359-3.7104H.9573v2.3318C2.4382 15.9832 5.4818 18 9 18z"
      />
      <path
        fill="#FBBC05"
        d="M3.9641 10.71c-.18-.54-.2822-1.1168-.2822-1.71s.1023-1.17.2822-1.71V4.9582H.9573C.3477 6.1732 0 7.5477 0 9s.3477 2.8268.9573 4.0418L3.9641 10.71z"
      />
      <path
        fill="#EA4335"
        d="M9 3.5795c1.3214 0 2.5077.4541 3.4405 1.346l2.5813-2.5813C13.4632.8918 11.4259 0 9 0 5.4818 0 2.4382 2.0168.9573 4.9582L3.9641 7.29C4.6718 5.1618 6.6564 3.5795 9 3.5795z"
      />
    </svg>
  )
}

function MicrosoftIcon() {
  return (
    <svg width="18" height="18" viewBox="0 0 21 21" aria-hidden="true">
      <rect x="1" y="1" width="9" height="9" fill="#f25022" />
      <rect x="11" y="1" width="9" height="9" fill="#7fba00" />
      <rect x="1" y="11" width="9" height="9" fill="#00a4ef" />
      <rect x="11" y="11" width="9" height="9" fill="#ffb900" />
    </svg>
  )
}

function OAuthButtons({ onError }) {
  const [loadingProvider, setLoadingProvider] = useState(null)

  const handleOAuthSignIn = async (provider) => {
    if (!isSupabaseConfigured) {
      onError?.(
        'Supabase no está configurado todavía. Completa frontend/.env con tu URL y anon key.',
      )
      return
    }

    onError?.(null)
    setLoadingProvider(provider)
    const { error } = await supabase.auth.signInWithOAuth({
      provider,
      options: {
        redirectTo: `${window.location.origin}/dashboard`,
        // Azure AD solo incluye el claim "email" en el id_token si se solicita
        // explícitamente; con el scope por defecto (openid) Supabase no puede
        // leer el correo del usuario y el login falla.
        ...(provider === 'azure' ? { scopes: 'openid email profile' } : {}),
      },
    })
    if (error) {
      onError?.(error.message)
      setLoadingProvider(null)
    }
  }

  const disabled = loadingProvider !== null

  return (
    <div className="flex flex-col gap-2.5">
      <button
        type="button"
        onClick={() => handleOAuthSignIn('google')}
        disabled={disabled}
        title={!isSupabaseConfigured ? 'Configura Supabase en frontend/.env primero' : undefined}
        className="inline-flex items-center justify-center gap-2 rounded-lg border border-white/10 bg-[#181b24] px-3 py-2.5 text-sm font-medium text-slate-200 transition-colors hover:bg-[#20232e] disabled:opacity-60"
      >
        <GoogleIcon />
        {loadingProvider === 'google' ? 'Conectando...' : 'Continuar con Google'}
      </button>
      <button
        type="button"
        onClick={() => handleOAuthSignIn('azure')}
        disabled={disabled}
        title={!isSupabaseConfigured ? 'Configura Supabase en frontend/.env primero' : undefined}
        className="inline-flex items-center justify-center gap-2 rounded-lg border border-white/10 bg-[#181b24] px-3 py-2.5 text-sm font-medium text-slate-200 transition-colors hover:bg-[#20232e] disabled:opacity-60"
      >
        <MicrosoftIcon />
        {loadingProvider === 'azure' ? 'Conectando...' : 'Continuar con Microsoft'}
      </button>
    </div>
  )
}

export default OAuthButtons
