import type { Transaction } from '@/types/transaction'; import Dexie, { type EntityTable } from 'dexie'; export interface SyncMetadata { key: string; value: string; } type WhisperMoneyDB = Dexie & { transactions: EntityTable; sync_metadata: EntityTable; }; let dbInstance: WhisperMoneyDB | null = null; function initializeDatabase(): WhisperMoneyDB { const database = new Dexie('whisper_money') as WhisperMoneyDB; database.version(5).stores({ transactions: 'id, user_id, account_id, updated_at', accounts: 'id, user_id, bank_id, updated_at', categories: 'id, user_id, updated_at', banks: 'id, user_id, updated_at', automation_rules: 'id, user_id, priority, updated_at', account_balances: 'id, account_id, balance_date, updated_at', sync_metadata: 'key', pending_changes: '++id, store, timestamp', }); database.version(6).stores({ transactions: 'id, user_id, account_id, updated_at', accounts: 'id, user_id, bank_id, updated_at', categories: 'id, user_id, updated_at', labels: 'id, user_id, updated_at', banks: 'id, user_id, updated_at', automation_rules: 'id, user_id, priority, updated_at', account_balances: 'id, account_id, balance_date, updated_at', sync_metadata: 'key', pending_changes: '++id, store, timestamp', }); // Version 7: Remove all tables except transactions and sync_metadata database.version(7).stores({ transactions: 'id, user_id, account_id, updated_at', sync_metadata: 'key', // Delete removed tables accounts: null, categories: null, labels: null, banks: null, automation_rules: null, account_balances: null, pending_changes: null, }); // Version 8: Ensure clean state (no schema changes, just trigger upgrade) database.version(8).stores({ transactions: 'id, user_id, account_id, updated_at', sync_metadata: 'key', }); return database; } export const db = new Proxy({} as WhisperMoneyDB, { get(_target, prop) { if (!dbInstance) { dbInstance = initializeDatabase(); } return dbInstance[prop as keyof WhisperMoneyDB]; }, });