/**
 * Chat Context - Persists chat messages across tab navigation
 *
 * Without this context, messages are lost when switching tabs
 * because ChatScreen component unmounts and remounts.
 */

import React, { createContext, useContext, useState, useCallback, ReactNode } from 'react';
import type { Message } from '@/types';

interface ChatContextValue {
  messages: Message[];
  setMessages: React.Dispatch<React.SetStateAction<Message[]>>;
  addMessage: (message: Message) => void;
  clearMessages: (initialMessage: Message) => void;
}

const ChatContext = createContext<ChatContextValue | undefined>(undefined);

export function ChatProvider({ children }: { children: ReactNode }) {
  const [messages, setMessages] = useState<Message[]>([
    {
      id: '1',
      role: 'assistant',
      content: "Hello! I'm Julia, your AI wellness companion.\n\nType a message below to chat with me.",
      timestamp: new Date(),
    },
  ]);

  const addMessage = useCallback((message: Message) => {
    setMessages(prev => [...prev, message]);
  }, []);

  const clearMessages = useCallback((initialMessage: Message) => {
    setMessages([initialMessage]);
  }, []);

  return (
    <ChatContext.Provider value={{ messages, setMessages, addMessage, clearMessages }}>
      {children}
    </ChatContext.Provider>
  );
}

export function useChat() {
  const context = useContext(ChatContext);
  if (!context) {
    throw new Error('useChat must be used within ChatProvider');
  }
  return context;
}
