import { db } from '@/lib/dexie-db'; import { SyncManager } from '@/lib/sync-manager'; import type { AccountBalance } from '@/types/account'; class AccountBalanceSyncService { private syncManager: SyncManager; constructor() { this.syncManager = new SyncManager({ storeName: 'account_balances', endpoint: '/api/sync/account-balances', }); } async sync() { return await this.syncManager.sync(); } async getAll(): Promise { return await this.syncManager.getAll(); } async getById(id: string): Promise { return (await db.account_balances.get(id)) || null; } async getByAccountId(accountId: number): Promise { try { const allBalances = await this.getAll(); return allBalances.filter((b) => b.account_id === accountId); } catch (error) { console.warn('Failed to get balances from IndexedDB:', error); return []; } } async create( data: Omit, ): Promise { return await this.syncManager.createLocal(data); } async createMany( balances: Omit[], ): Promise { try { const created: AccountBalance[] = []; for (const data of balances) { const result = await this.updateOrCreate( data.account_id, data.balance_date, data.balance, ); created.push(result); } return created; } catch (error) { console.error('Failed to create balances:', error); throw new Error( 'Failed to save balances locally. Please refresh the page and try again.', ); } } async update(id: string, data: Partial): Promise { const existing = await this.getById(id); if (!existing) { throw new Error('Balance not found'); } return await this.syncManager.update( id.toString(), data, ); } async updateOrCreate( accountId: number, balanceDate: string, balance: number, ): Promise { try { const existing = await db.account_balances .where({ account_id: accountId, balance_date: balanceDate }) .first(); if (existing) { const timestamp = new Date().toISOString(); const updated = { ...existing, balance, updated_at: timestamp, }; await db.account_balances.put(updated); await db.pending_changes.add({ store: 'account_balances', operation: 'update', data: { id: existing.id, account_id: accountId, balance_date: balanceDate, balance, }, timestamp, }); return updated; } else { return await this.create({ account_id: accountId, balance_date: balanceDate, balance, }); } } catch (error) { console.error('Failed to update or create balance:', error); throw new Error('Failed to save balance. Please try again.'); } } async delete(id: string): Promise { return await this.syncManager.delete(id); } } export const accountBalanceSyncService = new AccountBalanceSyncService();