File size: 4,886 Bytes
59b93a7
 
3f5ff7f
59b93a7
 
 
 
 
771f178
59b93a7
 
771f178
59b93a7
 
 
 
 
 
 
 
 
 
 
 
771f178
59b93a7
 
 
 
 
 
 
 
771f178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// api.js

const API_BASE_URL = '';

export async function fetchRecommendations(query) {
    try {
        const response = await fetch(`${API_BASE_URL}/api/recommend`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(query)
        });
        if (!response.ok) throw new Error(`API error: ${response.status}`);
        const data = await response.json();
        return data.recommendations || [];
    } catch (error) {
        console.error("Failed to fetch recommendations:", error);
        return [];
    }
}

export async function ingestProperty(property) {
    try {
        const response = await fetch(`${API_BASE_URL}/api/ingest`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(property)
        });
        return await response.json();
    } catch (error) {
        console.error("Failed to ingest property:", error);
        return null;
    }
}

export async function logInteraction(propertyId, interactionType) {
    try {
        const token = localStorage.getItem('darak_token') || 'guest';
        const response = await fetch(`${API_BASE_URL}/api/interact`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                user_id: token,
                property_id: propertyId,
                interaction_type: interactionType
            })
        });
        return await response.json();
    } catch (error) {
        console.error("Failed to log interaction:", error);
        return null;
    }
}

export async function fetchAllProperties(limit = 50, offset = 0) {
    try {
        const response = await fetch(`${API_BASE_URL}/api/properties?limit=${limit}&offset=${offset}`);
        if (!response.ok) throw new Error(`API error: ${response.status}`);
        const data = await response.json();
        return data.properties || [];
    } catch (error) {
        console.error("Failed to fetch all properties:", error);
        return [];
    }
}

export async function semanticSearch(query) {
    try {
        const safeQuery = String(query).trim().slice(0, 200);
        if (!safeQuery) return [];
        const response = await fetch(`${API_BASE_URL}/api/search?q=${encodeURIComponent(safeQuery)}`);
        if (!response.ok) throw new Error(`API error: ${response.status}`);
        const data = await response.json();
        return data.properties || [];
    } catch (error) {
        console.error("Failed to search properties:", error);
        return [];
    }
}

export async function triggerScraping() {
    const ALLOWED_SCRAPE_URL = `${API_BASE_URL}/api/scrape`;
    try {
        const response = await fetch(ALLOWED_SCRAPE_URL, { method: 'POST' });
        if (!response.ok) throw new Error(`API error: ${response.status}`);
        return await response.json();
    } catch (error) {
        console.error("Failed to trigger scraping:", error);
        return { status: "error", message: error.message };
    }
}

export async function fetchChatResponse(message, fileAttachment = null) {
    try {
        const token = localStorage.getItem('darak_token') || 'guest';
        const payload = { message: message, user_id: token };
        if (fileAttachment) {
            payload.file_data = fileAttachment.base64;
            payload.file_name = fileAttachment.name;
            payload.file_type = fileAttachment.type;
        }
        const response = await fetch(`${API_BASE_URL}/api/chat`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(payload)
        });
        const data = await response.json();
        return data.reply || "عذراً، لم أتمكن من الرد.";
    } catch (error) {
        console.error("Failed to fetch chat response:", error);
        return "عذراً، حدث خطأ في الاتصال. يرجى المحاولة لاحقاً.";
    }
}

export async function loginUser(username, password) {
    const response = await fetch(`${API_BASE_URL}/api/auth/login`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ username, password })
    });
    if (!response.ok) throw new Error("فشل تسجيل الدخول. يرجى التحقق من بياناتك.");
    return await response.json();
}

export async function registerUser(username, password) {
    const response = await fetch(`${API_BASE_URL}/api/auth/register`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ username, password })
    });
    if (!response.ok) throw new Error("اسم المستخدم موجود بالفعل. اختر اسماً آخر.");
    return await response.json();
}