File size: 28,373 Bytes
62be02c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
"""
SaulGPT — Database Layer (Hybrid PostgreSQL / SQLite)
======================================================
Unified persistence for users, conversations, and messages.
Automatically uses Supabase PostgreSQL when DATABASE_URL is present,
and cleanly falls back to local SQLite (saulgpt.db) when offline.
Includes connection handling, automatic retry, and FOREIGN KEY safety guards.
"""

import json
import sqlite3
import os
from datetime import datetime
from typing import Optional, List, Dict, Any

from dotenv import load_dotenv
load_dotenv(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", ".env"))
load_dotenv(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".env"))

DATABASE_URL = os.getenv("DATABASE_URL", "").strip()

try:
    import psycopg2
    from psycopg2.extras import RealDictCursor, Json
    PSYCOPG2_AVAILABLE = True
except ImportError:
    PSYCOPG2_AVAILABLE = False


def is_postgres() -> bool:
    return bool(PSYCOPG2_AVAILABLE and DATABASE_URL and DATABASE_URL.startswith("postgres"))


DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "saulgpt.db")


def _get_sqlite_conn() -> sqlite3.Connection:
    """Get a SQLite connection with WAL journal mode, busy timeout, and foreign keys."""
    conn = sqlite3.connect(DB_PATH, timeout=30.0, check_same_thread=False)
    conn.row_factory = sqlite3.Row
    try:
        conn.execute("PRAGMA busy_timeout=30000")
        conn.execute("PRAGMA foreign_keys=ON")
    except Exception:
        pass
    return conn


def _get_pg_conn():
    """Get a PostgreSQL connection to Supabase."""
    return psycopg2.connect(DATABASE_URL, cursor_factory=RealDictCursor)


def init_db():
    """Create tables if they don't exist and ensure default guest user."""
    if is_postgres():
        try:
            conn = _get_pg_conn()
            with conn.cursor() as cur:
                cur.execute("""
                    CREATE TABLE IF NOT EXISTS users (
                        id SERIAL PRIMARY KEY,
                        email TEXT UNIQUE NOT NULL,
                        username TEXT UNIQUE NOT NULL,
                        password TEXT NOT NULL,
                        created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
                    );

                    CREATE TABLE IF NOT EXISTS conversations (
                        id SERIAL PRIMARY KEY,
                        user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
                        title TEXT NOT NULL DEFAULT 'New Chat',
                        created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
                        updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
                    );

                    CREATE TABLE IF NOT EXISTS messages (
                        id SERIAL PRIMARY KEY,
                        conv_id INTEGER REFERENCES conversations(id) ON DELETE CASCADE,
                        conversation_id INTEGER REFERENCES conversations(id) ON DELETE CASCADE,
                        role TEXT NOT NULL,
                        content TEXT NOT NULL,
                        meta JSONB,
                        turn INTEGER NOT NULL DEFAULT 1,
                        created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
                    );

                    CREATE INDEX IF NOT EXISTS idx_conversations_user ON conversations(user_id, updated_at DESC);
                    CREATE INDEX IF NOT EXISTS idx_messages_conv ON messages(conv_id, turn);
                """)
                cur.execute("""
                    INSERT INTO users (id, email, username, password)
                    VALUES (1, 'guest@saulgpt.local', 'Guest User', 'guest_mode_unrestricted')
                    ON CONFLICT (id) DO NOTHING
                """)
                conn.commit()
            conn.close()
            print("[Database] Supabase PostgreSQL connected and initialized.")
            return
        except Exception as e:
            print(f"[Database Warning] Supabase init failed: {e}. Falling back to SQLite.")

    # SQLite fallback
    conn = _get_sqlite_conn()
    try:
        conn.execute("PRAGMA journal_mode=WAL")
        conn.execute("PRAGMA busy_timeout=30000")
        conn.execute("PRAGMA foreign_keys=ON")
        conn.executescript("""
            CREATE TABLE IF NOT EXISTS users (
                id          INTEGER PRIMARY KEY AUTOINCREMENT,
                email       TEXT UNIQUE NOT NULL,
                username    TEXT UNIQUE NOT NULL,
                password    TEXT NOT NULL,
                created_at  TEXT NOT NULL DEFAULT (datetime('now'))
            );

            CREATE TABLE IF NOT EXISTS conversations (
                id          INTEGER PRIMARY KEY AUTOINCREMENT,
                user_id     INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
                title       TEXT NOT NULL DEFAULT 'New Chat',
                created_at  TEXT NOT NULL DEFAULT (datetime('now')),
                updated_at  TEXT NOT NULL DEFAULT (datetime('now'))
            );

            CREATE TABLE IF NOT EXISTS messages (
                id              INTEGER PRIMARY KEY AUTOINCREMENT,
                conversation_id INTEGER NOT NULL REFERENCES conversations(id) ON DELETE CASCADE,
                role            TEXT NOT NULL CHECK(role IN ('user','assistant')),
                content         TEXT NOT NULL,
                meta            TEXT,
                turn            INTEGER NOT NULL DEFAULT 0,
                created_at      TEXT NOT NULL DEFAULT (datetime('now'))
            );

            CREATE INDEX IF NOT EXISTS idx_conversations_user
                ON conversations(user_id, updated_at DESC);
            CREATE INDEX IF NOT EXISTS idx_messages_conv
                ON messages(conversation_id, turn);
        """)
        conn.execute("""
            INSERT OR IGNORE INTO users (id, email, username, password)
            VALUES (1, 'guest@saulgpt.local', 'Guest User', 'guest_mode_unrestricted')
        """)
        conn.commit()
        print("[Database] SQLite saulgpt.db initialized.")
    except Exception as e:
        print(f"[Database] SQLite init_db warning: {e}")
    finally:
        conn.close()


