import Dexie, { type EntityTable } from 'dexie';
import { IChatLog, ISession, ISyncEntry, IDraft } from './schema';

const DB_NAME = 'Test01_DB';

export const db = new Dexie(DB_NAME) as Dexie & {
    chatLogs: EntityTable<IChatLog, 'id'>,
    sessions: EntityTable<ISession, 'id'>,
    drafts: EntityTable<IDraft, 'id'>,
    syncQueue: EntityTable<ISyncEntry, 'id'>
};

// Schema definition
db.version(1).stores({
    chatLogs: 'id, sessionId, timestamp', // Query by session or time
    sessions: 'id, status, createdAt', // List active/completed
    drafts: 'id, sessionId, status, createdAt, completedAt, [status+completedAt]', // Index for feeds
    syncQueue: '++id, status' // Queue management
});

export type AppDatabase = typeof db;
