File size: 30,018 Bytes
1dc84d4
540c3fc
1dc84d4
540c3fc
 
 
 
9f8e288
ccbfc8e
 
1dc84d4
ccbfc8e
 
3f86912
ccbfc8e
1dc84d4
 
9337b76
 
 
ffd6cda
c83774d
ffd6cda
c15c644
1dc84d4
c15c644
9337b76
1dc84d4
 
 
 
9337b76
3d25258
 
1dc84d4
 
 
 
 
9337b76
c83774d
3d25258
9337b76
1dc84d4
9643373
9337b76
1dc84d4
 
9337b76
1dc84d4
3f86912
ffd6cda
1dc84d4
 
9337b76
1dc84d4
 
9337b76
1dc84d4
9337b76
3d25258
 
1dc84d4
 
6ef20f2
1dc84d4
6ef20f2
c83774d
1dc84d4
 
 
ccbfc8e
 
3d25258
 
 
 
c83774d
3d25258
 
 
ccbfc8e
3d25258
ccbfc8e
9337b76
c83774d
25e63e1
1dc84d4
c83774d
 
 
 
c15c644
9e7ded1
 
 
9f8e288
9e7ded1
 
 
 
c83774d
1dc84d4
c83774d
3d25258
c83774d
 
1dc84d4
9e7ded1
c83774d
1dc84d4
 
c83774d
 
9643373
1dc84d4
2c5e6a5
1dc84d4
3d25258
c83774d
 
 
1dc84d4
c83774d
ffd6cda
c83774d
 
ccbfc8e
25e63e1
9e7ded1
 
 
 
 
 
 
 
 
 
 
c83774d
ccbfc8e
c83774d
 
9e7ded1
 
c83774d
1dc84d4
c83774d
 
ccbfc8e
c83774d
 
ccbfc8e
c83774d
ccbfc8e
3d25258
c83774d
 
ccbfc8e
c83774d
ccbfc8e
c83774d
1dc84d4
 
 
c83774d
1dc84d4
ae97bd4
 
c83774d
 
ae97bd4
c83774d
add7275
 
 
 
 
2c5e6a5
add7275
 
c83774d
ae97bd4
ccbfc8e
 
3d25258
ccbfc8e
c83774d
 
 
2c5e6a5
c83774d
add7275
c83774d
 
 
 
 
 
ffd6cda
 
ccbfc8e
c83774d
ae97bd4
c83774d
ccbfc8e
 
 
 
 
2c5e6a5
ccbfc8e
add7275
c83774d
ccbfc8e
c83774d
 
 
ccbfc8e
c83774d
1dc84d4
 
 
9e7ded1
1dc84d4
 
c83774d
 
 
 
 
 
 
 
 
1dc84d4
 
540c3fc
1dc84d4
3d25258
add7275
540c3fc
 
 
 
 
 
 
c83774d
 
540c3fc
ccbfc8e
540c3fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e7ded1
540c3fc
9e7ded1
540c3fc
 
 
 
 
 
3f86912
9e7ded1
540c3fc
 
 
9e7ded1
1dc84d4
add7275
 
540c3fc
add7275
c83774d
9e7ded1
 
 
 
c83774d
 
2c5e6a5
c83774d
add7275
 
c83774d
 
 
ccbfc8e
2c5e6a5
add7275
2c5e6a5
 
 
 
540c3fc
2c5e6a5
540c3fc
 
 
 
 
2c5e6a5
9f8e288
2c5e6a5
c15c644
add7275
 
2c5e6a5
add7275
c83774d
add7275
2c5e6a5
9f8e288
2c5e6a5
 
ccbfc8e
ffd6cda
c83774d
add7275
c15c644
2c5e6a5
 
 
 
 
 
 
c15c644
2c5e6a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c83774d
2c5e6a5
add7275
c83774d
9f8e288
2c5e6a5
add7275
c83774d
c601c21
c83774d
b1fa9bd
c83774d
 
c15c644
c83774d
 
 
 
 
 
c15c644
c83774d
 
 
 
 
9f8e288
c83774d
9f8e288
c83774d
9f8e288
c83774d
 
ae97bd4
9f8e288
c83774d
c601c21
1dc84d4
540c3fc
1dc84d4
3d25258
9e7ded1
 
 
 
3f86912
c15c644
add7275
 
c15c644
 
9f8e288
c15c644
 
540c3fc
c15c644
add7275
9f8e288
540c3fc
9e7ded1
c15c644
9e7ded1
 
c15c644
 
add7275
9f8e288
540c3fc
 
9e7ded1
c83774d
 
 
 
9e7ded1
c83774d
 
540c3fc
9e7ded1
 
