"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var MapRefinementService_1; var _a, _b, _c, _d, _e, _f; Object.defineProperty(exports, "__esModule", { value: true }); exports.MapRefinementService = void 0; const common_1 = require("@nestjs/common"); const typeorm_1 = require("@nestjs/typeorm"); const typeorm_2 = require("typeorm"); const map_candidate_entity_1 = require("./entities/map-candidate.entity"); const place_jordan_entity_1 = require("./entities/place-jordan.entity"); const place_syria_entity_1 = require("./entities/place-syria.entity"); const place_egypt_entity_1 = require("./entities/place-egypt.entity"); const place_iraq_entity_1 = require("./entities/place-iraq.entity"); let MapRefinementService = MapRefinementService_1 = class MapRefinementService { dataSource; candidateRepository; jordanRepository; syriaRepository; egyptRepository; iraqRepository; logger = new common_1.Logger(MapRefinementService_1.name); constructor(dataSource, candidateRepository, jordanRepository, syriaRepository, egyptRepository, iraqRepository) { this.dataSource = dataSource; this.candidateRepository = candidateRepository; this.jordanRepository = jordanRepository; this.syriaRepository = syriaRepository; this.egyptRepository = egyptRepository; this.iraqRepository = iraqRepository; } async suggestPlace(dto, submittedBy) { const lat = parseFloat(dto.lat || dto.latitude); const lng = parseFloat(dto.lng || dto.longitude); const country = (dto.country || 'JORDAN').toUpperCase(); if (isNaN(lat) || isNaN(lng)) { throw new Error('Invalid coordinates: Latitude and Longitude must be numbers'); } this.logger.log(`📍 Suggesting place: ${dto.name} at [${lat}, ${lng}]. Performing spatial enrichment...`); const enrichment = await this.dataSource.query(` SELECT g.id as gov_id, g.name_ar as gov_name, d.id as dist_id, d.name_ar as dist_name, s.id as sub_id, s.name_ar as sub_name, n.id as neigh_id, n.name_ar as neigh_name FROM (SELECT ST_SetSRID(ST_MakePoint($1, $2), 4326) as p) p LEFT JOIN admin_boundaries g ON g.admin_level = 4 AND ST_Within(p.p::geometry, g.geom::geometry) LEFT JOIN admin_boundaries d ON d.admin_level = 6 AND ST_Within(p.p::geometry, d.geom::geometry) LEFT JOIN admin_boundaries s ON s.admin_level = 8 AND ST_Within(p.p::geometry, s.geom::geometry) LEFT JOIN neighborhood_polygons n ON ST_Within(p.p::geometry, n.geometry::geometry) LIMIT 1 `, [lng, lat]); const spatialData = enrichment[0] || {}; const isArabic = (text) => /[\u0600-\u06FF]/.test(text); const nameAr = dto.name_ar || (isArabic(dto.name) ? dto.name : null); const nameEn = dto.name_en || (!isArabic(dto.name) ? dto.name : null); const candidate = this.candidateRepository.create({ name: dto.name, name_ar: nameAr, name_en: nameEn, category: dto.category, address: dto.address, latitude: lat, longitude: lng, submittedBy, status: map_candidate_entity_1.CandidateStatus.PENDING, country: country, location: { type: 'Point', coordinates: [lng, lat], }, governorate_id: spatialData.gov_id, district_id: spatialData.dist_id, sub_district_id: spatialData.sub_id, neighborhood_id: spatialData.neigh_id }); const saved = await this.candidateRepository.save(candidate); this.logger.log(`✅ Success: Candidate ${saved.id} enriched with Gov:${saved.governorate_id}, Neigh:${spatialData.neigh_id}`); this.runExternalVerification(saved.id).catch(err => this.logger.error(`❌ Verification failed for ${saved.id}: ${err.message}`)); return { ...saved, governorate_name: spatialData.gov_name, district_name: spatialData.dist_name, neighborhood_name: spatialData.neigh_name, location_wkt: `POINT(${lng} ${lat})` }; } async runExternalVerification(candidateId) { const candidate = await this.candidateRepository.findOne({ where: { id: candidateId } }); if (!candidate) return; this.logger.log(`🔍 Starting automated verification for candidate ${candidate.id}...`); try { const axios = require('axios'); const searchUrl = `https://duckduckgo.com/html/?q=${candidate.latitude},${candidate.longitude}`; const response = await axios.get(searchUrl, { headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' }, timeout: 10000 }); const html = response.data; const titleMatch = html.match(/