File size: 31,080 Bytes
771f178
 
 
 
a0a0034
 
 
771f178
a0a0034
 
 
771f178
 
 
 
 
 
 
 
a0a0034
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
771f178
 
 
 
 
 
 
 
 
 
a0a0034
 
 
 
771f178
a0a0034
 
771f178
 
a0a0034
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
771f178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0a0034
 
 
 
 
771f178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0a0034
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
771f178
 
 
 
 
 
 
 
 
 
 
 
a0a0034
 
 
 
 
771f178
a0a0034
 
 
 
 
 
 
 
 
 
 
771f178
a0a0034
 
771f178
 
a0a0034
 
 
771f178
a0a0034
771f178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0a0034
771f178
 
 
 
 
 
 
a0a0034
 
 
 
771f178
a0a0034
771f178
 
 
 
 
 
 
 
 
a0a0034
 
 
 
 
 
 
 
771f178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0a0034
 
 
 
 
 
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
import os
os.environ["ANONYMIZED_TELEMETRY"] = "False"

from fastapi import FastAPI, HTTPException, BackgroundTasks
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from typing import Optional, List, Dict
import chromadb
from chromadb.utils import embedding_functions
import uuid
from openai import OpenAI
from scrapers import scrape_all
import requests
import base64
import json
import os
from database import log_interaction, create_user, verify_user, save_chat_message, get_chat_history
from collaborative_filter import get_collaborative_recommendations

# Initialize FastAPI App
app = FastAPI(title="Darak AI Real Estate Engine")

# Enable CORS for frontend connection
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"], # Allow local frontend to connect
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Initialize ChromaDB (Local Vector Database)
chroma_client = chromadb.PersistentClient(path="./chroma_db")

# Initialize OpenAI Client for OpenRouter API
OPENROUTER_KEY = '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'
openai_client = OpenAI(
    api_key=OPENROUTER_KEY,
    base_url="https://openrouter.ai/api/v1"
)

# Initialize Local Embedding Model (Free, runs on CPU)
# We must use this because the router does not support embeddings.
default_ef = embedding_functions.DefaultEmbeddingFunction()

# Create or get the Vector Collection
collection = chroma_client.get_or_create_collection(
    name="egypt_properties",
    embedding_function=default_ef
)

# Removed duplicate OpenRouter key since it is defined above

# -----------------
# DATA MODELS
# -----------------
class Property(BaseModel):
    title: str
    type: str
    location: str
    price: str
    status: str
    description: str
    lat: float
    lng: float
    image: str
    matterport_id: str = ""

class UserQuery(BaseModel):
    goal: str
    property_type: str
    budget: str
    location: str
    user_id: str = "guest"

class Interaction(BaseModel):
    user_id: str
    property_id: str
    interaction_type: str # 'like' or 'view'

class DesignRequest(BaseModel):
    image_base64: str  # base64 encoded image
    style: str        # e.g., "مودرن (Modern)"
    instructions: str = ""  # optional extra instructions

class ChatRequest(BaseModel):
    message: str
    history: list = []

class PropertyEvaluationRequest(BaseModel):
    prop_type: str
    location: str
    area: float
    finish: str
    price: float

class PropertyInsightRequest(BaseModel):
    title: str
    price: str
    location: str
    type: str
    description: str

class PropertyCompareRequest(BaseModel):
    prop1: dict
    prop2: dict

class AuthRequest(BaseModel):
    username: str
    password: str

class AuthenticatedChatRequest(BaseModel):
    message: Optional[str] = ""
    user_id: str = "guest"
    file_data: Optional[str] = None
    file_name: Optional[str] = None
    file_type: Optional[str] = None

# -----------------
# API ENDPOINTS
# -----------------


@app.post("/api/auth/register")
async def register(req: AuthRequest):
    user_id = create_user(req.username, req.password)
    if not user_id:
        raise HTTPException(status_code=400, detail="Username already exists")
    return {"token": user_id, "username": req.username}

@app.post("/api/auth/login")
async def login(req: AuthRequest):
    user_id = verify_user(req.username, req.password)
    if not user_id:
        raise HTTPException(status_code=401, detail="Invalid username or password")
    return {"token": user_id, "username": req.username}