540c3fc
1c5a346
9f8e288
 
 
 
 
 
 
 
 
540c3fc
9f8e288
 
 
 
 
 
 
 
 
 
 
 
 
c15c644
 
 
 
 
cb599e6
add7275
 
 
c15c644
 
 
 
cb599e6
add7275
c15c644
 
 
 
 
 
 
705db9f
c15c644
 
 
 
486c74d
 
c15c644
 
 
 
486c74d
c15c644
 
b1fa9bd
c15c644
 
 
add7275
3f86912
540c3fc
 
9e7ded1
c15c644
69406fb
2c5e6a5
c15c644
 
540c3fc
c15c644
 
 
 
2c5e6a5
add7275
540c3fc
 
 
9f8e288
 
2c5e6a5
540c3fc
 
 
2c5e6a5
540c3fc
9f8e288
add7275
540c3fc
2c5e6a5
add7275
2c5e6a5
3f86912
c15c644
9e7ded1
 
 
 
 
 
3f86912
c15c644
69406fb
c15c644
 
 
 
 
add7275
9f8e288
 
c15c644
9f8e288
 
 
 
 
c15c644
9f8e288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c15c644
 
 
 
 
3f86912
1dc84d4
 
c83774d
1dc84d4
3d25258
1dc84d4
 
c83774d
1dc84d4
 
c83774d
c15c644
540c3fc
1dc84d4
ffd6cda
c83774d
 
 
 
 
 
c15c644
c83774d
add7275
486c74d
c15c644
486c74d
 
 
add7275
 
 
c15c644
add7275
9e7ded1
c15c644
 
2c5e6a5
c15c644
9e7ded1
c83774d
69406fb
add7275
c15c644
 
 
 
 
486c74d
c15c644
c83774d
 
 
3f86912
9e7ded1
c83774d
 
 
add7275
c15c644
 
 
 
646ce38
c15c644
 
 
 
 
 
 
 
cb599e6
c15c644
add7275
cb599e6
 
 
c15c644
 
 
cb599e6
2c5e6a5
cb599e6
c15c644
 
 
 
add7275
 
 
 
646ce38
add7275
c15c644
cb599e6
add7275
cb599e6
c15c644
 
 
 
 
 
 
c83774d
1dc84d4
 
9f8e288
 
 
 
1dc84d4
c83774d
9f8e288
 
c83774d
9f8e288
 
c83774d
 
 
 
add7275
 
9f8e288
 
9e7ded1
9f8e288
 
9e7ded1
9f8e288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e7ded1
9f8e288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ce11bc
1dc84d4
 
9f8e288
 
540c3fc
9f8e288
1dc84d4
c83774d
ccbfc8e
9f8e288
c83774d
9f8e288
 
c83774d
ccbfc8e
9e7ded1
add7275
9e7ded1
 
add7275
9f8e288
 
9e7ded1
c83774d
9f8e288
 
 
 
 
 
 
 
 
 
 
 
 
540c3fc
c15c644
9f8e288
 
 
 
 
 
 
 
 
c15c644
 
 
 
9f8e288
c83774d
9f8e288
 
 
 
 
 
 
 
 
 
 
ffd6cda
9643373
 
 
c83774d
9643373
9f8e288
 
add7275
 
9643373
 
 
 
c83774d
3d25258
 
 
add7275
3d25258
ffd6cda
ccbfc8e
add7275
 
ccbfc8e
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
"""
main.py — Pricelyst Shopping Advisor (Jessica Edition 2026 - Upgrade v3.1)

✅ Feature: "Vernacular Engine" (Shona/Ndebele/English Input -> Native Response).
✅ Feature: "Precision Search" (Prioritizes exact phrase matches over popularity).
✅ Feature: "Concept Exploder" (Event Planning -> Shopping List).
✅ UI/UX: "Nearest Match" phrasing for substitutions.
✅ Core: Deep Vector Search + Market Matrix + Store Preferences.

ENV VARS:
- GOOGLE_API_KEY=...
- FIREBASE='{"type":"service_account", ...}'
- PRICE_API_BASE=https://api.pricelyst.co.zw
- GEMINI_MODEL=gemini-2.5-flash
- PORT=5000
"""

import os
import re
import json
import time
import math
import logging
import base64
from datetime import datetime, timezone
from typing import Any, Dict, List, Optional, Tuple

import requests
import pandas as pd
from flask import Flask, request, jsonify
from flask_cors import CORS

# ––––– Logging –––––

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s | %(levelname)s | %(message)s"
)
logger = logging.getLogger("pricelyst-advisor")

# ––––– Gemini SDK –––––

try:
    from google import genai
    from google.genai import types
except Exception as e:
    genai = None
    logger.error("google-genai not installed. pip install google-genai. Error=%s", e)

GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY", "")
GEMINI_MODEL = os.environ.get("GEMINI_MODEL", "gemini-2.5-flash")

