File size: 26,356 Bytes
1b4b6d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361e46e
1b4b6d2
 
 
361e46e
 
1b4b6d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7516417
 
 
1b4b6d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fef50e
f1477fe
2fef50e
1b4b6d2
 
 
 
 
 
 
 
 
 
fa156f3
1b4b6d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1477fe
 
1b4b6d2
 
 
 
 
 
f1477fe
 
 
 
 
 
1b4b6d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa156f3
 
 
 
 
 
 
 
 
1b4b6d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369b6c9
 
 
 
 
 
 
 
 
 
 
 
 
1b4b6d2
51a8001
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1477fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01d5a04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51a8001
1b4b6d2
 
 
 
 
f1477fe
2fef50e
f1477fe
 
 
 
 
1b4b6d2
2fef50e
 
 
 
 
1b4b6d2
f1477fe
 
 
 
1b4b6d2
 
 
7516417
1b4b6d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51a8001
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8b0ff8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7516417
 
1b4b6d2
 
 
 
 
 
 
 
 
 
 
7516417
1b4b6d2
 
 
 
 
 
 
 
 
 
15994ae
 
 
 
 
 
7516417
 
 
1b4b6d2
 
 
 
 
 
 
 
 
c8b0ff8
 
1b4b6d2
 
 
c8b0ff8
 
 
 
1b4b6d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361e46e
 
 
 
1b4b6d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361e46e
1b4b6d2
361e46e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc1ec7a
 
 
 
 
6e77399
cc1ec7a
 
 
 
 
6e77399
cc1ec7a
0f3ab13
cc1ec7a
 
0f3ab13
cc1ec7a
6e77399
cc1ec7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e77399
cc1ec7a
6e77399
 
cc1ec7a
 
 
6e77399
 
cc1ec7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
"""
وحدة اتصال قاعدة البيانات المركزية — Centralized DB Connection
================================================================
استخدام موحّد لقاعدة البيانات عبر جميع الملفات.
يضمن إغلاق الاتصال بأمان حتى عند حدوث خطأ (Context Manager).

الاستخدام:
    from utils.db import get_connection, safe_query, safe_execute

    # طريقة 1: Context manager (مُوصى به)
    with get_connection() as conn:
        with conn.cursor() as cur:
            cur.execute("SELECT * FROM qto_users")
            rows = cur.fetchall()

    # طريقة 2: استعلام آمن مع إرجاع DataFrame
    df = safe_query("SELECT id, email FROM qto_users WHERE role=%s", ("admin",))

    # طريقة 3: تنفيذ أمر كتابة آمن
    success, msg = safe_execute(
        "INSERT INTO qto_market_prices (item_name, unit, rate_aed) VALUES (%s, %s, %s)",
        ("Rebar 8mm", "ton", 2668)
    )
"""
import pandas as pd
from contextlib import contextmanager
from utils.settings import DbSettings
import sqlite3
import logging
import re
import os

logger = logging.getLogger("qto.db")

def is_sqlite() -> bool:
    try:
        settings = DbSettings.from_env()
        return not settings.host
    except Exception:
        return True

def translate_sql(sql: str) -> str:
    # 1. Parameter markers: %s -> ?
    sql = sql.replace("%s", "?")
    
    # 2. DATE_SUB(NOW(), INTERVAL 15 MINUTE) -> datetime('now', 'localtime', '-15 minutes')
    sql = re.sub(
        r"DATE_SUB\(\s*NOW\(\)\s*,\s*INTERVAL\s+(\d+)\s+MINUTE\)",
        r"datetime('now', 'localtime', '-\1 minutes')",
        sql,
        flags=re.IGNORECASE
    )
    
    # 3. NOW() - INTERVAL 30 DAY -> datetime('now', 'localtime', '-30 days')
    sql = re.sub(
        r"NOW\(\)\s*-\s*INTERVAL\s+(\d+)\s+DAY",
        r"datetime('now', 'localtime', '-\1 days')",
        sql,
        flags=re.IGNORECASE
    )
    
    # 4. UTC_TIMESTAMP() -> datetime('now')
    sql = re.sub(r"\bUTC_TIMESTAMP\(\)", "datetime('now')", sql, flags=re.IGNORECASE)
    
    # 5. NOW() -> datetime('now', 'localtime')
    sql = re.sub(r"\bNOW\(\)", "datetime('now', 'localtime')", sql, flags=re.IGNORECASE)
    
    return sql


