'use client'

import { useState, useRef, useEffect } from 'react'
import { useChat } from 'ai/react'

type MessageWithLoading = {
  content: string;
  role: string;
  isStreaming?: boolean;
}

const EXAMPLE_PROMPTS = [
  {
    title: "Personalized Learning",
    description: "Get tailored explanations",
    prompt: "Can you help me understand photosynthesis in a simple way?"
  },
  {
    title: "24/7 Availability",
    description: "Learn at your own pace",
    prompt: "I need help solving this quadratic equation: x² + 5x + 6 = 0"
  },
  {
    title: "Multiple Subjects",
    description: "From math to literature",
    prompt: "What are the main themes in Shakespeare's Macbeth?"
  }
]

export default function LandingPageChatBot() {
  const { messages: rawMessages, input, handleInputChange, handleSubmit, isLoading, append } = useChat({
    api: '/api/landing_page_chat',
    streamProtocol: 'data',
    onError: (error) => {
      console.error('Chat error:', error);
    },
    onFinish: (message) => {
      console.log('Chat finished:', message);
      setMessages(prev => prev.map(msg => ({...msg, isStreaming: false})))
    },
    keepLastMessageOnError: true,
  })

  const chatContainerRef = useRef<HTMLDivElement>(null)
  const [hasInteracted, setHasInteracted] = useState(false)
  const [messages, setMessages] = useState<MessageWithLoading[]>([])

  useEffect(() => {
    setMessages(rawMessages.map((msg, index) => ({
      ...msg,
      isStreaming: isLoading && index === rawMessages.length - 1 && msg.role === 'assistant'
    })))
  }, [rawMessages, isLoading])

  const scrollToBottom = () => {
    if (chatContainerRef.current) {
      const { scrollHeight, clientHeight } = chatContainerRef.current
      chatContainerRef.current.scrollTop = scrollHeight - clientHeight
    }
  }

  useEffect(() => {
    scrollToBottom()
  }, [messages])

  const sendMessage = async (text: string) => {
    setHasInteracted(true)
    await append({
      content: text,
      role: 'user',
    })
  }

  const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
    setHasInteracted(true)
    handleSubmit(e)
  }

  return (
    <section className="w-full h-[800px] bg-gradient-to-b from-background-secondary to-background-primary flex items-center justify-center px-4">
      <div className="w-full max-w-5xl mx-auto h-full flex items-center">
        {!hasInteracted && messages.length === 0 ? (
          <div className="text-center space-y-8 w-full">
            <div className="space-y-4">
              <h2 className="text-5xl font-bold bg-gradient-to-r from-primary to-secondary bg-clip-text text-transparent">
                AI Learning Assistant
              </h2>
              <p className="text-text-secondary text-xl max-w-2xl mx-auto leading-relaxed">
                Explore new topics, get homework help, or discover learning resources
              </p>
            </div>

            <form onSubmit={onSubmit} className="max-w-3xl mx-auto">
              <div className="relative flex items-center">
                <input
                  type="text"
                  value={input}
                  onChange={handleInputChange}
                  placeholder="Ask anything about learning..."
                  className="w-full p-6 pr-36 rounded-2xl border border-border/50 bg-background-primary/50 
                           backdrop-blur-sm shadow-lg focus:outline-none focus:ring-2 focus:ring-primary/50 
                           text-lg transition-all duration-200 placeholder:text-text-secondary/50"
                />
                <button
                  type="submit"
                  disabled={isLoading}
                  className="absolute right-2 p-3 bg-primary text-white rounded-full
                           hover:bg-primary/90 transition-all duration-200
                           shadow-md hover:shadow-lg active:scale-95 disabled:opacity-50
                           disabled:hover:bg-primary disabled:cursor-not-allowed"
                >
                  <svg 
                    xmlns="http://www.w3.org/2000/svg" 
                    fill="none" 
                    viewBox="0 0 24 24" 
                    strokeWidth="1.5" 
                    stroke="currentColor" 
                    className={`size-6 ${isLoading ? 'animate-pulse' : ''}`}
                  >
                    <path 
                      strokeLinecap="round" 
                      strokeLinejoin="round" 
                      d="M6 12 3.269 3.125A59.769 59.769 0 0 1 21.485 12 59.768 59.768 0 0 1 3.27 20.875L5.999 12Zm0 0h7.5" 
                    />
                  </svg>
                </button>
              </div>
            </form>

            <div className="mt-12">
              <h3 className="text-xl font-semibold text-text-primary mb-6">Try these examples:</h3>
              <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
                {EXAMPLE_PROMPTS.map((prompt, index) => (
                  <button
                    key={index}
                    onClick={() => sendMessage(prompt.prompt)}
                    className="group p-6 rounded-2xl border border-border/50 bg-background-primary/30 
                             hover:bg-background-primary/50 backdrop-blur-sm transition-all duration-200
                             hover:border-primary/50 hover:shadow-lg text-left"
                  >
                    <div className="flex items-center justify-between mb-2">
                      <h4 className="font-semibold text-text-primary">{prompt.title}</h4>
                      <svg
                        className="w-5 h-5 text-primary opacity-0 group-hover:opacity-100 transition-opacity duration-200" 
                        fill="none"
                        stroke="currentColor"
                        viewBox="0 0 24 24"
                      >
                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
                      </svg>
                    </div>
                    <p className="text-text-secondary text-sm mb-2">{prompt.description}</p>
                    <p className="text-primary text-sm font-medium truncate">{prompt.prompt}</p>
                  </button>
                ))}
              </div>
            </div>
          </div>
        ) : (
          <div className="bg-background-primary/50 backdrop-blur-sm rounded-2xl shadow-lg border border-border/50 overflow-hidden w-full h-[700px] flex flex-col">
            <div className="p-6 border-b border-border/50 bg-background-secondary/30">
              <h2 className="text-xl font-semibold text-text-primary">AI Learning Assistant</h2>
            </div>

            <div
              ref={chatContainerRef}
              className="flex-1 overflow-y-auto p-6 space-y-6"
            >
              {messages.map((message, index) => (
                <div
                  key={index}
                  className={`flex ${message.role === 'user' ? 'justify-end' : 'justify-start'}`}
                >
                  <div
                    className={`max-w-[80%] p-4 rounded-2xl shadow-sm ${
                      message.role === 'user'
                        ? 'bg-primary text-white rounded-br-none'
                        : 'bg-background-secondary/50 text-text-primary rounded-bl-none'
                    }`}
                  >
                    {message.content}
                    {message.isStreaming && (
                      <span className="inline-block w-2.5 h-2.5 ml-1.5 bg-primary rounded-full animate-pulse" />
                    )}
                  </div>
                </div>
              ))}
            </div>

            <div className="p-6 border-t border-border/50 bg-background-secondary/30">
              <form onSubmit={onSubmit}>
                <div className="relative flex items-center">
                  <input
                    type="text"
                    value={input}
                    onChange={handleInputChange}
                    placeholder={isLoading ? "AI is thinking..." : "Type your message..."}
                    disabled={isLoading}
                    className="w-full p-4 pr-32 rounded-xl border border-border/50 bg-background-primary/50 
                             backdrop-blur-sm focus:outline-none focus:ring-2 focus:ring-primary/50
                             text-base transition-all duration-200 disabled:opacity-50"
                  />
                  <button
                    type="submit"
                    disabled={isLoading}
                    className="absolute right-2 p-2 bg-primary text-white rounded-full
                             hover:bg-primary/90 transition-all duration-200
                             shadow-md hover:shadow-lg active:scale-95 disabled:opacity-50
                             disabled:hover:bg-primary disabled:cursor-not-allowed"
                  >
                    <svg 
                      xmlns="http://www.w3.org/2000/svg" 
                      fill="none" 
                      viewBox="0 0 24 24" 
                      strokeWidth="1.5" 
                      stroke="currentColor" 
                      className={`size-5 ${isLoading ? 'animate-pulse' : ''}`}
                    >
                      <path 
                        strokeLinecap="round" 
                        strokeLinejoin="round" 
                        d="M6 12 3.269 3.125A59.769 59.769 0 0 1 21.485 12 59.768 59.768 0 0 1 3.27 20.875L5.999 12Zm0 0h7.5" 
                      />
                    </svg>
                  </button>
                </div>
              </form>
            </div>
          </div>
        )}
      </div>
    </section>
  )
}