_gemini_client = None
if genai and GOOGLE_API_KEY:
    try:
        _gemini_client = genai.Client(api_key=GOOGLE_API_KEY)
        logger.info("Gemini client ready (model=%s).", GEMINI_MODEL)
    except Exception as e:
        logger.error("Failed to init Gemini client: %s", e)

# ––––– Firebase Admin –––––

import firebase_admin
from firebase_admin import credentials, firestore

FIREBASE_ENV = os.environ.get("FIREBASE", "")

def init_firestore_from_env() -> Optional[firestore.Client]:
    if firebase_admin._apps:
        return firestore.client()
    if not FIREBASE_ENV:
        logger.warning("FIREBASE env var missing. Persistence disabled.")
        return None
    try:
        sa_info = json.loads(FIREBASE_ENV)
        cred = credentials.Certificate(sa_info)
        firebase_admin.initialize_app(cred)
        logger.info("Firebase initialized.")
        return firestore.client()
    except Exception as e:
        logger.critical("Failed to initialize Firebase: %s", e)
        return None

db = init_firestore_from_env()

# ––––– External API –––––

PRICE_API_BASE = os.environ.get("PRICE_API_BASE", "https://api.pricelyst.co.zw").rstrip("/")
HTTP_TIMEOUT = 30

# ––––– Static Data (Zim Context) –––––

ZIM_CONTEXT = {
    "fuel_petrol": 1.58,
    "fuel_diesel": 1.65,
    "gas_lpg": 2.00,
    "bread_avg": 1.10,
    "zesa_step_1": {"limit": 50, "rate": 0.04},
    "zesa_step_2": {"limit": 150, "rate": 0.09},
    "zesa_step_3": {"limit": 9999, "rate": 0.14},
    "zesa_levy": 0.06
}

# ––––– Cache –––––

PRODUCT_CACHE_TTL = 60 * 20 # 20 mins
_data_cache: Dict[str, Any] = {
    "ts": 0,
    "df": pd.DataFrame(),
    "raw_count": 0
}

app = Flask(__name__)
CORS(app)

# =========================
# 1. ETL Layer (Deep Search Indexing)
# =========================

def _norm(s: Any) -> str:
    if not s: return ""
    return str(s).strip().lower()

def _coerce_price(v: Any) -> float:
    try:
        return float(v) if v is not None else 0.0
    except:
        return 0.0

def _safe_json_loads(s: str, fallback: Any):
    try:
        if "```json" in s:
            s = s.split("```json")[1].split("```")[0]
        elif "```" in s:
            s = s.split("```")[0]
        return json.loads(s)
    except Exception as e:
        logger.error(f"JSON Parse Error: {e}")
        return fallback

def fetch_and_flatten_data() -> pd.DataFrame:
    all_products = []
    page = 1
    
    logger.info("ETL: Starting fetch from /api/v1/product-listing")
    
    while True:
        try:
            url = f"{PRICE_API_BASE}/api/v1/product-listing"
            r = requests.get(url, params={"page": page, "perPage": 50}, timeout=HTTP_TIMEOUT)
            r.raise_for_status()
            payload = r.json()
            data = payload.get("data") or []
            if not data: break
            
            all_products.extend(data)
            
            meta = payload
            if page >= (meta.get("totalPages") or 99):
                break
            page += 1
        except Exception as e:
            logger.error(f"ETL Error on page {page}: {e}")
            break

    rows = []
    for p in all_products:
        try:
            p_id = int(p.get("id") or 0)
            p_name = str(p.get("name") or "Unknown")
            
            brand_obj = p.get("brand") or {}
            brand_name = str(brand_obj.get("brand_name") or "")
            
            cats = p.get("categories") or []
            cat_names = [str(c.get("name") or "") for c in cats]
            cat_str = " ".join(cat_names)
            primary_cat = cat_names[0] if cat_names else "General"
            
            # Deep Search Vector
            search_vector = _norm(f"{p_name} {brand_name} {cat_str}")
            
            views = int(p.get("view_count") or 0)
            image = str(p.get("thumbnail") or p.get("image") or "")

            prices = p.get("prices") or []
            
            if not prices:
                rows.append({
                    "product_id": p_id,
                    "product_name": p_name,
                    "search_vector": search_vector,
                    "brand": brand_name,
                    "category": primary_cat,
                    "retailer": "Listing",
                    "price": 0.0,
                    "views": views,
                    "image": image,
                    "is_offer": False
                })
                continue

            for offer in prices:
                retailer = offer.get("retailer") or {}
                r_name = str(retailer.get("name") or "Unknown Store")
                price_val = _coerce_price(offer.get("price"))
                
                if price_val > 0:
                    rows.append({
                        "product_id": p_id,
                        "product_name": p_name,
                        "search_vector": search_vector,
                        "brand": brand_name,
                        "category": primary_cat,
                        "retailer": r_name,
                        "price": price_val,
                        "views": views,
                        "image": image,
                        "is_offer": True
                    })
        except:
            continue

    df = pd.DataFrame(rows)
    logger.info(f"ETL: Flattened into {len(df)} rows.")
    return df

