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[];
  conversationsLoading: boolean;
  hasMoreConversations: boolean;
  loadingMoreConversations: boolean;
  currentConversationId: string | null;
  currentConversationTitle: string;
  searchQuery: string;
  searchTimer: ReturnType<typeof setTimeout> | null;
  conversationsOffset: number;
  conversationsLimit: number;
}

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