'use client';

import { ChatArea } from '@admin/components/chat/chat-area';
import { ChatSidebar } from '@admin/components/chat/chat-sidebar';
import { GroupInfo } from '@admin/components/chat/group-info';
import GeneralHeader from '@admin/components/general-header';
import { chats, groupInfo, messages as initialMessages } from '@admin/data/chatdata';
import AppLayout from '@admin/layouts/app-layout';
import type { Message } from '@admin/lib/types';
import { Head } from '@inertiajs/react';
import { ReactNode, useState } from 'react';

type MobileView = 'sidebar' | 'chat' | 'info';

function Index() {
    const [activeChat, setActiveChat] = useState('2');
    const [messages, setMessages] = useState<Message[]>(initialMessages);
    const [showGroupInfo, setShowGroupInfo] = useState(true);
    const [mobileView, setMobileView] = useState<MobileView>('sidebar');

    const handleSendMessage = (content: string) => {
        const newMessage: Message = {
            id: String(messages.length + 1),
            senderId: 'current',
            senderName: 'You',
            content,
            timestamp: new Date().toLocaleTimeString('en-US', {
                hour: '2-digit',
                minute: '2-digit',
            }),
            read: false,
        };
        setMessages([...messages, newMessage]);
    };

    const handleFileSend = (files: File[]) => {
        files.forEach((file, index) => {
            const newMessage: Message = {
                id: String(messages.length + index + 1),
                senderId: 'current',
                senderName: 'You',
                content: `📎 ${file.name}`,
                timestamp: new Date().toLocaleTimeString('en-US', {
                    hour: '2-digit',
                    minute: '2-digit',
                }),
                read: false,
            };
            setMessages((prev) => [...prev, newMessage]);
        });
    };

    const handleChatSelect = (chatId: string) => {
        setActiveChat(chatId);
        setMobileView('chat');
    };

    const handleGroupInfoToggle = () => {
        setShowGroupInfo(!showGroupInfo);
        if (!showGroupInfo) {
            setMobileView('info');
        }
    };

    const currentChat = chats?.find((chat) => chat.id === activeChat);

    return (
        <div className="flex h-full w-full flex-col bg-card">
            <Head title="Chats" />
            <div className="border-b border-gray-200 bg-card">
                <GeneralHeader title="Chats" description="Manage your chats efficiently" page="Chats" />
            </div>
            <div className="flex h-full overflow-hidden">
                <div className={`${mobileView === 'sidebar' ? 'flex' : 'hidden'} w-full flex-shrink-0 overflow-y-auto md:flex md:w-80`}>
                    <ChatSidebar chats={chats} activeChat={activeChat} onChatSelect={handleChatSelect} />
                </div>

                <div className={`${mobileView === 'chat' ? 'flex' : 'hidden'} h-full flex-1 overflow-y-auto md:flex`}>
                    <ChatArea
                        chatName={currentChat?.name || 'TaskCo Official'}
                        chatAvatar={currentChat?.avatar}
                        memberCount={groupInfo.memberCount}
                        onlineCount={groupInfo.onlineCount}
                        messages={messages}
                        onSendMessage={handleSendMessage}
                        onFileSend={handleFileSend}
                        onBack={() => setMobileView('sidebar')}
                        onInfoClick={handleGroupInfoToggle}
                    />
                </div>

                {showGroupInfo && (
                    <div className={`${mobileView === 'info' ? 'flex' : 'hidden'} no-scrollbar w-full flex-shrink-0 overflow-y-auto md:flex md:w-80`}>
                        <GroupInfo
                            groupInfo={groupInfo}
                            onClose={() => {
                                setShowGroupInfo(false);
                                setMobileView('chat');
                            }}
                            onBack={() => setMobileView('chat')}
                        />
                    </div>
                )}
            </div>
        </div>
    );
}
Index.layout = (page: ReactNode) => (
    <AppLayout
        breadcrumbs={[
            { title: 'Chat', href: '/chats' },
            { title: 'Chats', href: route('users.index') },
        ]}
        title="Chats"
    >
        {page}
    </AppLayout>
);

export default Index;