def get_market_index(force_refresh: bool = False) -> pd.DataFrame:
    global _data_cache
    if force_refresh or _data_cache["df"].empty or (time.time() - _data_cache["ts"] > PRODUCT_CACHE_TTL):
        logger.info("ETL: Refreshing Market Index...")
        df = fetch_and_flatten_data()
        _data_cache["df"] = df
        _data_cache["ts"] = time.time()
        _data_cache["raw_count"] = len(df)
    return _data_cache["df"]

# =========================
# 2. Analyst Engine (Precision Search & Matrix)
# =========================

def search_products_deep(df: pd.DataFrame, query: str, limit: int = 15) -> pd.DataFrame:
    """
    Precision Search Algorithm.
    Prioritizes:
    1. Exact sequential match in Name/Vector (Highest Score)
    2. Token overlap (Medium Score)
    3. Views/Popularity (Tie-breaker)
    """
    if df.empty or not query: return df
    q_norm = _norm(query)
    q_tokens = set(q_norm.split())
    
    def scoring_algo(row):
        score = 0
        vector = row['search_vector']
        
        # 1. Exact Name Match (Highest)
        if q_norm == _norm(row['product_name']):
            score += 1000
            
        # 2. Sequential Vector Match (High)
        if q_norm in vector:
            score += 500
            
        # 3. Brand Match
        if row['brand'].lower() in q_norm:
            score += 200
            
        # 4. Token Overlap
        text_tokens = set(vector.split())
        overlap = len(q_tokens.intersection(text_tokens))
        score += (overlap * 50)
        
        return score

    df_scored = df.copy()
    df_scored['match_score'] = df_scored.apply(scoring_algo, axis=1)
    
    # Filter out zero matches
    matches = df_scored[df_scored['match_score'] > 0]
    
    if matches.empty: return matches

    # Sort: Match Score (Desc) -> Views (Desc) -> Price (Asc)
    matches = matches.sort_values(by=['match_score', 'views', 'price'], ascending=[False, False, True])
    
    return matches.head(limit)

def calculate_basket_optimization(item_names: List[str], preferred_retailer: str = None) -> Dict[str, Any]:
    """
    Generates a FULL MARKET MATRIX with Precision Search.
    """
    df = get_market_index()
    if df.empty: 
        return {"actionable": False, "error": "No data"}

    found_items = [] 
    missing_global = []

    # 1. Resolve Items & Check Brand Fidelity
    for item in item_names:
        hits = search_products_deep(df[df['is_offer']==True], item, limit=10)
        
        if hits.empty:
            missing_global.append(item)
            continue
        
        best_match = hits.iloc[0]
        
        # --- Brand Fidelity Check ---
        q_norm = _norm(item)
        res_norm = _norm(best_match['product_name'] + " " + best_match['brand'])
        q_tokens = q_norm.split()
        
        is_substitute = False
        # If query has brand/spec but result score is low-ish (not exact name match), flag it.
        # Using a simple heuristic for now based on token overlap vs query length
        found_tokens = sum(1 for t in q_tokens if t in res_norm)
        if len(q_tokens) > 1 and found_tokens < len(q_tokens):
            is_substitute = True
                
        # Aggregate all offers
        product_offers = hits[hits['product_name'] == best_match['product_name']].sort_values('price')
        
        offers_list = []
        for _, r in product_offers.iterrows():
            offers_list.append({"retailer": r['retailer'], "price": float(r['price'])})

        found_items.append({
            "query": item,
            "product_name": str(best_match['product_name']),
            "is_substitute": is_substitute,
            "offers": offers_list,
            "best_price": offers_list[0]['price']
        })

    if not found_items:
        return {"actionable": True, "found_items": [], "global_missing": missing_global}

    # 2. MARKET MATRIX (Comparison across all stores)
    all_involved_retailers = set()
    for f in found_items:
        for o in f['offers']:
            all_involved_retailers.add(o['retailer'])
            
    store_comparison = []
    
    for retailer in all_involved_retailers:
        total_price = 0.0
        found_count = 0
        missing_in_store = []
        
        for item in found_items:
            price = next((o['price'] for o in item['offers'] if o['retailer'] == retailer), None)
            if price:
                total_price += price
                found_count += 1
            else:
                missing_in_store.append(item['product_name'])
                
        store_comparison.append({
            "retailer": retailer,
            "total_price": total_price,
            "found_count": found_count,
            "total_items": len(found_items),
            "missing_items": missing_in_store
        })

    store_comparison.sort(key=lambda x: (-x['found_count'], x['total_price']))
    
    return {
        "actionable": True,
        "is_basket": len(found_items) > 1,
        "found_items": found_items,
        "global_missing": missing_global,
        "market_matrix": store_comparison[:4], 
        "best_store": store_comparison[0] if store_comparison else None,
        "preferred_retailer": preferred_retailer
    }