@app.post("/api/chat")
async def chat_assistant(request: AuthenticatedChatRequest):
    """
    General chat endpoint for the Dark AI assistant.
    Uses database history and Gemini 2.5 Flash API to provide conversational responses in Arabic.
    """
    try:
        messages = [
            {
                "role": "system",
                "content": "أنت مساعد ذكي اسمك 'Dark' متخصص في العقارات في مصر. مهمتك مساعدة المستخدمين في العثور على عقارات، الإجابة على استفساراتهم العقارية، وتقديم نصائح للاستثمار العقاري. يجب أن تكون إجاباتك قصيرة، ودودة، ومفيدة، ودائماً باللغة العربية."
            }
        ]
        
        # Load history from DB
        history = []
        if request.user_id != "guest":
            history = get_chat_history(request.user_id, limit=10)
        
        # Add history
        for msg in history:
            messages.append({"role": msg.get("role", "user"), "content": msg.get("content", "")})
            
        msg_text = (request.message or "").strip()
        if request.file_data:
            f_type = (request.file_type or "").lower()
            f_name = request.file_name or "attachment"
            
            # 1. Image handling (Multimodal Vision)
            if "image" in f_type or request.file_data.startswith("data:image"):
                url_data = request.file_data if request.file_data.startswith("data:") else f"data:{f_type or 'image/jpeg'};base64,{request.file_data}"
                prompt_text = msg_text if msg_text else f"يرجى تحليل هذه الصورة المرفقة ({f_name}) وشرح ما يتعلق بالعقارات والتصميم."
                user_content = [
                    {"type": "text", "text": prompt_text},
                    {"type": "image_url", "image_url": {"url": url_data}}
                ]
                messages.append({"role": "user", "content": user_content})
                
            # 2. PDF handling (Text Extraction)
            elif "pdf" in f_type or f_name.endswith(".pdf"):
                extracted_text = ""
                try:
                    import pypdf, io, base64
                    b64_str = request.file_data.split(",")[-1] if "," in request.file_data else request.file_data
                    pdf_bytes = base64.b64decode(b64_str)
                    reader = pypdf.PdfReader(io.BytesIO(pdf_bytes))
                    for page in reader.pages:
                        t = page.extract_text()
                        if t: extracted_text += t + "\n"
                except Exception as e:
                    print(f"[PDF Extraction Error]: {e}")
                    extracted_text = "[تعذر استخراج النص من ملف PDF]"
                
                prompt_text = msg_text if msg_text else "قم بتحليل ملف PDF المرفق بالتفصيل."
                full_text = f"{prompt_text}\n\n--- [محتوى ملف PDF: {f_name}] ---\n{extracted_text}"
                messages.append({"role": "user", "content": full_text})
                
            # 3. Text / JSON / CSV handling
            else:
                try:
                    import base64
                    b64_str = request.file_data.split(",")[-1] if "," in request.file_data else request.file_data
                    doc_text = base64.b64decode(b64_str).decode("utf-8", errors="ignore")
                except Exception:
                    doc_text = "[تعذر قراءة محتوى الملف]"
                
                prompt_text = msg_text if msg_text else f"قم بتحليل الملف المرفق ({f_name})."
                full_text = f"{prompt_text}\n\n--- [محتوى الملف: {f_name}] ---\n{doc_text}"
                messages.append({"role": "user", "content": full_text})
        else:
            messages.append({"role": "user", "content": msg_text or "مرحباً"})

        # Save user message
        if request.user_id != "guest":
            log_text = msg_text if msg_text else f"[ملف مرفق: {request.file_name}]"
            save_chat_message(request.user_id, "user", log_text)

        # Use OpenAI Chat API with custom router
        response = openai_client.chat.completions.create(
            model="google/gemini-2.5-flash",
            messages=messages,
            max_tokens=300
        )
        
        reply = response.choices[0].message.content
        if not reply:
            return {"reply": 'عذراً، لم أتمكن من معالجة طلبك الآن.'}
            
        # Save assistant message
        if request.user_id != "guest":
            save_chat_message(request.user_id, "assistant", reply)
            
        return {"reply": reply}
            
            
    except Exception as e:
        print(f"[Chat Fatal Error] {str(e)}")
        return {"reply": "عذراً، حدث خطأ غير متوقع."}