class SQLiteCursorWrapper:
    def __init__(self, sqlite_cursor):
        self.cursor = sqlite_cursor

    def execute(self, sql, params=None):
        translated_sql = translate_sql(sql)
        if params is None:
            return self.cursor.execute(translated_sql)
        else:
            return self.cursor.execute(translated_sql, params)

    def executemany(self, sql, data_list):
        translated_sql = translate_sql(sql)
        return self.cursor.executemany(translated_sql, data_list)

    def fetchone(self):
        row = self.cursor.fetchone()
        if row is None:
            return None
        return dict(row)

    def fetchall(self):
        rows = self.cursor.fetchall()
        return [dict(r) for r in rows]

    @property
    def lastrowid(self):
        return self.cursor.lastrowid

    @property
    def description(self):
        return self.cursor.description

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.cursor.close()


class SQLiteConnectionWrapper:
    def __init__(self, sqlite_conn):
        self.conn = sqlite_conn
        self.conn.row_factory = sqlite3.Row

    def cursor(self):
        return SQLiteCursorWrapper(self.conn.cursor())

    def commit(self):
        self.conn.commit()

    def rollback(self):
        self.conn.rollback()

    def close(self):
        self.conn.close()

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        pass


def initialize_sqlite_db(conn):
    cur = conn.cursor()
    cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='qto_users'")
    if cur.fetchone():
        return
    
    # 1. qto_users
    cur.execute("""
    CREATE TABLE IF NOT EXISTS qto_users (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        email TEXT UNIQUE NOT NULL,
        password_hash TEXT,
        role TEXT DEFAULT 'user',
        email_verified INTEGER DEFAULT 0,
        reset_token_hash TEXT,
        reset_token_expires_at TEXT,
        verification_token_hash TEXT,
        last_login_at TEXT,
        google_id TEXT,
        microsoft_id TEXT,
        extra_projects_allowance INTEGER DEFAULT 0,
        token_version INTEGER DEFAULT 0,
        name TEXT,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    """)
    
    # 2. qto_subscriptions
    cur.execute("""
    CREATE TABLE IF NOT EXISTS qto_subscriptions (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        user_id INTEGER NOT NULL,
        feature TEXT DEFAULT 'qto',
        plan_tier INTEGER DEFAULT 0,
        provider TEXT DEFAULT 'manual',
        provider_customer_id TEXT,
        provider_subscription_id TEXT,
        status TEXT DEFAULT 'inactive',

        current_period_start TEXT,
        current_period_end TEXT,
        cancel_at_period_end INTEGER DEFAULT 0,
        projects_used INTEGER DEFAULT 0,
        ai_calls_used INTEGER DEFAULT 0,
        exports_used INTEGER DEFAULT 0,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    """)
    
    # 3. qto_projects
    cur.execute("""
    CREATE TABLE IF NOT EXISTS qto_projects (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        user_id INTEGER NOT NULL,
        name TEXT NOT NULL,
        date TEXT,
        boq_data TEXT,
        state_data TEXT,
        current_step INTEGER DEFAULT 1,
        status TEXT DEFAULT 'completed',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        UNIQUE(user_id, name)
    );
    """)
    cur.execute("PRAGMA table_info(qto_projects)")
    existing_proj_cols = {row[1] for row in cur.fetchall()}
    if "state_data" not in existing_proj_cols:
        cur.execute("ALTER TABLE qto_projects ADD COLUMN state_data TEXT;")
    if "current_step" not in existing_proj_cols:
        cur.execute("ALTER TABLE qto_projects ADD COLUMN current_step INTEGER DEFAULT 1;")
    
    # 4. qto_usage_logs
    cur.execute("""
    CREATE TABLE IF NOT EXISTS qto_usage_logs (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        user_id INTEGER,
        project_id INTEGER,
        event_type TEXT NOT NULL,
        quantity INTEGER DEFAULT 1,
        metadata TEXT,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    """)
    
    # 5. qto_files
    cur.execute("""
    CREATE TABLE IF NOT EXISTS qto_files (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        user_id INTEGER NOT NULL,
        project_id INTEGER,
        original_name TEXT NOT NULL,
        storage_provider TEXT DEFAULT 'local',
        storage_key TEXT NOT NULL,
        content_type TEXT,
        size_bytes INTEGER DEFAULT 0,
        checksum_sha256 TEXT,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    """)
    
    # 6. qto_invoices
    cur.execute("""
    CREATE TABLE IF NOT EXISTS qto_invoices (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        user_id INTEGER NOT NULL,
        subscription_id INTEGER,
        provider TEXT NOT NULL,
        provider_invoice_id TEXT,
        amount_aed REAL DEFAULT 0,
        currency TEXT DEFAULT 'AED',
        status TEXT DEFAULT 'draft',
        hosted_invoice_url TEXT,
        issued_at TEXT,
        paid_at TEXT,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    """)
    
    # 7. qto_audit_logs
    cur.execute("""
    CREATE TABLE IF NOT EXISTS qto_audit_logs (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        actor_user_id INTEGER,
        action TEXT NOT NULL,
        target_type TEXT,
        target_id TEXT,
        metadata TEXT,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    """)
    
    # 8. qto_deleted_accounts
    cur.execute("""
    CREATE TABLE IF NOT EXISTS qto_deleted_accounts (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        email TEXT NOT NULL,
        deleted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    """)
    
    # 8. qto_background_jobs
    cur.execute("""
    CREATE TABLE IF NOT EXISTS qto_background_jobs (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        user_id INTEGER,
        project_id INTEGER,
        job_type TEXT NOT NULL,
        status TEXT DEFAULT 'queued',
        payload TEXT,
        result TEXT,
        error TEXT,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        started_at TEXT,
        finished_at TEXT
    );
    """)
    
    # 9. qto_market_prices
    cur.execute("""
    CREATE TABLE IF NOT EXISTS qto_market_prices (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        item_name TEXT NOT NULL,
        unit TEXT,
        rate_aed REAL,
        last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    """)
    
    # 10. qto_memory_rules
    cur.execute("""
    CREATE TABLE IF NOT EXISTS qto_memory_rules (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        user_id INTEGER NULL,
        original_text TEXT NOT NULL,
        mapped_category TEXT NOT NULL,
        status TEXT DEFAULT 'pending',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    """)

    # 11. qto_agent_conversations
    cur.execute("""
    CREATE TABLE IF NOT EXISTS qto_agent_conversations (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        user_id INTEGER NOT NULL,
        agent_role TEXT NOT NULL,
        sender TEXT NOT NULL,
        message TEXT NOT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    """)

    # 12. qto_customer_complaints
    cur.execute("""
    CREATE TABLE IF NOT EXISTS qto_customer_complaints (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        user_id INTEGER NOT NULL,
        complaint_text TEXT NOT NULL,
        status TEXT DEFAULT 'open',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    """)

    # 13. qto_project_feedback
    cur.execute("""
    CREATE TABLE IF NOT EXISTS qto_project_feedback (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        user_id INTEGER NOT NULL,
        tool_name TEXT NOT NULL,
        project_name TEXT NOT NULL,
        rating INTEGER NOT NULL,
        reason TEXT,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    """)
    
    # 14. qto_inquiries
    cur.execute("""
    CREATE TABLE IF NOT EXISTS qto_inquiries (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        user_id INTEGER,
        name TEXT,
        email TEXT NOT NULL,
        subject TEXT NOT NULL,
        message TEXT NOT NULL,
        category TEXT DEFAULT 'general',
        status TEXT DEFAULT 'new',
        admin_notes TEXT,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    """)

    # 15. qto_reviews
    cur.execute("""
    CREATE TABLE IF NOT EXISTS qto_reviews (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        user_id INTEGER,
        user_name TEXT NOT NULL,
        user_role TEXT,
        company TEXT,
        rating INTEGER NOT NULL,
        review_title TEXT,
        review_text TEXT NOT NULL,
        is_approved INTEGER DEFAULT 0,
        is_featured INTEGER DEFAULT 0,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    """)

    # 16. qto_active_projects
    cur.execute("""
    CREATE TABLE IF NOT EXISTS qto_active_projects (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        user_id INTEGER NOT NULL,
        project_id INTEGER NOT NULL,
        current_step INTEGER DEFAULT 1,
        state_data TEXT,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        UNIQUE(user_id, project_id)
    );
    """)

    # 17. qto_visitor_logs
    cur.execute("""
    CREATE TABLE IF NOT EXISTS qto_visitor_logs (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        ip_address TEXT,
        path TEXT,
        user_agent TEXT,
        referer TEXT,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    """)

    # 18. qto_boq_revisions (BOQ Versioning: Rev 0, Rev 1, Rev 2...)
    cur.execute("""
    CREATE TABLE IF NOT EXISTS qto_boq_revisions (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        project_id INTEGER NOT NULL,
        user_id INTEGER NOT NULL,
        rev_no INTEGER NOT NULL DEFAULT 0,
        label TEXT DEFAULT 'Calculation',
        item_count INTEGER DEFAULT 0,
        grand_total REAL DEFAULT 0,
        boq_json TEXT,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    """)
    
    # Seed admin user if none exists
    cur.execute("SELECT COUNT(*) FROM qto_users")
    count = cur.fetchone()[0]
    if count == 0:
        import bcrypt
        import secrets as _secrets
        admin_emails = ["basel00omar.92@gmail.com", "basel.omar.qs@gmail.com", "admin@qto.com"]
        admin_password = os.environ.get("QTO_ADMIN_PASSWORD")
        generated_pw = False
        if not admin_password:
            admin_password = _secrets.token_urlsafe(16)
            generated_pw = True
        hashed = bcrypt.hashpw(admin_password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
        for em in admin_emails:
            cur.execute(
                "INSERT INTO qto_users (email, password_hash, role) VALUES (?, ?, ?)",
                (em, hashed, "admin")
            )
        conn.commit()
        if generated_pw:
            print("Local SQLite database seeded with admin users (set QTO_ADMIN_PASSWORD or use reset flow).")
        else:
            print("Local SQLite database seeded with admin users.")
        
        # Seed default prices
        try:
            from utils.market_prices_logic import DEFAULT_MATERIALS
            for m in DEFAULT_MATERIALS:
                for em in ["dubai", "abudhabi", "sharjah", "ajman"]:
                    rate = m[em]
                    em_name = "Abu Dhabi" if em == "abudhabi" else em.capitalize()
                    item_name = f"{m['name_en']} ({em_name})"
                    cur.execute(
                        "INSERT INTO qto_market_prices (item_name, unit, rate_aed) VALUES (?, ?, ?)",
                        (item_name, m["unit"], rate)
                    )
            conn.commit()
            print("Local SQLite database seeded with default market prices.")
        except Exception as ex:
            print(f"Could not seed market prices: {ex}")


MOCKUP_REVIEWS = [
    {
        "user_name": "Eng. Tariq Al-Mansoor",
        "user_role": "Senior Quantity Surveyor",
        "company": "Al-Mansoor Engineering Consultants, Dubai",
        "rating": 5,
        "review_title": "Saved 3 days of manual takeoff on our first villa",
        "review_text": "أداة استثنائية لحصر كميات الخرسانة والمباني والتشطيبات بدقة عالية ومقارنتها بأسعار السوق الإماراتي. تصدير الـ BOQ إلى Excel منسق جاهز للاستخدام مباشرة وفر علي وفريق العمل ساعات طويلة.",
        "is_approved": 1,
        "is_featured": 1
    },
    {
        "user_name": "Eng. Sarah Khalil",
        "user_role": "Commercial & Tendering Manager",
        "company": "Apex Contracting LLC, Abu Dhabi",
        "rating": 5,
        "review_title": "Seamless integration between Takeoff and Work Programme",
        "review_text": "The automatic derivation of cash flow and Gantt charts from the extracted BOQ quantities is a game changer for estimating and planning tenders in the UAE construction market.",
        "is_approved": 1,
        "is_featured": 1
    },
    {
        "user_name": "Eng. Mohammed Al-Hashimi",
        "user_role": "Project Director",
        "company": "Dar Al-Binaa Contracting, Sharjah",
        "rating": 5,
        "review_title": "دقة قراءة المخططات الإنشائية والمعمارية",
        "review_text": "جربت عدة أدوات ولكن THE QS HUB متوافق تماماً مع مخططات بلديات دبي والشارقة وعجمان. التعرف على جداول القواعد والميدات والأعمدة تم باحترافية عالية جداً.",
        "is_approved": 1,
        "is_featured": 1
    },
    {
        "user_name": "Eng. Omar Farooq",
        "user_role": "Cost Estimator & QS",
        "company": "Modern Build Contracting, Ajman",
        "rating": 5,
        "review_title": "Game changer for contractor tender submissions",
        "review_text": "Speeding up our tender submissions by over 70%. Being able to customize the unit rates based on live UAE market prices gives our management immense confidence.",
        "is_approved": 1,
        "is_featured": 1
    },
    {
        "user_name": "Eng. Mahmoud Youssef",
        "user_role": "Civil & Estimation Engineer",
        "company": "Falcon Construction Group, Ras Al Khaimah",
        "rating": 5,
        "review_title": "واجهة ممتازة ودعم فني متميز وسريع",
        "review_text": "المنصة سهلة الاستخدام جداً والمساعد الذكي سارة يساعد في أي استفسار هندسي فوراً. حصر كل عناصر الفيلا مع حماية البنود من السهو والنسيان نقل عملنا لمستوى آخر.",
        "is_approved": 1,
        "is_featured": 1
    }
]


def seed_mockup_reviews():
    """Seed pre-approved mockup reviews if qto_reviews table is empty."""
    try:
        df = safe_query("SELECT COUNT(*) as cnt FROM qto_reviews")
        if not df.empty and int(df.iloc[0]["cnt"]) > 0:
            return
        for r in MOCKUP_REVIEWS:
            safe_execute(
                """
                INSERT INTO qto_reviews (user_name, user_role, company, rating, review_title, review_text, is_approved, is_featured)
                VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
                """,
                (r["user_name"], r["user_role"], r["company"], r["rating"], r["review_title"], r["review_text"], r["is_approved"], r["is_featured"])
            )
        logger.info("Mockup reviews seeded successfully.")
    except Exception as e:
        logger.warning(f"Could not seed mockup reviews: {e}")



_POOL = None

def get_pool():
    global _POOL
    if _POOL is None:
        import pymysql
        from dbutils.pooled_db import PooledDB
        kwargs = DbSettings.from_env().pymysql_kwargs()
        _POOL = PooledDB(
            creator=pymysql,
            maxconnections=15,
            mincached=2,
            maxcached=5,
            blocking=True,
            ping=1,
            **kwargs
        )
    return _POOL

_SQLITE_INITIALIZED = False

@contextmanager
def get_connection():
    """
    Context manager for safe database connections.
    Guarantees connection is closed even if an error occurs.

    Usage:
        with get_connection() as conn:
            with conn.cursor() as cur:
                cur.execute(...)
    """
    global _SQLITE_INITIALIZED
    # SQLite Fallback check
    try:
        settings = DbSettings.from_env()
        use_sqlite = not settings.host
    except Exception:
        use_sqlite = True

    if use_sqlite:
        import sqlite3
        db_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "qto_local.db")
        conn = sqlite3.connect(db_path, timeout=60.0, check_same_thread=False)
        try:
            conn.execute("PRAGMA journal_mode=WAL;")
            conn.execute("PRAGMA synchronous=NORMAL;")
        except Exception:
            pass
        if not _SQLITE_INITIALIZED:
            initialize_sqlite_db(conn)
            _SQLITE_INITIALIZED = True
        wrapper = SQLiteConnectionWrapper(conn)
        try:
            yield wrapper
        finally:
            try:
                conn.close()
            except Exception:
                pass
    else:
        pool = get_pool()
        conn = pool.connection()
        try:
            yield conn
        finally:
            try:
                conn.close()
            except Exception:
                pass