def calculate_zesa_units(amount_usd: float) -> Dict[str, Any]:
    remaining = amount_usd / 1.06 
    units = 0.0
    
    t1 = ZIM_CONTEXT["zesa_step_1"]
    cost_t1 = t1["limit"] * t1["rate"]
    
    if remaining > cost_t1:
        units += t1["limit"]
        remaining -= cost_t1
        
        t2 = ZIM_CONTEXT["zesa_step_2"]
        cost_t2 = t2["limit"] * t2["rate"]
        
        if remaining > cost_t2:
            units += t2["limit"]
            remaining -= cost_t2
            units += remaining / ZIM_CONTEXT["zesa_step_3"]["rate"]
        else:
            units += remaining / t2["rate"]
    else:
        units += remaining / t1["rate"]

    return {
        "amount_usd": float(amount_usd),
        "est_units_kwh": float(round(units, 1))
    }

# =========================
# 3. Gemini Helpers (Vernacular & Intelligence)
# =========================

def gemini_detect_intent(transcript: str) -> Dict[str, Any]:
    if not _gemini_client: return {"actionable": False}
    
    PROMPT = """
    Analyze transcript. Return STRICT JSON.
    Classify intent:
    - CASUAL_CHAT: Greetings, "hi".
    - SHOPPING_BASKET: Looking for prices, products, "cheapest X".
    - UTILITY_CALC: Electricity/ZESA questions.
    - STORE_DECISION: "Where should I buy?", "Which store is cheapest?".
    - EVENT_PLANNING: "Plan a braai", "Wedding list", "Dinner for 5" (Implicit lists).
    
    Extract:
    - items: list of specific products found. **TRANSLATE ALL ITEMS TO ENGLISH** (e.g. 'Hupfu' -> 'Maize Meal').
    - utility_amount: number
    - store_preference: if a specific store is named (e.g. "at OK Mart").
    - is_event_planning: boolean (true if user asks to plan an event but lists no items).
    - language: Detected user language (e.g., "Shona", "Ndebele", "English").
    
    JSON Schema:
    {
      "actionable": boolean,
      "intent": "string",
      "items": ["string"],
      "utility_amount": number,
      "store_preference": "string",
      "is_event_planning": boolean,
      "language": "string"
    }
    """
    try:
        resp = _gemini_client.models.generate_content(
            model=GEMINI_MODEL,
            contents=PROMPT + "\nTranscript: " + transcript,
            config=types.GenerateContentConfig(response_mime_type="application/json")
        )
        return _safe_json_loads(resp.text, {"actionable": False, "intent": "CASUAL_CHAT", "language": "English"})
    except Exception as e:
        logger.error(f"Intent Detect Error: {e}")
        return {"actionable": False, "intent": "CASUAL_CHAT", "language": "English"}

def gemini_explode_concept(transcript: str) -> List[str]:
    """
    Converts a concept ("Braai for 10") into a concrete list ("Wors", "Charcoal").
    """
    if not _gemini_client: return []
    
    PROMPT = f"""
    User wants to plan an event: "{transcript}".
    Generate a STRICT list of 10-15 essential Zimbabwean shopping items for this.
    Use English terms for database lookup (e.g. 'Maize Meal', 'Cooking Oil').
    Return ONLY a JSON list of strings.
    """
    try:
        resp = _gemini_client.models.generate_content(
            model=GEMINI_MODEL,
            contents=PROMPT,
            config=types.GenerateContentConfig(response_mime_type="application/json")
        )
        return _safe_json_loads(resp.text, [])
    except Exception as e:
        logger.error(f"Explode Concept Error: {e}")
        return []

def gemini_analyze_image(image_b64: str, caption: str = "") -> Dict[str, Any]:
    if not _gemini_client: return {"error": "AI Offline"}
    
    PROMPT = f"""
    Analyze this image. Context: {caption}
    1. SHOPPING LIST? -> Extract items.
    2. SINGLE PRODUCT? -> Extract BRAND + NAME (e.g. "Pepsi 500ml").
    3. MEAL/DISH? -> Identify dish + ingredients.
    4. IRRELEVANT? -> Return type "IRRELEVANT".

    Return STRICT JSON:
    {{
      "type": "LIST" | "PRODUCT" | "MEAL" | "IRRELEVANT",
      "items": ["item1"],
      "description": "Short description"
    }}
    """
    try:
        image_bytes = base64.b64decode(image_b64)
        resp = _gemini_client.models.generate_content(
            model=GEMINI_MODEL,
            contents=[
                PROMPT, 
                types.Part.from_bytes(data=image_bytes, mime_type="image/jpeg")
            ],
            config=types.GenerateContentConfig(response_mime_type="application/json")
        )
        result = _safe_json_loads(resp.text, {"type": "IRRELEVANT", "items": []})
        return result
    except Exception as e:
        logger.error(f"Vision Error: {e}")
        return {"type": "IRRELEVANT", "items": []}