def ensure_user_exists(user_id: Optional[int] = None) -> int:
    """Ensures a user record exists so FOREIGN KEY constraints never fail."""
    if is_postgres():
        try:
            conn = _get_pg_conn()
            with conn.cursor() as cur:
                cur.execute("""
                    INSERT INTO users (id, email, username, password)
                    VALUES (1, 'guest@saulgpt.local', 'Guest User', 'guest_mode_unrestricted')
                    ON CONFLICT (id) DO NOTHING
                """)
                conn.commit()
                if user_id and isinstance(user_id, int) and user_id > 0:
                    cur.execute("SELECT id FROM users WHERE id = %s", (user_id,))
                    row = cur.fetchone()
                    if row:
                        conn.close()
                        return user_id
                    cur.execute("""
                        INSERT INTO users (id, email, username, password)
                        VALUES (%s, %s, %s, %s)
                        ON CONFLICT (id) DO NOTHING
                    """, (user_id, f"user_{user_id}@saulgpt.local", f"User {user_id}", "token_authenticated"))
                    conn.commit()
                    conn.close()
                    return user_id
            conn.close()
            return 1
        except Exception as e:
            print(f"[Database Postgres] ensure_user_exists warning: {e}")

    conn = _get_sqlite_conn()
    try:
        conn.execute("""
            INSERT OR IGNORE INTO users (id, email, username, password)
            VALUES (1, 'guest@saulgpt.local', 'Guest User', 'guest_mode_unrestricted')
        """)
        conn.commit()
        if user_id and isinstance(user_id, int) and user_id > 0:
            row = conn.execute("SELECT id FROM users WHERE id = ?", (user_id,)).fetchone()
            if row:
                return user_id
            try:
                conn.execute(
                    "INSERT INTO users (id, email, username, password) VALUES (?, ?, ?, ?)",
                    (user_id, f"user_{user_id}@saulgpt.local", f"User {user_id}", "token_authenticated")
                )
                conn.commit()
                return user_id
            except Exception:
                pass
        return 1
    except Exception as e:
        print(f"[Database SQLite] ensure_user_exists warning: {e}")
        return 1
    finally:
        conn.close()