@app.post("/api/evaluate")
async def evaluate_property(request: PropertyEvaluationRequest):
    """
    Intelligently analyzes a property's price based on its features using the LLM.
    """
    try:
        # Prompt for the LLM
        prompt = f"""أنت خبير عقاري محترف. قم بتقييم هذا العقار المعروض للبيع.
نوع العقار: {request.prop_type}
المنطقة: {request.location}
المساحة: {request.area} متر مربع
التشطيب: {request.finish}
السعر المعروض: {request.price} جنيه

قم بتحليل السعر وأرجع الرد بصيغة JSON فقط بالهيكل التالي (لا تكتب أي كلام آخر غير JSON):
{{
  "verdict_title": "عنوان التقييم (مثال: سعر ممتاز جداً، عادل، أو مبالغ فيه)",
  "verdict_description": "وصف قصير عن التقييم والسبب",
  "score_percentage": 85,
  "average_sqm_price": 20000,
  "estimated_value": 3000000,
  "smart_tip": "نصيحة استثمارية سريعة بخصوص هذا العقار"
}}
"""
        response = openai_client.chat.completions.create(
            model="google/gemini-2.5-flash",
            messages=[{"role": "user", "content": prompt}],
            max_tokens=800
        )
        
        reply = response.choices[0].message.content
        import re
        import json
        # Clean up markdown if any
        content = re.sub(r'^```[a-zA-Z]*\s*', '', reply.strip())
        content = re.sub(r'```\s*$', '', content.strip())
        
        json_match = re.search(r'\{[\s\S]*\}', content)
        if json_match:
            data = json.loads(json_match.group(0))
            return {"success": True, "data": data}
        else:
            return {"success": False, "error": "Invalid response format from AI"}
            
    except Exception as e:
        err = str(e)
        print(f"[Evaluate Error] {err}")
        if "429" in err or "RESOURCE_EXHAUSTED" in err:
            return {"success": False, "error": "تجاوزت حصة الذكاء الاصطناعي اليومية. يرجى المحاولة لاحقاً."}
        return {"success": False, "error": err}

@app.post("/api/property/insight")
async def property_insight(request: PropertyInsightRequest):
    """
    Generates a dynamic AI valuation, investment score, and intelligently extracts specs for a specific property.
    """
    try:
        prompt = f"""أنت مستشار عقاري خبير في السوق المصري. قم بتحليل هذا العقار واستخراج تفاصيله بدقة:
العنوان: {request.title}
السعر: {request.price}
المنطقة: {request.location}
النوع: {request.type}
الوصف: {request.description}

بناءً على ذلك، أرجع الرد بصيغة JSON فقط بالهيكل التالي (لا تكتب أي نصوص أخرى إطلاقاً). إذا لم تكن بعض التفاصيل (مثل عدد الغرف) مذكورة بوضوح، قم بوضع تقدير منطقي بناءً على السعر والمساحة:
{{
  "valuation_title": "عنوان التقييم (مثال: فرصة ممتازة، سعر عادل، أو أعلى من السوق)",
  "valuation_text": "جملة واحدة تشرح تقييم السعر.",
  "roi_percentage": "رقم مئوي (مثال: 12% سنوي)",
  "roi_progress": 80, 
  "growth_percentage": "رقم مئوي (مثال: + 25% متوقع)",
  "growth_progress": 90,
  "specs": {{
    "beds": "عدد الغرف المستنتج أو الحقيقي (رقم فقط)",
    "baths": "عدد الحمامات المستنتج (رقم فقط)",
    "area": "المساحة بالمتر المربع المستنتجة أو الحقيقية (مثال: 150)",
    "parking": "عدد مواقف السيارات المستنتج (رقم فقط، مثلا: 1 أو 2)"
  }}
}}
"""
        response = openai_client.chat.completions.create(
            model="google/gemini-2.5-flash",
            messages=[{"role": "user", "content": prompt}],
            max_tokens=600
        )
        
        reply = response.choices[0].message.content
        import re
        import json
        content = re.sub(r'^```[a-zA-Z]*\s*', '', reply.strip())
        content = re.sub(r'```\s*$', '', content.strip())
        
        json_match = re.search(r'\{[\s\S]*\}', content)
        if json_match:
            data = json.loads(json_match.group(0))
            return {"success": True, "data": data}
        else:
            return {"success": False, "error": "Invalid response format"}
            
    except Exception as e:
        err = str(e)
        print(f"[Insight Error] {err}")
        if "429" in err or "RESOURCE_EXHAUSTED" in err:
            return {"success": False, "error": "تجاوزت حصة الذكاء الاصطناعي اليومية. يرجى المحاولة لاحقاً."}
        return {"success": False, "error": err}