def gemini_chat_response(transcript: str, intent: Dict, analyst_data: Dict, chat_history: str = "") -> str:
    if not _gemini_client: return "I'm having trouble connecting to my brain right now."

    context_str = f"RECENT CHAT HISTORY (Last 6 messages):\n{chat_history}\n" if chat_history else ""
    context_str += f"ZIMBABWE CONTEXT: Fuel={ZIM_CONTEXT['fuel_petrol']}, ZESA Rate={ZIM_CONTEXT['zesa_step_1']['rate']}\n"
    
    if analyst_data:
        context_str += f"ANALYST DATA: {json.dumps(analyst_data, default=str)}\n"

    language = intent.get("language", "English")

    PROMPT = f"""
    You are Jessica, Pricelyst's Shopping Advisor (Zimbabwe).
    Role: Intelligent Shopping Companion.
    Goal: Shortest path to value. Complete Transparency.
    
    INPUT: "{transcript}"
    USER LANGUAGE: {language}
    INTENT: {intent.get('intent')}
    CONTEXT:
    {context_str}
    
    LOGIC RULES:
    
    1. **LANGUAGE**: Reply in **{language}**. If Shona, use Shona. If English, use English.
    
    2. **BASKET COMPARISON**:
       - If `market_matrix` has multiple stores, compare totals.
       - "Spar is **$6.95**, OK Mart is **$4.00** (but missing Oil)."

    3. **BRAND SUBSTITUTES (Phrasing)**:
       - If `is_substitute` is TRUE for an item, say: 
         "I couldn't find **[Query]**, but the **nearest match is** **[Found]** ($Price)."

    4. **SINGLE ITEMS**:
       - Best price first, then others.

    5. **CASUAL**:
       - Reset if user says "Hi".
       
    TONE: Helpful, direct, Zimbabwean. Use Markdown.
    """
    
    try:
        resp = _gemini_client.models.generate_content(
            model=GEMINI_MODEL,
            contents=PROMPT
        )
        return resp.text
    except Exception as e:
        logger.error(f"Chat Gen Error: {e}")
        return "I checked the prices, but I'm having trouble displaying them right now."

def gemini_generate_4step_plan(transcript: str, analyst_result: Dict) -> str:
    if not _gemini_client: return "# Error\nAI Offline."

    PROMPT = f"""
    Generate a formatted Markdown Shopping Plan.
    
    USER REQUEST: "{transcript}"
    DATA: {json.dumps(analyst_result, indent=2, default=str)}
    
    CRITICAL INSTRUCTION:
    For items in 'global_missing', you MUST provide a Realistic USD Estimate (e.g. Chicken ~$6.00).
    Do not leave them as "Unknown".
    
    SECTIONS:
    
    1. **In Our Catalogue ✅** 
       (Markdown Table: | Item | Retailer | Price (USD) |)
    
    2. **Not in Catalogue (Estimates) 😔**
       (Markdown Table: | Item | Estimated Price (USD) |)
       *Fill in estimated prices for missing items based on Zimbabwe market knowledge.*
       
    3. **Totals 💰**
       - Confirmed Total (Catalogue)
       - Estimated Total (Missing Items)
       - **Grand Total Estimate**
       
    4. **Ideas & Tips 💡**
       - 3 Creative ideas based on the specific event/meal (e.g. Braai tips, Cooking hacks).
    
    Tone: Warm, Professional, Zimbabwean.
    """
    try:
        resp = _gemini_client.models.generate_content(model=GEMINI_MODEL, contents=PROMPT)
        return resp.text
    except Exception as e:
        return "# Error\nCould not generate plan."

# =========================
# 4. Endpoints
# =========================

@app.get("/health")
def health():
    df = get_market_index()
    return jsonify({
        "ok": True,
        "offers_indexed": len(df),
        "api_source": PRICE_API_BASE,
        "persona": "Jessica v3.1 (Babel Fish)"
    })

