const { pool } = require("../modules/database") const { DefaultError } = require("../errors/errors") async function findPlatformsByStation(stationId) { try { const sql = "SELECT * FROM platforms WHERE stationId = ?" return await pool.query(sql, [stationId]) } catch (error) { throw new DefaultError(500, "Internal Server Error", error) } } async function findPlatformById(platformId) { try { const sql = "SELECT * FROM platforms WHERE id = ?" const rows = await pool.query(sql, [platformId]) return rows[0] || null } catch (error) { throw new DefaultError(500, "Internal Server Error", error) } } // cœur de la validation : quai le plus proche desservant CETTE ligne async function findNearestPlatformForLine(lineId, latitude, longitude, radiusM) { try { const sql = ` SELECT p.*, ST_Distance_Sphere(p.location, ST_SRID(POINT(?, ?), 4326)) AS distance FROM platforms p JOIN platform_lines pl ON pl.platformId = p.id WHERE pl.lineId = ? HAVING distance <= ? ORDER BY distance LIMIT 1 ` const rows = await pool.query(sql, [longitude, latitude, lineId, radiusM]) return rows[0] || null } catch (error) { throw new DefaultError(500, "Internal Server Error", error) } } async function createPlatform(stationId, name, direction, latitude, longitude, isPRM, hasElevator) { try { const sql = ` INSERT INTO platforms (stationId, name, direction, latitude, longitude, isPRM, hasElevator) VALUES (?, ?, ?, ?, ?, ?, ?) ` const rows = await pool.query(sql, [ stationId, name, direction, latitude, longitude, longitude, latitude, isPRM, hasElevator ]) return rows.insertId } catch (error) { throw new DefaultError(500, "Internal Server Error", error) } } async function updatePlatformAccessibility(platformId, isPRM, hasElevator) { try { const sql = "UPDATE platforms SET isPRM = ?, hasElevator = ? WHERE id = ?" const rows = await pool.query(sql, [isPRM, hasElevator, platformId]) return rows.affectedRows > 0 } catch (error) { throw new DefaultError(500, "Internal Server Error", error) } } async function deletePlatform(platformId) { try { const sql = "DELETE FROM platforms WHERE id = ?" const rows = await pool.query(sql, [platformId]) return rows.affectedRows > 0 } catch (error) { throw new DefaultError(500, "Internal Server Error", error) } } module.exports = { findPlatformsByStation, findPlatformById, findNearestPlatformForLine, createPlatform, updatePlatformAccessibility, deletePlatform }