@app.post("/api/property/compare")
async def property_compare(request: PropertyCompareRequest):
    """
    Generates a dynamic AI comparison between two properties and extracts their details.
    """
    try:
        prompt = f"""أنت مستشار عقاري خبير. قم بإنشاء تقرير مقارنة بين العقارين، مع استخراج (أو استنتاج منطقي بناءً على السعر والوصف) لتفاصيلها.
أرجع الرد بصيغة JSON فقط كالتالي (تأكد أن يكون بصيغة JSON صحيحة بدون نصوص أخرى):
{{
  "summary": "رأيك كمستشار عن أيهما أفضل للاستثمار وأيهما أفضل للسكن...",
  "prop1": {{
    "area": "مساحة تقديرية أو حقيقية (مثال: ١٥٠ م٢)",
    "rooms": "عدد غرف تقديري (مثال: ٣ نوم)",
    "finish": "نوع التشطيب (مثال: سوبر لوكس)",
    "roi": "نسبة مئوية (مثال: ١٠٪)"
  }},
  "prop2": {{
    "area": "مساحة تقديرية أو حقيقية",
    "rooms": "عدد غرف تقديري",
    "finish": "نوع التشطيب",
    "roi": "نسبة مئوية"
  }}
}}

العقار الأول:
العنوان: {request.prop1.get('title')} ({request.prop1.get('location')}) - السعر: {request.prop1.get('price')}
الوصف: {request.prop1.get('description')}

العقار الثاني:
العنوان: {request.prop2.get('title')} ({request.prop2.get('location')}) - السعر: {request.prop2.get('price')}
الوصف: {request.prop2.get('description')}
"""
        
        response = openai_client.chat.completions.create(
            model="google/gemini-2.5-flash",
            messages=[{"role": "user", "content": prompt}],
            max_tokens=600
        )
        
        reply = response.choices[0].message.content
        import re
        import json
        content = re.sub(r'^```[a-zA-Z]*\s*', '', reply.strip())
        content = re.sub(r'```\s*$', '', content.strip())
        json_match = re.search(r'\{[\s\S]*\}', content)
        
        if json_match:
            data = json.loads(json_match.group(0))
            return {"success": True, "data": data}
        else:
            return {"success": False, "error": "Invalid JSON from AI"}
    except Exception as e:
        err = str(e)
        print(f"[Compare Error] {err}")
        if "429" in err or "RESOURCE_EXHAUSTED" in err:
            return {"success": False, "error": "تجاوزت حصة الذكاء الاصطناعي اليومية. يرجى المحاولة لاحقاً."}
        return {"success": False, "error": err}

