import Database from 'better-sqlite3';
import { IChatSessionStore, CreateSessionOptions, SessionListOptions, PaginatedMessages, SessionSearchOptions } from './sessions.js';
import { I as IProviderStore, P as ProviderConfig } from '../provider-types-PTSlRPNB.js';
import { I as ITokenStore } from '../token-store-CSUBgYwn.js';
import { g as ChatSession, b as ChatId, j as ChatSessionConfig, c as ChatMessage } from '../types-ajANVzf7.js';
import { A as AuthToken } from '../types-BxggH0Yh.js';
import './storage.js';
import '../errors-CmVvczxZ.js';
import '../errors-C-so0M4t.js';
import '../agent-CW9XbmG_.js';
import 'zod';

/**
 * Factory function for creating all three SQLite stores from a single database path.
 *
 * @example
 * ```ts
 * import { createSQLiteStorage } from "@witqq/agent-sdk/chat/sqlite";
 *
 * const storage = createSQLiteStorage("/data/chat.db");
 * // storage.sessionStore  — IChatSessionStore
 * // storage.providerStore — IProviderStore
 * // storage.tokenStore    — ITokenStore
 * // storage.db            — raw Database instance
 * ```
 */

interface SQLiteStorageOptions {
    /** Path to SQLite database file. Use ":memory:" for in-memory database. */
    dbPath: string;
    /** Optional pre-created Database instance. If provided, dbPath is ignored. */
    db?: Database.Database;
}
interface SQLiteStorage {
    /** The underlying better-sqlite3 Database instance */
    db: Database.Database;
    /** Session store for chat sessions and messages */
    sessionStore: IChatSessionStore;
    /** Provider store for provider configurations */
    providerStore: IProviderStore;
    /** Token store for auth tokens */
    tokenStore: ITokenStore;
}
/**
 * Create all three SQLite stores sharing a single database.
 *
 * Requires `better-sqlite3` as a peer dependency.
 * Schema tables are auto-created on first use.
 *
 * @param pathOrOptions - Database file path string, or options object
 * @returns Object with db, sessionStore, providerStore, tokenStore
 *
 * @throws If better-sqlite3 is not installed
 */
declare function createSQLiteStorage(pathOrOptions: string | SQLiteStorageOptions): SQLiteStorage;

/**
 * SQLite-backed session store implementing IChatSessionStore.
 *
 * Uses better-sqlite3 for synchronous database access wrapped in
 * async interface. Messages are stored with position ordering for
 * deterministic retrieval. Parts and metadata are JSON-serialized.
 *
 * Accepts a shared Database instance — multiple stores can share
 * a single SQLite file via the factory function.
 */

declare class SQLiteSessionStore implements IChatSessionStore {
    private readonly db;
    constructor(db: Database.Database);
    createSession(options?: CreateSessionOptions): Promise<ChatSession>;
    getSession(id: ChatId): Promise<ChatSession | null>;
    listSessions(options?: SessionListOptions): Promise<ChatSession[]>;
    updateTitle(id: ChatId, title: string): Promise<void>;
    updateConfig(id: ChatId, config: Partial<ChatSessionConfig>): Promise<void>;
    deleteSession(id: ChatId): Promise<void>;
    appendMessage(sessionId: ChatId, message: ChatMessage): Promise<void>;
    saveMessages(sessionId: ChatId, messages: ChatMessage[]): Promise<void>;
    loadMessages(sessionId: ChatId, options?: {
        limit?: number;
        offset?: number;
    }): Promise<PaginatedMessages>;
    searchSessions(options: SessionSearchOptions): Promise<ChatSession[]>;
    count(): Promise<number>;
    clear(): Promise<void>;
    private getNextPosition;
    private incrementMessageCount;
    private updateSessionMeta;
    private loadAllMessages;
    private rowToSession;
    private buildSession;
}

/**
 * SQLite-backed provider store implementing IProviderStore.
 *
 * Stores provider configurations in a `providers` table within
 * a shared SQLite database. Schema auto-created on construction.
 */

declare class SQLiteProviderStore implements IProviderStore {
    private readonly db;
    constructor(db: Database.Database);
    create(config: ProviderConfig): Promise<void>;
    get(id: string): Promise<ProviderConfig | null>;
    update(id: string, changes: Partial<Omit<ProviderConfig, "id" | "createdAt">>): Promise<void>;
    delete(id: string): Promise<void>;
    list(): Promise<ProviderConfig[]>;
}

/**
 * SQLite-backed token store implementing ITokenStore.
 *
 * Stores auth tokens as JSON in a `tokens` table within
 * a shared SQLite database. Schema auto-created on construction.
 */

declare class SQLiteTokenStore implements ITokenStore {
    private readonly db;
    constructor(db: Database.Database);
    save(provider: string, token: AuthToken): Promise<void>;
    load(provider: string): Promise<AuthToken | null>;
    clear(provider: string): Promise<void>;
    clearAll(): Promise<void>;
    list(): Promise<string[]>;
}

export { SQLiteProviderStore, SQLiteSessionStore, type SQLiteStorage, type SQLiteStorageOptions, SQLiteTokenStore, createSQLiteStorage };