def safe_query(sql: str, params=None) -> pd.DataFrame:
    """
    Executes a SELECT query safely and returns results as a pandas DataFrame.
    Always uses parameterized queries to prevent SQL injection.

    Args:
        sql: SQL query string with %s placeholders
        params: tuple of parameters to bind

    Returns:
        pd.DataFrame of results, or empty DataFrame on error
    """
    try:
        with get_connection() as conn:
            with conn.cursor() as cur:
                cur.execute(sql, params or ())
                rows = cur.fetchall()
        return pd.DataFrame(rows) if rows else pd.DataFrame()
    except Exception as e:
        # Log the real error instead of silently returning "no rows" — an empty
        # DataFrame from a transient failure has caused bugs where users looked
        # unsubscribed / access was wrongly denied.
        logger.error("safe_query failed: %s | sql=%s", e, sql.strip().split("\n")[0][:200])
        return pd.DataFrame()


def safe_execute(sql: str, params=None, many=False, data_list=None) -> tuple:
    """
    Executes an INSERT/UPDATE/DELETE query safely with auto-commit.
    Always uses parameterized queries to prevent SQL injection.

    Args:
        sql: SQL statement with %s placeholders
        params: tuple of parameters (for single execution)
        many: if True, uses executemany with data_list
        data_list: list of tuples for batch execution

    Returns:
        (True, "OK") on success, (False, error_message) on failure
    """
    try:
        with get_connection() as conn:
            with conn.cursor() as cur:
                if many and data_list:
                    cur.executemany(sql, data_list)
                else:
                    cur.execute(sql, params or ())
            conn.commit()
        return True, "OK"
    except Exception as e:
        logger.error("safe_execute failed: %s | sql=%s", e, sql.strip().split("\n")[0][:200])
        return False, str(e)


