import { defineStore } from 'pinia';

export interface ConversationSummary {
  id: string;
  title: string;
  updated_at: string | number;
  total_messages?: number;
  total_tools?: number;
}

interface ConversationState {
  conversations: ConversationSummary[];
  searchResults: ConversationSummary[];
  conversationsLoading: boolean;
  hasMoreConversations: boolean;
  loadingMoreConversations: boolean;
  currentConversationId: string | null;
  currentConversationTitle: string;
  searchQuery: string;
  searchTimer: ReturnType<typeof setTimeout> | null;
  searchActive: boolean;
  searchInProgress: boolean;
  searchMoreAvailable: boolean;
  searchOffset: number;
  searchTotal: number;
  conversationsOffset: number;
  conversationsLimit: number;
}

export const useConversationStore = defineStore('conversation', {
  state: (): ConversationState => ({
    conversations: [],
    searchResults: [],
    conversationsLoading: false,
    hasMoreConversations: false,
    loadingMoreConversations: false,
    currentConversationId: null,
    currentConversationTitle: '',
    searchQuery: '',
    searchTimer: null,
    searchActive: false,
    searchInProgress: false,
    searchMoreAvailable: false,
    searchOffset: 0,
    searchTotal: 0,
    conversationsOffset: 0,
    conversationsLimit: 20
  }),
  actions: {
    resetConversations() {
      this.conversations = [];
      this.searchResults = [];
      this.searchActive = false;
      this.searchInProgress = false;
      this.searchMoreAvailable = false;
      this.searchOffset = 0;
      this.searchTotal = 0;
      this.hasMoreConversations = false;
      this.loadingMoreConversations = false;
      this.conversationsOffset = 0;
    },
    cancelSearchTimer() {
      if (this.searchTimer) {
        clearTimeout(this.searchTimer);
        this.searchTimer = null;
      }
    }
  }
});