@app.post("/chat")
def chat():
    body = request.get_json(silent=True) or {}
    msg = body.get("message", "")
    pid = body.get("profile_id")
    
    if not pid: return jsonify({"ok": False, "error": "Missing profile_id"}), 400
    
    # History
    history_str = ""
    if db:
        try:
            docs = db.collection("pricelyst_profiles").document(pid).collection("chat_logs") \
                     .order_by("ts", direction=firestore.Query.DESCENDING).limit(6).stream()
            msgs = [f"User: {d.to_dict().get('message')}\nJessica: {d.to_dict().get('response')}" for d in docs]
            if msgs: history_str = "\n".join(reversed(msgs))
        except: pass

    # Intent
    intent_data = gemini_detect_intent(msg)
    intent_type = intent_data.get("intent", "CASUAL_CHAT")
    items = intent_data.get("items", [])
    store_pref = intent_data.get("store_preference") 
    
    analyst_data = {}
    
    if items or intent_type in ["SHOPPING_BASKET", "STORE_DECISION", "TRUST_CHECK"]:
        analyst_data = calculate_basket_optimization(items, preferred_retailer=store_pref)
        
    elif intent_type == "UTILITY_CALC":
        amount = intent_data.get("utility_amount", 20)
        analyst_data = calculate_zesa_units(amount)
        
    reply = gemini_chat_response(msg, intent_data, analyst_data, history_str)
    
    if db:
        db.collection("pricelyst_profiles").document(pid).collection("chat_logs").add({
            "message": msg,
            "response": reply,
            "intent": intent_data,
            "ts": datetime.now(timezone.utc).isoformat()
        })

    return jsonify({"ok": True, "data": {"message": reply, "analyst_debug": analyst_data if items else None}})

@app.post("/api/analyze-image")
def analyze_image():
    body = request.get_json(silent=True) or {}
    image_b64 = body.get("image_data") 
    caption = body.get("caption", "")
    pid = body.get("profile_id")
    
    if not image_b64 or not pid: return jsonify({"ok": False}), 400
    
    vision_result = gemini_analyze_image(image_b64, caption)
    img_type = vision_result.get("type", "IRRELEVANT")
    items = vision_result.get("items", [])
    description = vision_result.get("description", "an image")
    
    # Fallback for empty products
    if (img_type in ["PRODUCT", "MEAL"]) and not items and description:
        items = [description]

    response_text = ""
    analyst_data = {}
    
    if img_type == "IRRELEVANT" and not items:
        prompt = f"User uploaded photo of {description}. Compliment it if appropriate, then explain you are a shopping bot."
        response_text = gemini_chat_response(prompt, {"intent": "CASUAL_CHAT"}, {}, "")
        
    elif items:
        analyst_data = calculate_basket_optimization(items)
        
        sim_msg = ""
        if img_type == "MEAL": sim_msg = f"I want to cook {description}. Cost of ingredients: {', '.join(items)}?"
        elif img_type == "LIST": sim_msg = f"Price of list: {', '.join(items)}?"
        else: sim_msg = f"Cheapest price for {', '.join(items)}?"
        
        response_text = gemini_chat_response(sim_msg, {"intent": "STORE_DECISION"}, analyst_data, "")
    
    else:
        response_text = "I couldn't identify the product. Could you type the name?"

    return jsonify({
        "ok": True,
        "image_type": img_type,
        "items_identified": items,
        "message": response_text,
        "analyst_data": analyst_data
    })

@app.post("/api/call-briefing")
def call_briefing():
    """
    Injects INTELLIGENT Market Data into the Voice Bot's context.
    Includes: Staples Index, ZESA/Fuel, Top 60 Catalogue.
    """
    body = request.get_json(silent=True) or {}
    pid = body.get("profile_id")
    username = body.get("username", "Friend")
    
    if not pid: return jsonify({"ok": False}), 400
    
    # 1. Memory Profile
    prof = {}
    if db:
        ref = db.collection("pricelyst_profiles").document(pid)
        doc = ref.get()
        if doc.exists: prof = doc.to_dict()
        else: ref.set({"created_at": datetime.now(timezone.utc).isoformat()})
    
    if username != "Friend" and username != prof.get("username"):
        if db: db.collection("pricelyst_profiles").document(pid).set({"username": username}, merge=True)

    # 2. Market Intelligence Generation
    df = get_market_index()
    market_intel = ""
    
    # A. ZESA & Fuel
    zesa_10 = calculate_zesa_units(10.0)
    zesa_20 = calculate_zesa_units(20.0)
    
    context_section = f"""
    [CRITICAL CONTEXT - ZIMBABWE]
    FUEL: Petrol=${ZIM_CONTEXT['fuel_petrol']}, Diesel=${ZIM_CONTEXT['fuel_diesel']}
    BREAD: ~${ZIM_CONTEXT['bread_avg']}
    ZESA (Electricity): $10 = {zesa_10['est_units_kwh']}u, $20 = {zesa_20['est_units_kwh']}u
    """
    
    # B. Staples Index
    staples = ["Cooking Oil", "Maize Meal", "Sugar", "Rice"]
    staple_summary = []
    
    if not df.empty:
        for s in staples:
            hits = search_products_deep(df[df['is_offer']==True], s, limit=5)
            if not hits.empty:
                cheapest = hits.sort_values('price').iloc[0]
                staple_summary.append(f"- {s}: ${cheapest['price']} @ {cheapest['retailer']}")
    
    staples_section = "\n[STAPLES - LOWEST]\n" + "\n".join(staple_summary)
    
    # C. Top 60 Catalogue
    catalogue_lines = []
    if not df.empty:
        top_items = df[df['is_offer']==True].sort_values('views', ascending=False).drop_duplicates('product_name').head(60)
        for _, r in top_items.iterrows():
            p_name = r['product_name']
            all_offers = df[(df['product_name'] == p_name) & df['is_offer']]
            prices_str = ", ".join([f"${o['price']} ({o['retailer']})" for _, o in all_offers.iterrows()])
            catalogue_lines.append(f"- {p_name}: {prices_str}")
            
    catalogue_section = "\n[CATALOGUE - TOP 60]\n" + "\n".join(catalogue_lines)
    
    return jsonify({
        "ok": True,
        "username": username,
        "memory_summary": prof.get("memory_summary", ""),
        "kpi_snapshot": context_section + staples_section + catalogue_section
    })

