'use client';

import React, { useState, useEffect } from 'react';
import { useParams, useRouter, usePathname } from 'next/navigation';
import { useAuthContext } from '@/providers/AuthProvider';
import { ProjectProvider } from '@/providers/ProjectProvider';
import { useProjects } from '@/hooks/useProjects';
import { useChat } from '@/hooks/useChat';
import { ViewMode } from '@/lib/types';
import EditorShell from '@/components/layout/EditorShell';
import ExportModal from '@/components/ExportModal';
import HelpModal from '@/components/HelpModal';
import { Loader2, BookOpen } from 'lucide-react';

function getViewModeFromPath(pathname: string): ViewMode {
    if (pathname.endsWith('/world')) return 'world_building';
    if (pathname.endsWith('/ideas')) return 'ideas';
    if (pathname.endsWith('/workflow')) return 'workflow';
    if (pathname.endsWith('/settings')) return 'settings';
    return 'write';
}

export default function ProjectLayout({ children }: { children: React.ReactNode }) {
    const params = useParams();
    const router = useRouter();
    const pathname = usePathname();
    const projectId = params.id as string;

    const { user, logout, incrementUsage, loading: authLoading } = useAuthContext();
    const {
        projects, setCurrentProjectId,
        updateProject, updateChapter, addChapter,
        createEntity, updateEntity, deleteEntity, deleteProject
    } = useProjects(user);
    const { chatHistory, isGenerating, sendMessage } = useChat();

    const [currentChapterId, setCurrentChapterId] = useState('');
    const [isExportModalOpen, setIsExportModalOpen] = useState(false);
    const [isHelpModalOpen, setIsHelpModalOpen] = useState(false);

    const viewMode = getViewModeFromPath(pathname);

    useEffect(() => {
        if (projectId) setCurrentProjectId(projectId);
    }, [projectId, setCurrentProjectId]);

    useEffect(() => {
        if (!authLoading && !user) router.replace('/login');
    }, [user, authLoading, router]);

    const project = projects.find(p => p.id === projectId);

    useEffect(() => {
        if (project && (!currentChapterId || !project.chapters.some(c => c.id === currentChapterId))) {
            setCurrentChapterId(project.chapters[0]?.id || '');
        }
    }, [project, currentChapterId]);

    if (authLoading || !user) {
        return (
            <div className="h-screen w-full flex flex-col items-center justify-center bg-slate-900 text-white">
                <Loader2 className="animate-spin text-blue-500 mb-4" size={48} />
                <div className="flex items-center gap-2">
                    <BookOpen className="text-blue-500" size={20} />
                    <span className="text-lg font-bold">PlumeIA</span>
                </div>
            </div>
        );
    }

    if (!project) {
        return (
            <div className="h-screen w-full flex flex-col items-center justify-center bg-slate-900 text-white">
                <Loader2 className="animate-spin text-blue-500 mb-4" size={48} />
                <p className="text-slate-400">Chargement du projet...</p>
            </div>
        );
    }

    const handleViewModeChange = (mode: ViewMode) => {
        const base = `/project/${projectId}`;
        switch (mode) {
            case 'write': router.push(base); break;
            case 'world_building': router.push(`${base}/world`); break;
            case 'ideas': router.push(`${base}/ideas`); break;
            case 'workflow': router.push(`${base}/workflow`); break;
            case 'settings': router.push(`${base}/settings`); break;
            case 'dashboard': router.push('/dashboard'); break;
            default: router.push(base);
        }
    };

    return (
        <ProjectProvider value={{
            project,
            user,
            projectId,
            currentChapterId,
            setCurrentChapterId,
            updateProject: (updates) => updateProject(projectId, updates),
            updateChapter: (chapterId, data) => updateChapter(projectId, chapterId, data),
            createEntity: (type, data) => createEntity(projectId, type, data),
            updateEntity: (entityId, data) => updateEntity(projectId, entityId, data),
            deleteEntity: (entityId) => deleteEntity(projectId, entityId),
            deleteProject: () => deleteProject(projectId),
            incrementUsage,
        }}>
            <EditorShell
                project={project}
                user={user}
                viewMode={viewMode}
                currentChapterId={currentChapterId}
                chatHistory={chatHistory}
                isGenerating={isGenerating}
                onViewModeChange={handleViewModeChange}
                onChapterSelect={(id) => { setCurrentChapterId(id); router.push(`/project/${projectId}`); }}
                onUpdateProject={(updates) => updateProject(projectId, updates)}
                onAddChapter={async () => {
                    const id = await addChapter(projectId, {});
                    if (id) {
                        setCurrentChapterId(id);
                        router.push(`/project/${projectId}`);
                    }
                }}
                onDeleteChapter={(id) => {
                    if (project.chapters.length > 1) {
                        const newChapters = project.chapters.filter(c => c.id !== id);
                        updateProject(projectId, { chapters: newChapters });
                        if (currentChapterId === id) setCurrentChapterId(newChapters[0].id);
                    }
                }}
                onLogout={() => { logout(); router.push('/'); }}
                onSendMessage={(msg) => {
                    if (project && user) sendMessage(project, 'global', msg, user, incrementUsage);
                }}
                onInsertText={() => { }}
                onOpenExport={() => setIsExportModalOpen(true)}
                onOpenHelp={() => setIsHelpModalOpen(true)}
            >
                <ExportModal isOpen={isExportModalOpen} onClose={() => setIsExportModalOpen(false)} project={project} onPrint={() => { }} />
                <HelpModal isOpen={isHelpModalOpen} onClose={() => setIsHelpModalOpen(false)} viewMode={viewMode} />
                {children}
            </EditorShell>
        </ProjectProvider>
    );
}
