"use client";

import { CopilotKit, useCopilotAction, useCopilotChat } from "@copilotkit/react-core";
import { CopilotKitCSSProperties, CopilotChat } from "@copilotkit/react-ui";
import { useState } from "react";
import { useUser } from "@auth0/nextjs-auth0/client";
import LoginButton from "@/components/LoginButton";
import LogoutButton from "@/components/LogoutButton";

// Block input and show indicator while agent is processing
function LoadingOverlay() {
  try {
    const { isLoading } = useCopilotChat();
    if (!isLoading) return null;
    return (
      <>
        {/* Overlay to block input area */}
        <div
          className="absolute bottom-0 left-0 right-0 h-24 z-40"
          style={{ pointerEvents: 'all' }}
          onClick={(e) => e.stopPropagation()}
        />
        {/* Thinking indicator */}
        <div className="absolute bottom-24 left-1/2 transform -translate-x-1/2 bg-white shadow-lg rounded-full px-4 py-2 flex items-center gap-2 z-50">
          <div className="flex gap-1">
            <span className="w-2 h-2 bg-indigo-500 rounded-full animate-bounce" style={{ animationDelay: "0ms" }}></span>
            <span className="w-2 h-2 bg-indigo-500 rounded-full animate-bounce" style={{ animationDelay: "150ms" }}></span>
            <span className="w-2 h-2 bg-indigo-500 rounded-full animate-bounce" style={{ animationDelay: "300ms" }}></span>
          </div>
          <span className="text-sm text-gray-600">Thinking...</span>
        </div>
      </>
    );
  } catch {
    return null;
  }
}

// Chat content with CopilotKit hooks - must be inside CopilotKit provider
function ChatContent({ themeColor, setThemeColor }: { themeColor: string; setThemeColor: (color: string) => void }) {
  useCopilotAction({
    name: "setThemeColor",
    parameters: [{
      name: "themeColor",
      description: "The theme color to set. Make sure to pick nice colors.",
      required: true,
    }],
    handler({ themeColor }) {
      setThemeColor(themeColor);
    },
  });

  return (
    <div
      className="flex-1 flex justify-center py-8 px-2 overflow-hidden relative"
      style={{ "--copilot-kit-primary-color": themeColor } as CopilotKitCSSProperties}
    >
      <div className="h-full w-full max-w-5xl flex flex-col">
        <CopilotChat
          labels={{
            title: "AI Cartwright",
            initial: "Hello! I'm here to help with anything related to caving. Ask me about caves, techniques, safety, equipment, or anything else caving-related!",
          }}
          className="h-full w-full"
        />
      </div>
      <LoadingOverlay />
    </div>
  );
}

export default function CopilotKitPage() {
  const [themeColor, setThemeColor] = useState("#6366f1");
  const [sourcesOnlyMode, setSourcesOnlyMode] = useState(false);
  const { user, isLoading: authLoading } = useUser();

  // Dynamic runtime URL based on sources-only mode
  const runtimeUrl = sourcesOnlyMode
    ? "/api/copilotkit?sourcesOnly=true"
    : "/api/copilotkit";

  // Show loading state while checking authentication
  if (authLoading) {
    return (
      <div className="app-container">
        <div className="loading-state">
          <div className="loading-text">Loading...</div>
        </div>
      </div>
    );
  }

  // If not authenticated, show login page
  if (!user) {
    return (
      <div className="app-container">
        <div className="main-card-wrapper">
          <img
            src="https://cdn.auth0.com/quantum-assets/dist/latest/logos/auth0/auth0-lockup-en-ondark.png"
            alt="Auth0 Logo"
            className="auth0-logo"
          />
          <h1 className="main-title">Cavepedia</h1>

          <div className="action-card">
            <p className="action-text">
              Welcome! Please log in to access the AI Cave Chat.
            </p>
            <LoginButton />
          </div>
        </div>
      </div>
    );
  }

  // If authenticated, show the CopilotKit chat with user profile
  return (
    <CopilotKit
      runtimeUrl={runtimeUrl}
      agent="vpi_1000"
      key={sourcesOnlyMode ? "sources" : "normal"}
    >
      <main className="h-screen w-screen flex flex-col bg-gray-50">
        {/* Header with user profile and logout */}
        <div className="w-full bg-white shadow-sm border-b border-gray-200 px-4 py-3">
          <div className="max-w-7xl mx-auto flex justify-between items-center">
            <div className="flex items-center gap-4">
              <h1 className="text-xl font-semibold text-gray-900">Cavepedia</h1>
              <label className="flex items-center gap-2 text-sm text-gray-600 cursor-pointer">
                <input
                  type="checkbox"
                  checked={sourcesOnlyMode}
                  onChange={(e) => setSourcesOnlyMode(e.target.checked)}
                  className="w-4 h-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500"
                />
                Sources only
              </label>
            </div>
            <div className="flex items-center gap-4">
              {user.picture && (
                <img
                  src={user.picture}
                  alt={user.name || 'User'}
                  className="w-8 h-8 rounded-full"
                />
              )}
              <div className="flex flex-col items-end">
                <span className="text-sm text-gray-700">{user.name}</span>
                {(user as any).roles && (user as any).roles.length > 0 && (
                  <span className="text-xs text-gray-500">
                    {(user as any).roles.join(', ')}
                  </span>
                )}
              </div>
              <LogoutButton />
            </div>
          </div>
        </div>

        {/* CopilotKit Chat */}
        <ChatContent themeColor={themeColor} setThemeColor={setThemeColor} />
      </main>
    </CopilotKit>
  );
}
