import React, { createContext, useContext, useState, ReactNode } from 'react';
import type {
  Message,
  SearchResult,
  SearchSource,
  SearchCommand,
  MockResponse,
} from '../components/assistant/xertica-assistant/types';

// Re-export shared types for backward compatibility
export type { Message, SearchResult, SearchSource, SearchCommand, MockResponse };

/**
 * @deprecated Use `Suggestion` from `xertica-assistant/types` instead.
 * Kept for backward compatibility with existing consumers.
 */
export interface Sugestao {
  id: string;
  texto: string;
  icon?: ReactNode;
}

/**
 * A saved conversation in the AssistenteContext (Portuguese naming convention).
 * Note: `Conversation` in `xertica-assistant/types` uses English naming.
 */
export interface Conversa {
  id: string;
  titulo: string;
  mensagens: Message[];
  ultimaMensagem: string;
  timestamp: string;
  favorita?: boolean;
}

interface AssistenteContextType {
  historico: Message[];
  conversaAtual: string;
  setConversaAtual: (id: string) => void;
  conversas: Conversa[];
  setConversas: React.Dispatch<React.SetStateAction<Conversa[]>>;
  isTyping: boolean;
  setIsTyping: (typing: boolean) => void;
  abaSelecionada: 'chat' | 'historico' | 'favoritos';
  setAbaSelecionada: (aba: 'chat' | 'historico' | 'favoritos') => void;
  editingDocument: { content: string; title: string } | null;
  setEditingDocument: (doc: { content: string; title: string } | null) => void;
  searchFilter: 'all' | 'document' | 'project' | 'conversation' | 'file' | 'contact';
  setSearchFilter: (
    filter: 'all' | 'document' | 'project' | 'conversation' | 'file' | 'contact'
  ) => void;
  savedSearches: string[];
  setSavedSearches: React.Dispatch<React.SetStateAction<string[]>>;
}

const AssistenteContext = createContext<AssistenteContextType | undefined>(undefined);

export function AssistenteProvider({ children }: { children: ReactNode }) {
  const [conversaAtual, setConversaAtual] = useState<string>('1');
  const [conversas, setConversas] = useState<Conversa[]>([
    {
      id: '1',
      titulo: 'Nova Conversa',
      mensagens: [],
      ultimaMensagem: '',
      timestamp: new Date().toISOString(),
      favorita: false,
    },
  ]);
  const [isTyping, setIsTyping] = useState(false);
  const [abaSelecionada, setAbaSelecionada] = useState<'chat' | 'historico' | 'favoritos'>('chat');
  const [editingDocument, setEditingDocument] = useState<{ content: string; title: string } | null>(
    null
  );
  const [searchFilter, setSearchFilter] = useState<
    'all' | 'document' | 'project' | 'conversation' | 'file' | 'contact'
  >('all');
  const [savedSearches, setSavedSearches] = useState<string[]>([]);

  const historico = conversas.find(c => c.id === conversaAtual)?.mensagens || [];

  return (
    <AssistenteContext.Provider
      value={{
        historico,
        conversaAtual,
        setConversaAtual,
        conversas,
        setConversas,
        isTyping,
        setIsTyping,
        abaSelecionada,
        setAbaSelecionada,
        editingDocument,
        setEditingDocument,
        searchFilter,
        setSearchFilter,
        savedSearches,
        setSavedSearches,
      }}
    >
      {children}
    </AssistenteContext.Provider>
  );
}

export function useAssistente() {
  const context = useContext(AssistenteContext);
  if (!context) {
    throw new Error('useAssistente must be used within AssistenteProvider');
  }
  return context;
}