@app.post("/api/design/analyze")
async def design_analyze(request: DesignRequest):
    """
    Analyzes a room image and suggests design changes using Gemini 2.5 Flash Vision.
    """
    try:
        print(f"[Design] Analyzing room with style: {request.style}")
        
        extra = f"Extra instructions: {request.instructions}" if request.instructions else ""
        analysis_prompt = (
            f"You are an expert interior designer. Analyze this room image and redesign it in {request.style} style. "
            f"{extra} "
            "Respond with ONLY a raw JSON object (no markdown, no code fences, no explanation). "
            "Use this exact structure:\n"
            '{"room_type": "living room", "design_description": "وصف بالعربية", '
            '"image_gen_prompt": "photorealistic modern interior design", '
            '"furniture": [{"category": "أريكة", "name": "اسم بالعربية", "price": "9500", "furniture_type": "sofa"}]}'
        )

        # Format base64 properly
        if request.image_base64.startswith('data:'):
            image_url = request.image_base64
        else:
            image_url = f"data:image/jpeg;base64,{request.image_base64}"
            
        print("[Design] Calling OpenRouter API...")
        headers = {
            "Authorization": f"Bearer {OPENROUTER_KEY}",
            "HTTP-Referer": "http://localhost:3000",
            "X-Title": "Darak"
        }
        payload = {
            "model": "google/gemini-2.5-flash",
            "messages": [
                {
                    "role": "user",
                    "content": [
                        {"type": "text", "text": analysis_prompt},
                        {"type": "image_url", "image_url": {"url": image_url}}
                    ]
                }
            ]
        }
        resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
        resp_json = resp.json()
        
        if "error" in resp_json:
            print(f"[Design Error] OpenRouter API Error: {resp_json['error']}")
            return get_mock_design()
            
        content = resp_json['choices'][0]['message']['content']
        
        if not content:
            print("[Design Error] No content in response")
            return get_mock_design()
            
        print(f"[Design] Got response ({len(content)} chars)")
        
        import re
        content = re.sub(r'^```[a-zA-Z]*\s*', '', content.strip())
        content = re.sub(r'```\s*$', '', content.strip())
        json_match = re.search(r'\{[\s\S]*\}', content)
        
        if not json_match:
            print(f"[Design Error] No JSON found in response: {content[:200]}")
            return get_mock_design()
        
        json_str = json_match.group(0)
        try:
            design_data = json.loads(json_str)
            print("[Design] ✓ Successfully parsed design data")
            return {"success": True, "data": design_data}
        except json.JSONDecodeError as e:
            print(f"[Design Error] Failed to parse JSON: {str(e)}")
            repaired = repair_json(json_str)
            if repaired:
                try:
                    design_data = json.loads(repaired)
                    print("[Design] ✓ Successfully repaired and parsed JSON")
                    return {"success": True, "data": design_data}
                except:
                    pass
            return get_mock_design()
    
    except Exception as e:
        print(f"[Design Fatal Error] {str(e)}")
        return get_mock_design()


def repair_json(s):
    """Attempt to repair malformed JSON"""
    import re
    # Remove trailing commas
    s = re.sub(r',(\s*[}\]])', r'\1', s)
    # Try to parse
    try:
        json.loads(s)
        return s
    except:
        return None


def get_mock_design():
    """Return mock design data when API fails"""
    return {
        "success": True,
        "data": {
            "room_type": "living room",
            "design_description": "تصميم حديث وأنيق مع أثاث عملي ومريح يتناسب مع ذوقك",
            "image_gen_prompt": "photorealistic modern interior design",
            "furniture": [
                {"category": "أريكة", "name": "أريكة جلدية سوداء حديثة", "price": "9500", "furniture_type": "sofa"},
                {"category": "طاولة قهوة", "name": "طاولة خشبية أنيقة", "price": "4200", "furniture_type": "coffee table"},
                {"category": "إضاءة", "name": "مصباح أرضي ذهبي", "price": "1800", "furniture_type": "lamp"},
                {"category": "سجادة", "name": "سجادة فاخرة رمادية", "price": "2300", "furniture_type": "rug"}
            ]
        }
    }

@app.post("/api/ingest")
async def ingest_property(prop: Property):
    """
    Takes a scraped property and saves it into the Vector Database.
    The description and location are automatically converted into AI Vectors.
    """
    prop_id = str(uuid.uuid4())
    
    # The text we want the AI to understand mathematically
    ai_context = f"{prop.title}. A {prop.type} located in {prop.location}. {prop.description}. Price: {prop.price}."
    
    collection.add(
        documents=[ai_context], # This gets embedded
        metadatas=[prop.dict()], # Store all raw data for the frontend
        ids=[prop_id]
    )
    
    return {"message": "Property ingested into AI Vector Database successfully", "id": prop_id}

@app.post("/api/interact")
async def track_interaction(interaction: Interaction):
    """
    Logs user interactions (like/view) into the SQLite database.
    """
    try:
        log_interaction(interaction.user_id, interaction.property_id, interaction.interaction_type)
        return {"message": "Interaction logged"}
    except Exception as e:
        print(f"[!] DB Error logging interaction: {e}")
        return {"error": "Failed to log interaction"}

