import { useState, createContext, useContext, ReactNode } from 'react';
import { Character } from '../data/schema';

interface CharacterDialogContextType {
    open: boolean;
    setOpen: (open: boolean) => void;
    character: Character | null;
    setCharacter: (character: Character | null) => void;
}

const CharacterDialogContext = createContext<CharacterDialogContextType | undefined>(
    undefined
);

export function CharacterDialogProvider({ children }: { children: ReactNode }) {
    const [open, setOpen] = useState(false);
    const [character, setCharacter] = useState<Character | null>(null);

    return (
        <CharacterDialogContext.Provider value={{ open, setOpen, character, setCharacter }}>
            {children}
        </CharacterDialogContext.Provider>
    );
}

export function useCharacterDialog() {
    const context = useContext(CharacterDialogContext);
    if (!context) {
        throw new Error('useCharacterDialog must be used within a CharacterDialogProvider');
    }
    return context;
}