def ensure_conversation_exists(conv_id: Optional[int], user_id: int = 1) -> int:
    """Ensures a conversation exists. Guarantees messages never hit FOREIGN KEY errors."""
    valid_uid = ensure_user_exists(user_id)
    if conv_id and isinstance(conv_id, int) and conv_id > 0:
        if is_postgres():
            try:
                conn = _get_pg_conn()
                with conn.cursor() as cur:
                    cur.execute("SELECT id FROM conversations WHERE id = %s", (conv_id,))
                    row = cur.fetchone()
                    if row:
                        conn.close()
                        return conv_id
                conn.close()
            except Exception as e:
                print(f"[Database Postgres] ensure_conversation_exists check error: {e}")
        else:
            conn = _get_sqlite_conn()
            try:
                row = conn.execute("SELECT id FROM conversations WHERE id = ?", (conv_id,)).fetchone()
                if row:
                    return conv_id
            except Exception as e:
                print(f"[Database SQLite] ensure_conversation_exists check error: {e}")
            finally:
                conn.close()

    return create_conversation(valid_uid, "New Chat")


# ─────────────────────────────────────────────────────────────
# USER OPERATIONS
# ─────────────────────────────────────────────────────────────

def create_user(email: str, username: str, password_hash: str) -> Optional[int]:
    base_username = (username or email.split("@")[0]).strip()
    candidate = base_username
    counter = 1

    if is_postgres():
        try:
            conn = _get_pg_conn()
            with conn.cursor() as cur:
                while True:
                    cur.execute("SELECT id FROM users WHERE username = %s AND email != %s", (candidate, email))
                    if not cur.fetchone():
                        break
                    candidate = f"{base_username}_{counter}"
                    counter += 1

                cur.execute(
                    "INSERT INTO users (email, username, password) VALUES (%s, %s, %s) RETURNING id",
                    (email.strip().lower(), candidate, password_hash)
                )
                new_id = cur.fetchone()["id"]
                conn.commit()
            conn.close()
            return new_id
        except Exception as e:
            print(f"[Database Postgres] create_user error: {e}")
            return None

    conn = _get_sqlite_conn()
    try:
        while True:
            existing = conn.execute("SELECT id FROM users WHERE username = ? AND email != ?", (candidate, email)).fetchone()
            if not existing:
                break
            candidate = f"{base_username}_{counter}"
            counter += 1

        cur = conn.execute(
            "INSERT INTO users (email, username, password) VALUES (?, ?, ?)",
            (email.strip().lower(), candidate, password_hash)
        )
        conn.commit()
        return cur.lastrowid
    except Exception as e:
        print(f"[Database SQLite] create_user error: {e}")
        return None
    finally:
        conn.close()


def get_user_by_email(email: str) -> Optional[Dict]:
    if is_postgres():
        try:
            conn = _get_pg_conn()
            with conn.cursor() as cur:
                cur.execute("SELECT * FROM users WHERE email = %s", (email.strip().lower(),))
                row = cur.fetchone()
            conn.close()
            return dict(row) if row else None
        except Exception as e:
            print(f"[Database Postgres] get_user_by_email error: {e}")
            return None

    conn = _get_sqlite_conn()
    try:
        row = conn.execute("SELECT * FROM users WHERE email = ?", (email.strip().lower(),)).fetchone()
        return dict(row) if row else None
    except Exception as e:
        print(f"[Database SQLite] get_user_by_email error: {e}")
        return None
    finally:
        conn.close()


def get_user_by_id(user_id: int) -> Optional[Dict]:
    if is_postgres():
        try:
            conn = _get_pg_conn()
            with conn.cursor() as cur:
                cur.execute("SELECT * FROM users WHERE id = %s", (user_id,))
                row = cur.fetchone()
            conn.close()
            return dict(row) if row else None
        except Exception as e:
            print(f"[Database Postgres] get_user_by_id error: {e}")
            return None

    conn = _get_sqlite_conn()
    try:
        row = conn.execute("SELECT * FROM users WHERE id = ?", (user_id,)).fetchone()
        return dict(row) if row else None
    except Exception as e:
        print(f"[Database SQLite] get_user_by_id error: {e}")
        return None
    finally:
        conn.close()


# ─────────────────────────────────────────────────────────────
# CONVERSATION OPERATIONS
# ─────────────────────────────────────────────────────────────