@contextmanager
def transaction():
    """Run several writes atomically — commits on success, rolls back on error.

    Usage:
        with transaction() as cur:
            cur.execute("UPDATE ...", (...))
            cur.execute("INSERT ...", (...))
    Either all statements land or none do.
    """
    with get_connection() as conn:
        cur = conn.cursor()
        try:
            yield cur
            conn.commit()
        except Exception:
            try:
                conn.rollback()
            except Exception:
                pass
            logger.exception("transaction rolled back")
            raise
        finally:
            try:
                cur.close()
            except Exception:
                pass


# ---------------------------------------------------------------------------
# Centralised active-project state writer
# ---------------------------------------------------------------------------
_MAX_STATE_BYTES = 4 * 1024 * 1024  # 4 MB hard cap (TiDB Cloud JSON ~6 MB)


def _trim_state(state_data: dict) -> dict:
    """Aggressively trim heavy fields so state_json fits in the DB column."""
    import copy
    sd = copy.copy(state_data)

    # Truncate per-page text arrays to 4000 chars (preserves full title block & drawing text)
    for key in ("str_texts", "arch_texts"):
        if key in sd and isinstance(sd[key], list):
            sd[key] = [(t[:4000] if isinstance(t, str) else "") for t in sd[key]]

    # Strip classified_pages to absolute minimum
    if "classified_pages" in sd and isinstance(sd["classified_pages"], list):
        clean = []
        for p in sd["classified_pages"]:
            clean.append({
                "pdf": p.get("pdf", "structural"),
                "page_index": p.get("page_index", 0),
                "page_num": p.get("page_num", 1),
                "detected_type": p.get("detected_type", "unknown"),
                "confidence": p.get("confidence", "low"),
                "items": p.get("items", []),
            })
        sd["classified_pages"] = clean

    return sd