@app.post("/api/log-call-usage")
def log_call_usage():
    """
    Post-Call Orchestrator.
    v3.1: Handles Concept Explosion & Plan Generation.
    """
    body = request.get_json(silent=True) or {}
    pid = body.get("profile_id")
    transcript = body.get("transcript", "")
    
    if not pid: return jsonify({"ok": False}), 400
    
    # 1. Update Long-Term Memory
    if len(transcript) > 20 and db:
        try:
            curr_mem = db.collection("pricelyst_profiles").document(pid).get().to_dict().get("memory_summary", "")
            mem_prompt = f"Update user memory (budget, family size) based on: {transcript}\nOLD: {curr_mem}"
            mem_resp = _gemini_client.models.generate_content(model=GEMINI_MODEL, contents=mem_prompt)
            db.collection("pricelyst_profiles").document(pid).set({"memory_summary": mem_resp.text}, merge=True)
        except: pass

    # 2. Plan Generation Logic
    intent_data = gemini_detect_intent(transcript)
    plan_data = {}
    
    # Check if ACTIONABLE (Shopping or Event)
    if intent_data.get("actionable"):
        target_items = intent_data.get("items", [])
        
        # LOGIC: If Event Planning + No specific items -> EXPLODE CONCEPT
        if intent_data.get("is_event_planning") and not target_items:
            logger.info("💥 Exploding Concept for Event...")
            target_items = gemini_explode_concept(transcript)
            
        if target_items:
            analyst_result = calculate_basket_optimization(target_items)
            
            # v3.1: Generate Plan with Estimates & Creative Tips
            md_content = gemini_generate_4step_plan(transcript, analyst_result)
            
            plan_data = {
                "is_actionable": True,
                "title": f"Plan ({datetime.now().strftime('%d %b')})",
                "markdown_content": md_content,
                "items": target_items,
                "created_at": datetime.now(timezone.utc).isoformat()
            }
            
            if db:
                doc_ref = db.collection("pricelyst_profiles").document(pid).collection("shopping_plans").document()
                plan_data["id"] = doc_ref.id
                doc_ref.set(plan_data)

    if db:
        db.collection("pricelyst_profiles").document(pid).collection("call_logs").add({
            "transcript": transcript,
            "intent": intent_data,
            "plan_generated": bool(plan_data),
            "ts": datetime.now(timezone.utc).isoformat()
        })

    return jsonify({
        "ok": True,
        "shopping_plan": plan_data if plan_data.get("is_actionable") else None
    })

@app.get("/api/shopping-plans")
def list_plans():
    pid = request.args.get("profile_id")
    if not pid or not db: return jsonify({"ok": False}), 400
    try:
        docs = db.collection("pricelyst_profiles").document(pid).collection("shopping_plans") \
                 .order_by("created_at", direction=firestore.Query.DESCENDING).limit(10).stream()
        return jsonify({"ok": True, "plans": [{"id": d.id, **d.to_dict()} for d in docs]})
    except: return jsonify({"ok": False}), 500

@app.delete("/api/shopping-plans/<plan_id>")
def delete_plan(plan_id):
    pid = request.args.get("profile_id")
    if not pid or not db: return jsonify({"ok": False}), 400
    try:
        db.collection("pricelyst_profiles").document(pid).collection("shopping_plans").document(plan_id).delete()
        return jsonify({"ok": True})
    except: return jsonify({"ok": False}), 500

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 7860))
    try: get_market_index(force_refresh=True)
    except: pass
    app.run(host="0.0.0.0", port=port)