def create_conversation(user_id: int, title: str = "New Chat") -> int:
    valid_uid = ensure_user_exists(user_id)
    if is_postgres():
        try:
            conn = _get_pg_conn()
            with conn.cursor() as cur:
                cur.execute(
                    "INSERT INTO conversations (user_id, title) VALUES (%s, %s) RETURNING id",
                    (valid_uid, title)
                )
                conv_id = cur.fetchone()["id"]
                conn.commit()
            conn.close()
            return conv_id
        except Exception as e:
            print(f"[Database Postgres] create_conversation error: {e}")
            try:
                conn = _get_pg_conn()
                with conn.cursor() as cur:
                    cur.execute(
                        "INSERT INTO conversations (user_id, title) VALUES (1, %s) RETURNING id",
                        (title,)
                    )
                    conv_id = cur.fetchone()["id"]
                    conn.commit()
                conn.close()
                return conv_id
            except Exception as e2:
                print(f"[Database Postgres] fallback create_conversation error: {e2}")
                return 1

    conn = _get_sqlite_conn()
    try:
        cur = conn.execute(
            "INSERT INTO conversations (user_id, title) VALUES (?, ?)",
            (valid_uid, title)
        )
        conn.commit()
        return cur.lastrowid
    except Exception as e:
        print(f"[Database SQLite] create_conversation error: {e}")
        try:
            cur = conn.execute(
                "INSERT INTO conversations (user_id, title) VALUES (1, ?)",
                (title,)
            )
            conn.commit()
            return cur.lastrowid
        except Exception as e2:
            print(f"[Database SQLite] create_conversation fallback error: {e2}")
            return 1
    finally:
        conn.close()


def get_conversations(user_id: int, limit: int = 50, offset: int = 0) -> Dict:
    if is_postgres():
        try:
            conn = _get_pg_conn()
            with conn.cursor() as cur:
                cur.execute(
                    "SELECT id, title, created_at, updated_at FROM conversations "
                    "WHERE user_id = %s ORDER BY updated_at DESC LIMIT %s OFFSET %s",
                    (user_id, limit, offset)
                )
                rows = cur.fetchall()
                cur.execute("SELECT COUNT(*) as cnt FROM conversations WHERE user_id = %s", (user_id,))
                cnt_row = cur.fetchone()
                total = cnt_row["cnt"] if cnt_row else 0
            conn.close()
            return {"conversations": [dict(r) for r in rows], "total": total}
        except Exception as e:
            print(f"[Database Postgres] get_conversations error: {e}")
            return {"conversations": [], "total": 0}

    conn = _get_sqlite_conn()
    try:
        rows = conn.execute(
            "SELECT id, title, created_at, updated_at FROM conversations "
            "WHERE user_id = ? ORDER BY updated_at DESC LIMIT ? OFFSET ?",
            (user_id, limit, offset)
        ).fetchall()
        total_row = conn.execute(
            "SELECT COUNT(*) as cnt FROM conversations WHERE user_id = ?",
            (user_id,)
        ).fetchone()
        total = total_row["cnt"] if total_row else 0
        return {"conversations": [dict(r) for r in rows], "total": total}
    except Exception as e:
        print(f"[Database SQLite] get_conversations error: {e}")
        return {"conversations": [], "total": 0}
    finally:
        conn.close()


def get_conversation(conv_id: int, user_id: Optional[int] = None) -> Optional[Dict]:
    if is_postgres():
        try:
            conn = _get_pg_conn()
            with conn.cursor() as cur:
                if user_id:
                    cur.execute(
                        "SELECT id, title, created_at, updated_at FROM conversations "
                        "WHERE id = %s AND user_id = %s",
                        (conv_id, user_id)
                    )
                else:
                    cur.execute(
                        "SELECT id, title, created_at, updated_at FROM conversations WHERE id = %s",
                        (conv_id,)
                    )
                row = cur.fetchone()
            conn.close()
            return dict(row) if row else None
        except Exception as e:
            print(f"[Database Postgres] get_conversation error: {e}")
            return None

    conn = _get_sqlite_conn()
    try:
        if user_id:
            row = conn.execute(
                "SELECT id, title, created_at, updated_at FROM conversations "
                "WHERE id = ? AND user_id = ?",
                (conv_id, user_id)
            ).fetchone()
        else:
            row = conn.execute(
                "SELECT id, title, created_at, updated_at FROM conversations WHERE id = ?",
                (conv_id,)
            ).fetchone()
        return dict(row) if row else None
    except Exception as e:
        print(f"[Database SQLite] get_conversation error: {e}")
        return None
    finally:
        conn.close()