@app.post("/api/recommend")
async def recommend_properties(query: UserQuery):
    """
    Takes the user's answers from the Onboarding Quiz, converts them into a Vector,
    and searches the Vector Database for the closest Semantic Matches.
    Also injects Collaborative Filtering if the user has history.
    """
    if collection.count() == 0:
        # Failsafe: If DB is empty, ingest dummy data
        ingest_dummy_data()
        
    # Construct the search query from the user's answers
    search_text = f"I am looking for a {query.property_type} in {query.location} for {query.goal}. My budget is {query.budget}."
    
    # Perform Cosine Similarity Search in ChromaDB
    results = collection.query(
        query_texts=[search_text],
        n_results=10 # Get more to allow re-ranking
    )
    
    # Format semantic results
    semantic_matches = []
    if results['metadatas']:
        for i, meta in enumerate(results['metadatas'][0]):
            distance = results['distances'][0][i]
            match_score = max(50, int(100 - (distance * 30)))
            meta['matchScore'] = match_score
            # Pass the internal chroma id back so we can track interactions easily
            meta['property_id'] = results['ids'][0][i]
            semantic_matches.append(meta)
            
    # -- Collaborative Filtering Injection --
    collab_boosted = []
    collab_ids = []
    if query.user_id != "guest":
        # Get property IDs recommended by other similar users
        collab_ids = get_collaborative_recommendations(query.user_id, all_properties=None, top_k=3)
        print(f"[*] Collaborative matches for {query.user_id}: {collab_ids}")
        
    # Re-rank: If a semantic match is also a collaborative match, boost it.
    for match in semantic_matches:
        if match.get('property_id') in collab_ids:
            match['matchScore'] = min(99, match['matchScore'] + 15) # Boost score
            match['isCollab'] = True
            
    # Sort by matchScore descending and return top 5
    semantic_matches.sort(key=lambda x: x['matchScore'], reverse=True)
    
    # Ensure properties that were ONLY in collab_ids are fetched if we need more (advanced logic)
    # For now, boosting the semantic ones is a great hybrid start.
    
    return {"recommendations": semantic_matches[:5]}


def ingest_dummy_data():
    """ Helper function to populate the Vector DB if it's empty """
    print("Database empty. Ingesting realistic Egyptian properties...")
    dummy_properties = [
        Property(title="شقة فاخرة في تاج سيتي", type="Apartment", location="التجمع الخامس", price="٤٬٥٠٠٬٠٠٠", status="للبيع", description="شقة رائعة بالقرب من المدارس الدولية بمساحة 180 متر مربع. تشطيب سوبر لوكس.", lat=30.0682, lng=31.3653, image="https://images.unsplash.com/photo-1512917774080-9991f1c4c750?w=600"),
        Property(title="فيلا مستقلة بكمبوند ميفيدا", type="Villa", location="التجمع الخامس", price="١٨٬٠٠٠٬٠٠٠", status="للبيع", description="فيلا مستقلة بحمام سباحة وحديقة خاصة في شارع التسعين. مساحة المبنى 400 متر والحديقة 200 متر.", lat=30.0125, lng=31.4552, image="https://images.unsplash.com/photo-1600596542815-ffad4c1539a9?w=600"),
        Property(title="تاون هاوس بيفرلي هيلز", type="Townhouse", location="الشيخ زايد", price="٨٬٢٠٠٬٠٠٠", status="للبيع", description="تاون هاوس حديث بتشطيب الترا سوبر لوكس داخل كمبوند. مساحة 250 متر.", lat=30.0469, lng=30.9850, image="https://images.unsplash.com/photo-1600607687931-cebf5871f585?w=600"),
        Property(title="شقة بمدينتي مجموعة B", type="Apartment", location="مدينتي", price="٣٬٢٠٠٬٠٠٠", status="للبيع", description="شقة مميزة بمدينتي تطل على الوايد جاردن مساحة 140 متر مربع، 3 غرف نوم.", lat=30.0934, lng=31.6222, image="https://images.unsplash.com/photo-1522708323590-d24dbb6b0267?w=600"),
        Property(title="شاليه بقرية مراسي", type="Chalet", location="الساحل الشمالي", price="١٢٬٥٠٠٬٠٠٠", status="للبيع", description="شاليه يرى البحر مباشرة بقرية مراسي الساحل الشمالي مساحة 120 متر مع رووف خاص.", lat=30.8250, lng=28.9500, image="https://images.unsplash.com/photo-1499793983690-e29da59ef1c2?w=600"),
        Property(title="مكتب إداري بالعاصمة", type="Commercial", location="العاصمة الإدارية", price="٢٬٨٠٠٬٠٠٠", status="للبيع", description="مكتب إداري في منطقة الأعمال المركزية بالعاصمة الإدارية بمساحة 60 متر، تشطيب كامل.", lat=30.0055, lng=31.7251, image="https://images.unsplash.com/photo-1497366216548-37526070297c?w=600"),
        Property(title="شقة بكمبوند زد", type="Apartment", location="الشيخ زايد", price="٧٬٠٠٠٬٠٠٠", status="للبيع", description="شقة فاخرة جداً في أبراج زد الشيخ زايد، إطلالة على البارك، مساحة 160 متر.", lat=30.0469, lng=30.9850, image="https://images.unsplash.com/photo-1545324418-cc1a3fa10c00?w=600"),
        Property(title="توين هاوس بالجونة", type="Townhouse", location="الجونة", price="١٥٬٠٠٠٬٠٠٠", status="للبيع", description="توين هاوس على اللاجون في الجونة بتشطيب كامل، جاهز للتسليم.", lat=27.3942, lng=33.6783, image="https://images.unsplash.com/photo-1564013799919-ab600027ffc6?w=600"),
        Property(title="شقة للإيجار بالمعادي", type="Apartment", location="المعادي", price="٢٥٬٠٠٠", status="للإيجار", description="شقة مفروشة بالكامل تطل على النيل بالمعادي، غرفتين نوم.", lat=29.9538, lng=31.2585, image="https://images.unsplash.com/photo-1502672260266-1c1de2d9d0cb?w=600")
    ]
    for p in dummy_properties:
        collection.add(
            documents=[f"{p.title}. A {p.type} located in {p.location}. {p.description}. Price: {p.price}."],
            metadatas=[p.dict()],
            ids=[str(uuid.uuid4())]
        )

