File size: 2,060 Bytes
0dd2082
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const { getDb } = require('../db/database');
const { success } = require('../utils/response');
const AppError = require('../utils/AppError');
const aiService = require('../services/ai.service');

async function getSuggestions(req, res, next) {
    try {
        const { q } = req.query;

        if (!q || q.trim().length < 2) {
            return success(res, { suggestions: [] });
        }

        const lower = q.toLowerCase().trim();
        const db = await getDb();

        // 1. Fetch real matches from the local database
        const rows = await db.all(
            'SELECT id, name, category FROM places WHERE name LIKE ? LIMIT 5',
            [`%${lower}%`]
        );

        const dbMatches = rows.map(r => ({
            type: 'place',
            text: r.name,
            category: r.category,
            id: r.id
        }));

        // 2. Supplement with AI Semantic Suggestions
        const aiSuggestions = await aiService.generateSemanticSuggestions(q);

        // Combine them: Database hits first (definite matches), then AI suggestions
        const suggestions = [...dbMatches, ...aiSuggestions].slice(0, 8);

        success(res, { suggestions });
    } catch (err) {
        next(err);
    }
}

async function getPlaceById(req, res, next) {
    try {
        const { id } = req.params;
        const db = await getDb();

        const row = await db.get('SELECT * FROM places WHERE id = ?', [id]);

        if (!row) {
            throw new AppError('Place not found in records', 404);
        }

        const place = {
            id: row.id,
            name: row.name,
            category: row.category,
            address: row.address,
            rating: row.rating,
            priceRange: row.priceRange,
            features: JSON.parse(row.features || '[]'),
            reviews: JSON.parse(row.reviews || '[]'),
            coordinates: { lat: row.lat, lon: row.lon }
        };

        success(res, { place });
    } catch (err) {
        next(err);
    }
}

module.exports = { getSuggestions, getPlaceById };