def update_conversation_title(conv_id: int, title: str):
    if is_postgres():
        try:
            conn = _get_pg_conn()
            with conn.cursor() as cur:
                cur.execute(
                    "UPDATE conversations SET title = %s, updated_at = NOW() WHERE id = %s",
                    (title, conv_id)
                )
                conn.commit()
            conn.close()
            return
        except Exception as e:
            print(f"[Database Postgres] update_conversation_title error: {e}")
            return

    conn = _get_sqlite_conn()
    try:
        conn.execute(
            "UPDATE conversations SET title = ?, updated_at = datetime('now') WHERE id = ?",
            (title, conv_id)
        )
        conn.commit()
    except Exception as e:
        print(f"[Database SQLite] update_conversation_title error: {e}")
    finally:
        conn.close()


def touch_conversation(conv_id: int):
    if is_postgres():
        try:
            conn = _get_pg_conn()
            with conn.cursor() as cur:
                cur.execute(
                    "UPDATE conversations SET updated_at = NOW() WHERE id = %s",
                    (conv_id,)
                )
                conn.commit()
            conn.close()
            return
        except Exception as e:
            print(f"[Database Postgres] touch_conversation error: {e}")
            return

    conn = _get_sqlite_conn()
    try:
        conn.execute(
            "UPDATE conversations SET updated_at = datetime('now') WHERE id = ?",
            (conv_id,)
        )
        conn.commit()
    except Exception as e:
        print(f"[Database SQLite] touch_conversation error: {e}")
    finally:
        conn.close()


def delete_conversation(conv_id: int, user_id: int) -> bool:
    if is_postgres():
        try:
            conn = _get_pg_conn()
            with conn.cursor() as cur:
                cur.execute(
                    "DELETE FROM conversations WHERE id = %s AND user_id = %s",
                    (conv_id, user_id)
                )
                deleted = cur.rowcount > 0
                conn.commit()
            conn.close()
            return deleted
        except Exception as e:
            print(f"[Database Postgres] delete_conversation error: {e}")
            return False

    conn = _get_sqlite_conn()
    try:
        cur = conn.execute(
            "DELETE FROM conversations WHERE id = ? AND user_id = ?",
            (conv_id, user_id)
        )
        conn.commit()
        return cur.rowcount > 0
    except Exception as e:
        print(f"[Database SQLite] delete_conversation error: {e}")
        return False
    finally:
        conn.close()


# ─────────────────────────────────────────────────────────────
# MESSAGE OPERATIONS
# ─────────────────────────────────────────────────────────────

def add_message(conv_id: int, role: str, content: str, meta: Any = None, turn: int = 0) -> Optional[int]:
    safe_conv_id = ensure_conversation_exists(conv_id)
    if is_postgres():
        try:
            conn = _get_pg_conn()
            with conn.cursor() as cur:
                pg_meta = None
                if meta is not None:
                    if isinstance(meta, str):
                        try:
                            pg_meta = Json(json.loads(meta))
                        except Exception:
                            pg_meta = Json({"raw": meta})
                    else:
                        pg_meta = Json(meta)
                cur.execute(
                    "INSERT INTO messages (conv_id, conversation_id, role, content, meta, turn) "
                    "VALUES (%s, %s, %s, %s, %s, %s) RETURNING id",
                    (safe_conv_id, safe_conv_id, role, content, pg_meta, turn)
                )
                msg_id = cur.fetchone()["id"]
                conn.commit()
            conn.close()
            return msg_id
        except Exception as e:
            print(f"[Database Postgres] add_message error: {e}")
            return None

    conn = _get_sqlite_conn()
    try:
        sql_meta = meta
        if sql_meta is not None and not isinstance(sql_meta, str):
            sql_meta = json.dumps(sql_meta)
        cur = conn.execute(
            "INSERT INTO messages (conversation_id, role, content, meta, turn) VALUES (?, ?, ?, ?, ?)",
            (safe_conv_id, role, content, sql_meta, turn)
        )
        conn.commit()
        return cur.lastrowid
    except Exception as e:
        print(f"[Database SQLite] add_message error: {e}")
        return None
    finally:
        conn.close()


