import React from 'react';
import { Chat } from '../types';

interface ChatSidebarProps {
    chats: Chat[];
    activeChat: Chat | null;
    onSelectChat: (chat: Chat) => void;
    onNewChat: () => void;
    onDeleteChat: (chatId: string) => void;
    isLoading: boolean;
}

const ChatSidebar: React.FC<ChatSidebarProps> = ({
    chats,
    activeChat,
    onSelectChat,
    onNewChat,
    onDeleteChat,
    isLoading,
}) => {
    return (
        <aside className="w-72 bg-gray-900 flex flex-col h-full">
            {/* Header */}
            <div className="p-4 border-b border-gray-700">
                <button
                    onClick={onNewChat}
                    disabled={isLoading}
                    className="w-full flex items-center justify-center gap-2 px-4 py-3 bg-gradient-to-r from-indigo-600 to-purple-600 hover:from-indigo-700 hover:to-purple-700 text-white font-semibold rounded-xl transition-all duration-200 shadow-lg hover:shadow-indigo-500/25 disabled:opacity-50 disabled:cursor-not-allowed"
                >
                    <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 4v16m8-8H4" />
                    </svg>
                    New Chat
                </button>
            </div>

            {/* Chat List */}
            <div className="flex-1 overflow-y-auto py-2 scrollbar-thin scrollbar-thumb-gray-700 scrollbar-track-transparent">
                {chats.length === 0 ? (
                    <div className="px-4 py-8 text-center text-gray-500 text-sm">
                        No chats yet. Start a new conversation!
                    </div>
                ) : (
                    <div className="space-y-1 px-2">
                        {chats.map((chat) => (
                            <div
                                key={chat.id}
                                className={`group relative flex items-center gap-3 px-3 py-3 rounded-lg cursor-pointer transition-all duration-150 ${activeChat?.id === chat.id
                                    ? 'bg-gray-700/80 text-white'
                                    : 'text-gray-300 hover:bg-gray-800 hover:text-white'
                                    }`}
                                onClick={() => onSelectChat(chat)}
                            >
                                <svg className="w-5 h-5 flex-shrink-0 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z" />
                                </svg>
                                <span className="flex-1 truncate text-sm font-medium">
                                    {chat.title}
                                </span>
                                {/* Delete button - shows on hover */}
                                <button
                                    onClick={(e) => {
                                        e.stopPropagation();
                                        onDeleteChat(chat.id);
                                    }}
                                    className="opacity-0 group-hover:opacity-100 p-1 hover:bg-gray-600 rounded transition-all duration-150"
                                    title="Delete chat"
                                >
                                    <svg className="w-4 h-4 text-gray-400 hover:text-red-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
                                    </svg>
                                </button>
                            </div>
                        ))}
                    </div>
                )}
            </div>

            {/* Footer */}
            <div className="p-4 border-t border-gray-700 space-y-3">
                {/* Manage Documents Button */}
                <button
                    onClick={() => window.dispatchEvent(new CustomEvent('open-documents-modal'))}
                    className="w-full flex items-center gap-3 px-3 py-2 text-gray-300 hover:text-white hover:bg-gray-800 rounded-lg transition-colors text-sm font-medium"
                >
                    <div className="bg-gray-800 p-1.5 rounded-md group-hover:bg-gray-700">
                        <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 6v6m0 0v6m0-6h6m-6 0H6" />
                        </svg>
                    </div>
                    Manage Documents
                </button>

                <div className="flex items-center gap-2 text-xs text-gray-500 px-2 pt-2 border-t border-gray-800">
                    <div className="w-2 h-2 bg-green-500 rounded-full animate-pulse"></div>
                    <span>Created by Muhammadjon</span>
                </div>
            </div>
        </aside>
    );
};

export default ChatSidebar;