@app.post("/api/scrape")
async def scrape_and_ingest(background_tasks: BackgroundTasks):
    """
    Triggers live scraping from all 3 sources and ingests results into ChromaDB.
    """
    def _run():
        properties = scrape_all()
        for p in properties:
            try:
                collection.add(
                    documents=[f"{p['title']}. A {p['type']} located in {p['location']}. {p['description']}. Price: {p['price']}."],
                    metadatas=[p],
                    ids=[str(uuid.uuid4())]
                )
            except Exception as e:
                print(f"[ingest error] {e}")
        print(f"[+] Ingested {len(properties)} live properties into ChromaDB")
    background_tasks.add_task(_run)
    return {"message": "Scraping started in background"}


@app.get("/api/search")
async def semantic_search(q: str):
    """
    Intelligent semantic search using ChromaDB.
    """
    try:
        results = collection.query(
            query_texts=[q],
            n_results=20
        )
        
        matches = []
        if results['metadatas']:
            for i, meta in enumerate(results['metadatas'][0]):
                distance = results['distances'][0][i]
                match_score = max(50, int(100 - (distance * 30)))
                meta['matchScore'] = match_score
                matches.append(meta)
                
        matches.sort(key=lambda x: x['matchScore'], reverse=True)
        return {"properties": matches}
    except Exception as e:
        print(f"[Search Error] {e}")
        return {"properties": []}


@app.get("/api/properties")
async def get_all_properties(limit: int = 50, offset: int = 0):
    count = collection.count()
    if count == 0:
        ingest_dummy_data()
    results = collection.get(limit=offset + limit, include=["metadatas"])
    properties = []
    for meta in results["metadatas"][offset:]:
        if not meta.get("matchScore"):
            meta["matchScore"] = 75
        properties.append(meta)
    return {"properties": properties, "total": len(properties)}


# Run with: uvicorn main:app --reload

import os
# Mount frontend (works locally and in Docker)
frontend_path = os.path.join(os.path.dirname(__file__), "../front")
app.mount("/", StaticFiles(directory=frontend_path, html=True), name="front")