File size: 24,390 Bytes
da57416
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
"""
database.py β€” SQLite + FTS5 Database Layer for Banking Email Intelligence

Provides persistent storage for emails, transactions, offers, and sync metadata.
Supports full-text search via SQLite FTS5 for offline RAG retrieval.
"""

import sqlite3
import os
import logging
from datetime import datetime, timedelta
from typing import Optional, Dict, List, Any, Tuple
from contextlib import contextmanager

from config import DB_PATH, DATA_DIR, INITIAL_SYNC_DAYS

logger = logging.getLogger(__name__)


class BankingDatabase:
    """SQLite database with FTS5 full-text search for banking email storage."""

    def __init__(self, db_path: str = DB_PATH):
        self.db_path = db_path
        os.makedirs(os.path.dirname(db_path), exist_ok=True)
        self._init_schema()

    @contextmanager
    def _get_connection(self):
        """Context manager for database connections with WAL mode."""
        conn = sqlite3.connect(self.db_path, timeout=30)
        conn.row_factory = sqlite3.Row
        conn.execute("PRAGMA journal_mode=WAL")
        conn.execute("PRAGMA foreign_keys=ON")
        try:
            yield conn
            conn.commit()
        except Exception:
            conn.rollback()
            raise
        finally:
            conn.close()

    def _init_schema(self):
        """Create all tables and FTS5 indexes if they don't exist."""
        with self._get_connection() as conn:
            # Core email storage
            conn.execute("""
                CREATE TABLE IF NOT EXISTS emails (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    message_id TEXT UNIQUE NOT NULL,
                    sender TEXT NOT NULL,
                    sender_domain TEXT NOT NULL,
                    bank_name TEXT,
                    subject TEXT,
                    body_text TEXT,
                    body_html TEXT,
                    received_date DATETIME NOT NULL,
                    folder TEXT,
                    has_attachments BOOLEAN DEFAULT 0,
                    email_type TEXT,
                    vision_needed BOOLEAN DEFAULT 0,
                    synced_at DATETIME DEFAULT CURRENT_TIMESTAMP
                )
            """)

            # FTS5 virtual table for full-text search
            conn.execute("""
                CREATE VIRTUAL TABLE IF NOT EXISTS emails_fts USING fts5(
                    subject, body_text, bank_name,
                    content='emails', content_rowid='id'
                )
            """)

            # Triggers to keep FTS5 in sync with emails table
            conn.execute("""
                CREATE TRIGGER IF NOT EXISTS emails_fts_insert AFTER INSERT ON emails BEGIN
                    INSERT INTO emails_fts(rowid, subject, body_text, bank_name)
                    VALUES (new.id, new.subject, new.body_text, new.bank_name);
                END
            """)

            conn.execute("""
                CREATE TRIGGER IF NOT EXISTS emails_fts_delete AFTER DELETE ON emails BEGIN
                    INSERT INTO emails_fts(emails_fts, rowid, subject, body_text, bank_name)
                    VALUES ('delete', old.id, old.subject, old.body_text, old.bank_name);
                END
            """)

            conn.execute("""
                CREATE TRIGGER IF NOT EXISTS emails_fts_update AFTER UPDATE ON emails BEGIN
                    INSERT INTO emails_fts(emails_fts, rowid, subject, body_text, bank_name)
                    VALUES ('delete', old.id, old.subject, old.body_text, old.bank_name);
                    INSERT INTO emails_fts(rowid, subject, body_text, bank_name)
                    VALUES (new.id, new.subject, new.body_text, new.bank_name);
                END
            """)

            # Parsed transactions
            conn.execute("""
                CREATE TABLE IF NOT EXISTS transactions (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    email_id INTEGER REFERENCES emails(id) ON DELETE CASCADE,
                    bank_name TEXT,
                    amount REAL,
                    currency TEXT DEFAULT 'INR',
                    transaction_type TEXT,
                    category TEXT,
                    merchant TEXT,
                    card_last4 TEXT,
                    transaction_date DATETIME,
                    raw_text TEXT
                )
            """)

            # Promotional offers
            conn.execute("""
                CREATE TABLE IF NOT EXISTS offers (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    email_id INTEGER REFERENCES emails(id) ON DELETE CASCADE,
                    bank_name TEXT,
                    offer_text TEXT,
                    source TEXT DEFAULT 'email_body',
                    category TEXT,
                    valid_until DATETIME,
                    extracted_at DATETIME DEFAULT CURRENT_TIMESTAMP
                )
            """)

            # Sync log
            conn.execute("""
                CREATE TABLE IF NOT EXISTS sync_log (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    sync_started DATETIME,
                    sync_completed DATETIME,
                    emails_fetched INTEGER DEFAULT 0,
                    folder TEXT,
                    status TEXT DEFAULT 'started'
                )
            """)

            # Indexes for frequent queries
            conn.execute("""
                CREATE INDEX IF NOT EXISTS idx_emails_received
                ON emails(received_date DESC)
            """)
            conn.execute("""
                CREATE INDEX IF NOT EXISTS idx_emails_bank
                ON emails(bank_name)
            """)
            conn.execute("""
                CREATE INDEX IF NOT EXISTS idx_emails_type
                ON emails(email_type)
            """)
            conn.execute("""
                CREATE INDEX IF NOT EXISTS idx_transactions_date
                ON transactions(transaction_date DESC)
            """)
            conn.execute("""
                CREATE INDEX IF NOT EXISTS idx_transactions_bank
                ON transactions(bank_name)
            """)
            conn.execute("""
                CREATE INDEX IF NOT EXISTS idx_offers_bank
                ON offers(bank_name)
            """)

        logger.info(f"Database initialized at {self.db_path}")

    # ─── Delta Sync Methods ──────────────────────────────────────────────────

    def get_latest_sync_date(self) -> Optional[datetime]:
        """Get the latest received_date from the emails table for delta sync."""
        with self._get_connection() as conn:
            row = conn.execute(
                "SELECT MAX(received_date) as latest FROM emails"
            ).fetchone()
            if row and row["latest"]:
                try:
                    return datetime.fromisoformat(row["latest"])
                except (ValueError, TypeError):
                    # Try parsing other date formats
                    try:
                        return datetime.strptime(row["latest"], "%Y-%m-%d %H:%M:%S")
                    except (ValueError, TypeError):
                        return None
            return None

    def get_initial_sync_date(self) -> datetime:
        """Get the date to start from on first sync (90 days ago)."""
        return datetime.now() - timedelta(days=INITIAL_SYNC_DAYS)

    def get_sync_since_date(self) -> datetime:
        """Get the date to sync from β€” latest stored date or 90 days ago."""
        latest = self.get_latest_sync_date()
        if latest:
            return latest
        return self.get_initial_sync_date()

    # ─── Email CRUD ──────────────────────────────────────────────────────────

    def insert_email(self, email_data: Dict[str, Any]) -> Optional[int]:
        """
        Insert or ignore an email record. Returns the row ID if inserted, None if duplicate.
        email_data keys: message_id, sender, sender_domain, bank_name, subject,
                         body_text, body_html, received_date, folder, has_attachments,
                         email_type, vision_needed
        """
        with self._get_connection() as conn:
            try:
                cursor = conn.execute("""
                    INSERT OR IGNORE INTO emails
                    (message_id, sender, sender_domain, bank_name, subject,
                     body_text, body_html, received_date, folder, has_attachments,
                     email_type, vision_needed)
                    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
                """, (
                    email_data.get("message_id"),
                    email_data.get("sender"),
                    email_data.get("sender_domain"),
                    email_data.get("bank_name"),
                    email_data.get("subject"),
                    email_data.get("body_text"),
                    email_data.get("body_html"),
                    email_data.get("received_date"),
                    email_data.get("folder"),
                    email_data.get("has_attachments", False),
                    email_data.get("email_type"),
                    email_data.get("vision_needed", False),
                ))
                if cursor.rowcount > 0:
                    return cursor.lastrowid
                return None
            except sqlite3.IntegrityError:
                logger.debug(f"Duplicate email: {email_data.get('message_id')}")
                return None

    def get_email_by_id(self, email_id: int) -> Optional[Dict]:
        """Get a single email record by ID."""
        with self._get_connection() as conn:
            row = conn.execute("SELECT * FROM emails WHERE id = ?", (email_id,)).fetchone()
            return dict(row) if row else None

    def get_emails_needing_vision(self) -> List[Dict]:
        """Get emails flagged for vision processing."""
        with self._get_connection() as conn:
            rows = conn.execute(
                "SELECT * FROM emails WHERE vision_needed = 1 ORDER BY received_date DESC"
            ).fetchall()
            return [dict(r) for r in rows]

    def mark_vision_processed(self, email_id: int):
        """Mark an email as no longer needing vision processing."""
        with self._get_connection() as conn:
            conn.execute(
                "UPDATE emails SET vision_needed = 0 WHERE id = ?", (email_id,)
            )

    def update_email_type(self, email_id: int, email_type: str):
        """Update the classified type of an email."""
        with self._get_connection() as conn:
            conn.execute(
                "UPDATE emails SET email_type = ? WHERE id = ?",
                (email_type, email_id)
            )

    def get_recent_emails(self, limit: int = 50) -> List[Dict]:
        """Get recent emails ordered by received date."""
        with self._get_connection() as conn:
            rows = conn.execute(
                "SELECT * FROM emails ORDER BY received_date DESC LIMIT ?", (limit,)
            ).fetchall()
            return [dict(r) for r in rows]

    def get_email_count(self) -> int:
        """Get total number of stored emails."""
        with self._get_connection() as conn:
            row = conn.execute("SELECT COUNT(*) as cnt FROM emails").fetchone()
            return row["cnt"] if row else 0

    # ─── Transaction CRUD ────────────────────────────────────────────────────

    def insert_transaction(self, txn_data: Dict[str, Any]) -> int:
        """Insert a parsed transaction record. Returns row ID."""
        with self._get_connection() as conn:
            cursor = conn.execute("""
                INSERT INTO transactions
                (email_id, bank_name, amount, currency, transaction_type,
                 category, merchant, card_last4, transaction_date, raw_text)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            """, (
                txn_data.get("email_id"),
                txn_data.get("bank_name"),
                txn_data.get("amount"),
                txn_data.get("currency", "INR"),
                txn_data.get("transaction_type"),
                txn_data.get("category"),
                txn_data.get("merchant"),
                txn_data.get("card_last4"),
                txn_data.get("transaction_date"),
                txn_data.get("raw_text"),
            ))
            return cursor.lastrowid

    def get_transactions(
        self,
        bank_name: Optional[str] = None,
        transaction_type: Optional[str] = None,
        days: Optional[int] = None,
        limit: int = 200,
    ) -> List[Dict]:
        """Get transactions with optional filters, including email subject and received_date."""
        query = """
            SELECT t.*, e.subject AS email_subject, e.received_date AS email_received_date
            FROM transactions t
            LEFT JOIN emails e ON t.email_id = e.id
            WHERE 1=1
        """
        params: List[Any] = []

        if bank_name:
            query += " AND t.bank_name = ?"
            params.append(bank_name)
        if transaction_type:
            query += " AND t.transaction_type = ?"
            params.append(transaction_type)
        if days:
            cutoff = (datetime.now() - timedelta(days=days)).isoformat()
            query += " AND COALESCE(e.received_date, t.transaction_date) >= ?"
            params.append(cutoff)

        query += " ORDER BY COALESCE(e.received_date, t.transaction_date) DESC LIMIT ?"
        params.append(limit)

        with self._get_connection() as conn:
            rows = conn.execute(query, params).fetchall()
            return [dict(r) for r in rows]

    def get_transaction_count(self) -> int:
        """Get total number of parsed transactions."""
        with self._get_connection() as conn:
            row = conn.execute("SELECT COUNT(*) as cnt FROM transactions").fetchone()
            return row["cnt"] if row else 0

    def get_spending_summary(self) -> Dict[str, Any]:
        """Get aggregated spending data for visualization."""
        with self._get_connection() as conn:
            # By category
            by_category = conn.execute("""
                SELECT category, SUM(amount) as total, COUNT(*) as count
                FROM transactions
                WHERE transaction_type = 'debit' AND category IS NOT NULL
                GROUP BY category
                ORDER BY total DESC
            """).fetchall()

            # By bank
            by_bank = conn.execute("""
                SELECT bank_name, SUM(amount) as total, COUNT(*) as count
                FROM transactions
                WHERE transaction_type = 'debit' AND bank_name IS NOT NULL
                GROUP BY bank_name
                ORDER BY total DESC
            """).fetchall()

            # Daywise trend β€” use email received_date (reliable ISO) for grouping
            # Use substr() as fallback since strftime cannot parse timezone offsets
            daywise = conn.execute("""
                SELECT COALESCE(
                    date(COALESCE(e.received_date, t.transaction_date)),
                    substr(COALESCE(e.received_date, t.transaction_date), 1, 10),
                    'Unknown'
                ) as day,
                SUM(CASE WHEN t.transaction_type='debit' THEN t.amount ELSE 0 END) as debits,
                SUM(CASE WHEN t.transaction_type='credit' THEN t.amount ELSE 0 END) as credits,
                COUNT(*) as count
                FROM transactions t
                LEFT JOIN emails e ON t.email_id = e.id
                GROUP BY day
                HAVING day != 'Unknown' AND day IS NOT NULL AND length(day) = 10
                ORDER BY day
            """).fetchall()

            # Totals
            totals = conn.execute("""
                SELECT
                    SUM(CASE WHEN transaction_type='debit' THEN amount ELSE 0 END) as total_debits,
                    SUM(CASE WHEN transaction_type='credit' THEN amount ELSE 0 END) as total_credits,
                    COUNT(*) as total_count
                FROM transactions
            """).fetchone()

            return {
                "by_category": [dict(r) for r in by_category],
                "by_bank": [dict(r) for r in by_bank],
                "daywise": [dict(r) for r in daywise],
                "total_debits": totals["total_debits"] or 0 if totals else 0,
                "total_credits": totals["total_credits"] or 0 if totals else 0,
                "total_count": totals["total_count"] or 0 if totals else 0,
            }

    # ─── Offer CRUD ──────────────────────────────────────────────────────────

    def insert_offer(self, offer_data: Dict[str, Any]) -> int:
        """Insert an extracted offer record. Returns row ID."""
        with self._get_connection() as conn:
            cursor = conn.execute("""
                INSERT INTO offers
                (email_id, bank_name, offer_text, source, category, valid_until)
                VALUES (?, ?, ?, ?, ?, ?)
            """, (
                offer_data.get("email_id"),
                offer_data.get("bank_name"),
                offer_data.get("offer_text"),
                offer_data.get("source", "email_body"),
                offer_data.get("category"),
                offer_data.get("valid_until"),
            ))
            return cursor.lastrowid

    def get_offers(self, bank_name: Optional[str] = None, limit: int = 100) -> List[Dict]:
        """Get all cached offers with optional bank filter."""
        query = "SELECT o.*, e.subject as email_subject, e.body_text as email_body, e.sender as email_sender, e.received_date as email_date FROM offers o LEFT JOIN emails e ON o.email_id = e.id WHERE 1=1"
        params: List[Any] = []

        if bank_name:
            query += " AND o.bank_name = ?"
            params.append(bank_name)

        query += " ORDER BY o.extracted_at DESC LIMIT ?"
        params.append(limit)

        with self._get_connection() as conn:
            rows = conn.execute(query, params).fetchall()
            return [dict(r) for r in rows]

    def get_offer_count(self) -> int:
        """Get total number of extracted offers."""
        with self._get_connection() as conn:
            row = conn.execute("SELECT COUNT(*) as cnt FROM offers").fetchone()
            return row["cnt"] if row else 0

    # ─── FTS5 Search ─────────────────────────────────────────────────────────

    def search_fts(self, query: str, limit: int = 20) -> List[Dict]:
        """
        Full-text search across emails using FTS5.
        Returns matching email records with relevance ranking.
        """
        with self._get_connection() as conn:
            # Use FTS5 MATCH with BM25 ranking
            rows = conn.execute("""
                SELECT e.*, rank
                FROM emails_fts fts
                JOIN emails e ON e.id = fts.rowid
                WHERE emails_fts MATCH ?
                ORDER BY rank
                LIMIT ?
            """, (query, limit)).fetchall()
            return [dict(r) for r in rows]

    def search_offers_fts(self, query: str, limit: int = 20) -> List[Dict]:
        """Search emails classified as offers using FTS5."""
        with self._get_connection() as conn:
            rows = conn.execute("""
                SELECT e.*, rank
                FROM emails_fts fts
                JOIN emails e ON e.id = fts.rowid
                WHERE emails_fts MATCH ?
                AND e.email_type = 'offer'
                ORDER BY rank
                LIMIT ?
            """, (query, limit)).fetchall()
            return [dict(r) for r in rows]

    # ─── Sync Log ────────────────────────────────────────────────────────────

    def start_sync_log(self, folder: str) -> int:
        """Start a sync log entry. Returns the log ID."""
        with self._get_connection() as conn:
            cursor = conn.execute("""
                INSERT INTO sync_log (sync_started, folder, status)
                VALUES (?, ?, 'in_progress')
            """, (datetime.now().isoformat(), folder))
            return cursor.lastrowid

    def complete_sync_log(self, log_id: int, emails_fetched: int, status: str = "success"):
        """Complete a sync log entry."""
        with self._get_connection() as conn:
            conn.execute("""
                UPDATE sync_log
                SET sync_completed = ?, emails_fetched = ?, status = ?
                WHERE id = ?
            """, (datetime.now().isoformat(), emails_fetched, status, log_id))

    def get_sync_stats(self) -> Dict[str, Any]:
        """Get sync statistics."""
        with self._get_connection() as conn:
            total_syncs = conn.execute("SELECT COUNT(*) as cnt FROM sync_log").fetchone()
            total_emails = conn.execute("SELECT SUM(emails_fetched) as total FROM sync_log WHERE status='success'").fetchone()
            last_sync = conn.execute(
                "SELECT * FROM sync_log ORDER BY sync_started DESC LIMIT 1"
            ).fetchone()

            return {
                "total_syncs": total_syncs["cnt"] if total_syncs else 0,
                "total_emails_fetched": total_emails["total"] or 0 if total_emails else 0,
                "last_sync": dict(last_sync) if last_sync else None,
            }

    # ─── Dashboard Stats ─────────────────────────────────────────────────────

    def get_dashboard_stats(self) -> Dict[str, Any]:
        """Get overall dashboard statistics."""
        return {
            "email_count": self.get_email_count(),
            "transaction_count": self.get_transaction_count(),
            "offer_count": self.get_offer_count(),
            "sync_stats": self.get_sync_stats(),
            "latest_sync_date": self.get_latest_sync_date(),
        }

    # ─── Vision Agent Support ────────────────────────────────────────────────

    def append_body_text(self, email_id: int, extracted_text: str):
        """
        Append vision-extracted text to an email's body_text field.
        Used by the vision agent to store OCR results so the classifier
        can process them naturally.
        """
        with self._get_connection() as conn:
            current = conn.execute(
                "SELECT body_text FROM emails WHERE id = ?", (email_id,)
            ).fetchone()
            if current:
                existing = current["body_text"] or ""
                updated = existing + "\n[VISION EXTRACTED]\n" + extracted_text
                conn.execute(
                    "UPDATE emails SET body_text = ? WHERE id = ?",
                    (updated, email_id)
                )
                logger.info(f"Appended vision text to email {email_id} ({len(extracted_text)} chars)")

    # ─── Demo: Clear All Data ────────────────────────────────────────────────

    def clear_all_data(self):
        """
        Truncate all tables for demo cleanup on exit.
        Keeps schema intact but removes all data.
        """
        with self._get_connection() as conn:
            conn.execute("DELETE FROM offers")
            conn.execute("DELETE FROM transactions")
            conn.execute("DELETE FROM sync_log")
            conn.execute("DELETE FROM emails")
            # Rebuild FTS5 index
            conn.execute("INSERT INTO emails_fts(emails_fts) VALUES('rebuild')")
            logger.info("All database tables cleared (demo cleanup)")