def get_messages(conv_id: int) -> List[Dict]:
    if is_postgres():
        try:
            conn = _get_pg_conn()
            with conn.cursor() as cur:
                cur.execute(
                    "SELECT role, content, meta, turn, created_at FROM messages "
                    "WHERE (conv_id = %s OR conversation_id = %s) ORDER BY turn ASC, id ASC",
                    (conv_id, conv_id)
                )
                rows = cur.fetchall()
            conn.close()
            return [dict(r) for r in rows]
        except Exception as e:
            print(f"[Database Postgres] get_messages error: {e}")
            return []

    conn = _get_sqlite_conn()
    try:
        rows = conn.execute(
            "SELECT role, content, meta, turn, created_at FROM messages "
            "WHERE conversation_id = ? ORDER BY turn ASC",
            (conv_id,)
        ).fetchall()
        return [dict(r) for r in rows]
    except Exception as e:
        print(f"[Database SQLite] get_messages error: {e}")
        return []
    finally:
        conn.close()


def get_last_turn(conv_id: int) -> int:
    if is_postgres():
        try:
            conn = _get_pg_conn()
            with conn.cursor() as cur:
                cur.execute(
                    "SELECT COALESCE(MAX(turn), 0) as last_turn FROM messages "
                    "WHERE (conv_id = %s OR conversation_id = %s)",
                    (conv_id, conv_id)
                )
                row = cur.fetchone()
            conn.close()
            return row["last_turn"] if row else 0
        except Exception as e:
            print(f"[Database Postgres] get_last_turn error: {e}")
            return 0

    conn = _get_sqlite_conn()
    try:
        row = conn.execute(
            "SELECT COALESCE(MAX(turn), 0) as last_turn FROM messages WHERE conversation_id = ?",
            (conv_id,)
        ).fetchone()
        return row["last_turn"] if row else 0
    except Exception as e:
        print(f"[Database SQLite] get_last_turn error: {e}")
        return 0
    finally:
        conn.close()


def bulk_import_messages(conv_id: int, turns: list) -> bool:
    """Import an array of turn dicts into a conversation in one transaction."""
    safe_conv_id = ensure_conversation_exists(conv_id)
    if is_postgres():
        try:
            conn = _get_pg_conn()
            with conn.cursor() as cur:
                for i, t in enumerate(turns):
                    meta = t.get("meta")
                    pg_meta = None
                    if meta is not None:
                        if isinstance(meta, str):
                            try:
                                pg_meta = Json(json.loads(meta))
                            except Exception:
                                pg_meta = Json({"raw": meta})
                        else:
                            pg_meta = Json(meta)
                    cur.execute(
                        "INSERT INTO messages (conv_id, conversation_id, role, content, meta, turn) "
                        "VALUES (%s, %s, %s, %s, %s, %s)",
                        (safe_conv_id, safe_conv_id, t["role"], t["content"], pg_meta, i + 1)
                    )
                cur.execute("UPDATE conversations SET updated_at = NOW() WHERE id = %s", (safe_conv_id,))
                conn.commit()
            conn.close()
            return True
        except Exception as e:
            print(f"[Database Postgres] bulk_import_messages error: {e}")
            return False

    conn = _get_sqlite_conn()
    try:
        rows = []
        for i, t in enumerate(turns):
            meta = t.get("meta")
            if meta is not None and not isinstance(meta, str):
                meta = json.dumps(meta)
            rows.append((safe_conv_id, t["role"], t["content"], meta, i + 1))
        conn.executemany(
            "INSERT INTO messages (conversation_id, role, content, meta, turn) VALUES (?, ?, ?, ?, ?)",
            rows
        )
        conn.execute(
            "UPDATE conversations SET updated_at = datetime('now') WHERE id = ?",
            (safe_conv_id,)
        )
        conn.commit()
        return True
    except Exception as e:
        print(f"[Database SQLite] bulk_import_messages error: {e}")
        conn.rollback()
        return False
    finally:
        conn.close()