def upsert_active_state(user_id: int, project_id: int, step: int,
                         state_data: dict) -> tuple:
    """
    Single entry-point for writing to qto_active_projects.
    Returns (True, "OK") on success, (False, error_msg) on failure.
    """
    import json as _json

    trimmed = _trim_state(state_data)
    state_json = _json.dumps(trimmed, ensure_ascii=False, default=str)

    # Progressive key-dropping until we fit under the limit
    for drop_key in ("str_texts", "arch_texts", "classified_pages",
                     "extraction_results", "confirmed_auto_data",
                     "boq_items", "boq_meta"):
        if len(state_json.encode("utf-8")) <= _MAX_STATE_BYTES:
            break
        if drop_key in trimmed:
            logger.warning("upsert_active_state: dropping '%s' (%d bytes > %d limit)",
                           drop_key, len(state_json.encode("utf-8")), _MAX_STATE_BYTES)
            trimmed[drop_key] = [] if isinstance(trimmed.get(drop_key), list) else {}
            state_json = _json.dumps(trimmed, ensure_ascii=False, default=str)

    byte_size = len(state_json.encode("utf-8"))
    logger.info("upsert_active_state: project=%s step=%s size=%d bytes", project_id, step, byte_size)

    try:
        df = safe_query(
            "SELECT id FROM qto_active_projects WHERE user_id=%s AND project_id=%s",
            (user_id, project_id),
        )
        if df.empty:
            return safe_execute(
                "INSERT INTO qto_active_projects (user_id, project_id, current_step, state_data) "
                "VALUES (%s, %s, %s, %s)",
                (user_id, project_id, step, state_json),
            )
        else:
            return safe_execute(
                "UPDATE qto_active_projects SET current_step=%s, state_data=%s "
                "WHERE user_id=%s AND project_id=%s",
                (step, state_json, user_id, project_id),
            )
    except Exception as e:
        logger.error("upsert_active_state FAILED: %s", e)
        return False, str(e)