"use client";
import Image from 'next/image';
import React, { useState, useEffect } from "react";
import { useGoogleLogin } from "@react-oauth/google";
import { useTheme } from 'next-themes';
import { Sun, Moon } from 'lucide-react'; // Optional: install lucide-react for clean icons
import Keycloak from "keycloak-js";


const keycloak = new Keycloak({
  url: "https://keycloak.petermaquiran.xyz",
  realm: "tvone", // ✅ IMPORTANT
  clientId: "tvone-web", // must match Keycloak client
});

interface GoogleAuthResponse {
  access_token: string;
  token_type: string;
  expires_in: number;
  scope: string;
  authuser?: string;
  prompt?: string;
}

interface KeycloakTokenResponse {
  access_token: string;
  expires_in: number;
  refresh_expires_in: number;
  refresh_token: string;
  token_type: string;
  id_token?: string;
  "not-before-policy": number;
  session_state: string;
  scope: string;
}

export default function AppleStyleAuth() {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const { theme, setTheme } = useTheme();
  const [mounted, setMounted] = useState(false);

  // Avoid hydration mismatch by waiting for mount
  useEffect(() => {
    keycloak.init({
      onLoad: "check-sso", // or "login-required"
      pkceMethod: "S256",
    }).then((authenticated) => {
      if (authenticated) {
        localStorage.setItem("token", keycloak.token!);
        console.log("Logged in", keycloak.token);
        localStorage.setItem("token", keycloak.token as string);

        fetch("http://localhost:3001/profile/", {
          headers: {
            Authorization: `Bearer ${localStorage.getItem("token")}`,
          },
        });
      }
    });
    setMounted(true);
  }, []);

  const handleExchange = async (googleResponse: GoogleAuthResponse): Promise<void> => {
    try {
      const details: Record<string, string> = {
        grant_type: 'urn:ietf:params:oauth:grant-type:token-exchange',
        client_id: 'tvone-web', // Replace with your actual Keycloak Client ID
        subject_token: googleResponse.access_token,
        subject_token_type: 'urn:ietf:params:oauth:token-type:access_token',
        subject_issuer: 'google', // Ensure this matches your Keycloak IdP Alias
      };

      const formBody = new URLSearchParams(details).toString();

      const response = await fetch(
        'https://keycloak.petermaquiran.xyz/realms/tvone/protocol/openid-connect/token', 
        {
          method: 'POST',
          headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
          body: formBody,
        }
      );

      if (!response.ok) {
        throw new Error(`Keycloak exchange failed: ${response.statusText}`);
      }

      const data: KeycloakTokenResponse = await response.json();

      // Store the Keycloak token to send to your NestJS API
      localStorage.setItem("token", data.access_token);
      console.log("Authenticated with Keycloak:", data.access_token);
      
      // Redirect user or update Global Auth State here
    } catch (error) {
      console.error("Authentication Flow Error:", error);
    }
  };
  
  const googleLogin = useGoogleLogin({
    onSuccess: (res) =>  {
      handleExchange(res)
      console.log("Google Success", res) 
    },
    onError: () => console.log("Google Failed"),
  });

  const handleManualLogin = async (): Promise<void> => {
    const details: Record<string, string> = {
      grant_type: 'password',
      client_id: 'tvone-web-client',
      username: email,
      password: password,
      scope: 'openid',
    };

    try {
      const response = await fetch(
        'https://keycloak.petermaquiran.xyz/realms/<realm>/protocol/openid-connect/token', 
        {
          method: 'POST',
          headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
          body: new URLSearchParams(details).toString(),
        }
      );

      const data: KeycloakTokenResponse = await response.json();
      if (data.access_token) {
        localStorage.setItem("token", data.access_token);
      }
    } catch (err) {
      console.error("Login failed", err);
    }
  };

  if (!mounted) return null;

  return (
    <div className="relative flex min-h-screen items-center justify-center bg-[#f5f5f7] px-4 py-10 transition-colors duration-500 dark:bg-black">
      
      {/* THEME TOGGLE BUTTON */}
      <button
        onClick={() => setTheme(theme === 'dark' ? 'light' : 'dark')}
        className="absolute right-6 top-6 flex h-10 w-10 items-center justify-center rounded-full bg-white/80 shadow-sm backdrop-blur-md transition-all hover:scale-110 active:scale-95 dark:bg-neutral-800/80"
        aria-label="Toggle Theme"
      >
        {theme === 'dark' ? (
          <Sun className="h-5 w-5 text-yellow-400" />
        ) : (
          <Moon className="h-5 w-5 text-neutral-600" />
        )}
      </button>

      <div className="w-full max-w-[400px] space-y-8 text-center">
        
        {/* 1. LOGOTIPO */}
        <div className="flex flex-col items-center">
          <div className="flex h-16 w-16 items-center justify-center rounded-[1.2rem] bg-white text-white shadow-2xl transition-transform duration-700 hover:rotate-[360deg] dark:bg-white dark:text-black">
            <Image src="/logo.png" alt="logo" width={100} height={100} />
          </div>
          <h1 className="mt-6 text-3xl font-bold tracking-tight text-neutral-900 dark:text-white">
            Iniciar sessão
          </h1>
          <p className="mt-2 text-sm text-neutral-500">Usa a tua conta TVone.</p>
        </div>

        {/* 2. FORMULÁRIO */}
        <div className="space-y-3">
          <input
            type="email"
            placeholder="E-mail ou Telefone"
            value={email}
            onChange={(e) => setEmail(e.target.value)}
            className="w-full rounded-2xl border border-neutral-200 bg-white/50 px-5 py-4 text-sm outline-none transition-all focus:border-blue-500 focus:bg-white focus:text-black focus:ring-4 focus:ring-blue-500/10 dark:border-neutral-800 dark:bg-neutral-900 dark:text-white"
          />
          <input
            type="password"
            placeholder="Palavra-passe"
            value={password}
            onChange={(e) => setPassword(e.target.value)}
            className="w-full rounded-2xl border border-neutral-200 bg-white/50 px-5 py-4 text-sm outline-none transition-all focus:border-blue-500 focus:bg-white focus:text-black focus:ring-4 focus:ring-blue-500/10 dark:border-neutral-800 dark:bg-neutral-900 dark:text-white"
          />
          <button className="w-full rounded-2xl bg-blue-600 py-4 text-sm font-bold text-white transition-all hover:bg-blue-700 active:scale-[0.98]">
            Continuar
          </button>
        </div>

        {/* 3. DIVISOR */}
        <div className="relative flex items-center py-4">
          <div className="flex-grow border-t border-neutral-200 dark:border-neutral-800"></div>
          <span className="mx-4 flex-shrink text-[11px] font-bold uppercase tracking-widest text-neutral-400">
            ou
          </span>
          <div className="flex-grow border-t border-neutral-200 dark:border-neutral-800"></div>
        </div>

        {/* 4. BOTÃO GOOGLE */}
          <button
            onClick={() =>
              keycloak.login({
                redirectUri: `${window.location.origin}/create-news`,
              })
            }
            className="group flex w-full items-center justify-center gap-3 rounded-2xl border border-neutral-200 bg-white px-6 py-3.5 transition-all hover:bg-neutral-50 active:scale-[0.98] dark:border-neutral-800 dark:bg-transparent dark:hover:bg-neutral-900"
          >
          <svg className="h-5 w-5" viewBox="0 0 24 24">
            <path d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z" fill="#4285F4" />
            <path d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z" fill="#34A853" />
            <path d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z" fill="#FBBC05" />
            <path d="M12 5.38c1.62 0 3.06.56 4.21 1.66l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z" fill="#EA4335" />
          </svg>
          <span className="text-sm font-semibold tracking-tight text-neutral-700 dark:text-neutral-300">
            Continuar com o Google
          </span>
        </button>

        {/* 5. LINKS */}
        <div className="pt-4 text-center">
          <a href="#" className="text-xs font-medium text-blue-600 hover:underline">
            Esqueceste-te da palavra-passe?
          </a>
          {/* <p className="mt-8 text-xs text-neutral-400">
            Não tens conta?{" "}
            <a href="#" className="font-bold text-neutral-900 dark:text-white">
              Cria uma agora.
            </a>
          </p> */}
        </div>
      </div>
    </div>
  );
}