'use client';

import React, { createContext, useContext } from 'react';
import { BookProject, UserProfile, Entity, EntityType } from '@/lib/types';

interface ProjectContextType {
    project: BookProject;
    user: UserProfile;
    projectId: string;
    currentChapterId: string;
    setCurrentChapterId: (id: string) => void;
    updateProject: (updates: Partial<BookProject>) => void;
    updateChapter: (chapterId: string, data: any) => void;
    createEntity: (type: EntityType, initialData?: Partial<Entity>) => Promise<string>;
    updateEntity: (entityId: string, data: Partial<Entity>) => Promise<void>;
    deleteEntity: (entityId: string) => Promise<void>;
    deleteProject: () => Promise<void>;
    incrementUsage: () => void;
}

const ProjectContext = createContext<ProjectContextType | null>(null);

export function ProjectProvider({
    value,
    children,
}: {
    value: ProjectContextType;
    children: React.ReactNode;
}) {
    return <ProjectContext.Provider value={value}>{children}</ProjectContext.Provider>;
}

export function useProjectContext(): ProjectContextType {
    const ctx = useContext(ProjectContext);
    if (!ctx) throw new Error('useProjectContext must be used within ProjectProvider');
    return ctx;
}
