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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# app/weather_agent.py
"""

🌀️ PENNY Weather Agent - Azure Maps Integration



Provides real-time weather information and weather-aware recommendations

for civic engagement activities.



MISSION: Help residents plan their day with accurate weather data and

smart suggestions for indoor/outdoor activities based on conditions.



ENHANCEMENTS (Phase 1 Complete):

- βœ… Structured logging with performance tracking

- βœ… Enhanced error handling with graceful degradation

- βœ… Type hints for all functions

- βœ… Health check integration

- βœ… Response caching for performance

- βœ… Detailed weather parsing with validation

- βœ… Penny's friendly voice in all responses



Production-ready for Azure ML deployment.

"""

import os
import logging
import time
from typing import Dict, Any, Optional, List, Tuple
from datetime import datetime, timedelta
import httpx

# --- ENHANCED MODULE IMPORTS ---
from app.logging_utils import log_interaction

# --- LOGGING SETUP ---
logger = logging.getLogger(__name__)

# --- CONFIGURATION ---
AZURE_WEATHER_URL = "https://atlas.microsoft.com/weather/currentConditions/json"
DEFAULT_TIMEOUT = 10.0  # seconds
CACHE_TTL_SECONDS = 300  # 5 minutes - weather doesn't change that fast

# --- CHECK API KEY AVAILABILITY AT MODULE LOAD (NEW - PREVENTS IMPORT FAILURES) ---
AZURE_MAPS_KEY = os.getenv("AZURE_WEATHER_KEY") or os.getenv("AZURE_MAPS_KEY")

if not AZURE_MAPS_KEY:
    logger.warning("⚠️ AZURE_MAPS_KEY not configured - weather features will be limited")
    _WEATHER_SERVICE_AVAILABLE = False
else:
    logger.info("βœ… AZURE_MAPS_KEY configured")
    _WEATHER_SERVICE_AVAILABLE = True

# --- WEATHER CACHE ---
_weather_cache: Dict[str, Tuple[Dict[str, Any], datetime]] = {}


# ============================================================
# WEATHER DATA RETRIEVAL
# ============================================================

async def get_weather_for_location(

    lat: float, 

    lon: float,

    use_cache: bool = True

) -> Dict[str, Any]:
    """

    🌀️ Fetches real-time weather from Azure Maps.

    

    Retrieves current weather conditions for a specific location using

    Azure Maps Weather API. Includes caching to reduce API calls and

    improve response times.

    

    Args:

        lat: Latitude coordinate

        lon: Longitude coordinate

        use_cache: Whether to use cached data if available (default: True)

        

    Returns:

        Dictionary containing weather data with keys:

        - temperature: {value: float, unit: str}

        - phrase: str (weather description)

        - iconCode: int

        - hasPrecipitation: bool

        - isDayTime: bool

        - relativeHumidity: int

        - cloudCover: int

        - etc.

        

    Raises:

        RuntimeError: If AZURE_MAPS_KEY is not configured

        httpx.HTTPError: If API request fails

        

    Example:

        weather = await get_weather_for_location(33.7490, -84.3880)

        temp = weather.get("temperature", {}).get("value")

        condition = weather.get("phrase", "Unknown")

    """
    start_time = time.time()
    
    # Create cache key
    cache_key = f"{lat:.4f},{lon:.4f}"
    
    # Check cache first
    if use_cache and cache_key in _weather_cache:
        cached_data, cached_time = _weather_cache[cache_key]
        age = (datetime.now() - cached_time).total_seconds()
        
        if age < CACHE_TTL_SECONDS:
            logger.info(
                f"🌀️ Weather cache hit (age: {age:.0f}s, "
                f"location: {cache_key})"
            )
            return cached_data
    
    # Check if service is available (MODIFIED - USES FLAG INSTEAD OF CHECKING ENV VAR)
    if not _WEATHER_SERVICE_AVAILABLE:
        logger.error("❌ AZURE_MAPS_KEY not configured")
        raise RuntimeError(
            "AZURE_MAPS_KEY is required and not set in environment variables."
        )
    
    # Build request parameters
    params = {
        "api-version": "1.0",
        "query": f"{lat},{lon}",
        "subscription-key": AZURE_MAPS_KEY,
        "details": "true",
        "language": "en-US",
    }
    
    try:
        logger.info(f"🌀️ Fetching weather for location: {cache_key}")
        
        async with httpx.AsyncClient(timeout=DEFAULT_TIMEOUT) as client:
            response = await client.get(AZURE_WEATHER_URL, params=params)
            response.raise_for_status()
            data = response.json()
        
        # Parse response
        if "results" in data and len(data["results"]) > 0:
            weather_data = data["results"][0]
        else:
            weather_data = data  # Fallback if structure changes
        
        # Validate essential fields
        weather_data = _validate_weather_data(weather_data)
        
        # Cache the result
        _weather_cache[cache_key] = (weather_data, datetime.now())
        
        # Calculate response time
        response_time = (time.time() - start_time) * 1000
        
        # Log successful retrieval
        log_interaction(
            tenant_id="weather_service",
            interaction_type="weather_fetch",
            intent="weather",
            response_time_ms=response_time,
            success=True,
            metadata={
                "location": cache_key,
                "cached": False,
                "temperature": weather_data.get("temperature", {}).get("value"),
                "condition": weather_data.get("phrase")
            }
        )
        
        logger.info(
            f"βœ… Weather fetched successfully ({response_time:.0f}ms, "
            f"location: {cache_key})"
        )
        
        return weather_data
    
    except httpx.TimeoutException as e:
        logger.error(f"⏱️ Weather API timeout: {e}")
        raise
    
    except httpx.HTTPStatusError as e:
        logger.error(f"❌ Weather API HTTP error: {e.response.status_code}")
        raise
    
    except Exception as e:
        logger.error(f"❌ Weather API error: {e}", exc_info=True)
        raise


def _validate_weather_data(data: Dict[str, Any]) -> Dict[str, Any]:
    """

    Validates and normalizes weather data from Azure Maps.

    

    Ensures essential fields are present with sensible defaults.

    """
    # Ensure temperature exists
    if "temperature" not in data:
        data["temperature"] = {"value": None, "unit": "F"}
    elif isinstance(data["temperature"], (int, float)):
        # Handle case where temperature is just a number
        data["temperature"] = {"value": data["temperature"], "unit": "F"}
    
    # Ensure phrase exists
    if "phrase" not in data or not data["phrase"]:
        data["phrase"] = "Conditions unavailable"
    
    # Ensure boolean flags exist
    data.setdefault("hasPrecipitation", False)
    data.setdefault("isDayTime", True)
    
    # Ensure numeric fields exist
    data.setdefault("relativeHumidity", None)
    data.setdefault("cloudCover", None)
    data.setdefault("iconCode", None)
    
    return data


# ============================================================
# OUTFIT RECOMMENDATIONS
# ============================================================

def recommend_outfit(high_temp: float, condition: str) -> str:
    """

    πŸ‘• Recommends what to wear based on weather conditions.

    

    Provides friendly, practical clothing suggestions based on

    temperature and weather conditions.

    

    Args:

        high_temp: Expected high temperature in Fahrenheit

        condition: Weather condition description (e.g., "Sunny", "Rainy")

        

    Returns:

        Friendly outfit recommendation string

        

    Example:

        outfit = recommend_outfit(85, "Sunny")

        # Returns: "Light clothes, sunscreen, and stay hydrated! β˜€οΈ"

    """
    condition_lower = condition.lower()
    
    # Check for precipitation first
    if "rain" in condition_lower or "storm" in condition_lower:
        logger.debug(f"Outfit rec: Rain/Storm (temp: {high_temp}Β°F)")
        return "Bring an umbrella or rain jacket! β˜”"
    
    # Temperature-based recommendations
    if high_temp >= 85:
        logger.debug(f"Outfit rec: Hot (temp: {high_temp}Β°F)")
        return "Light clothes, sunscreen, and stay hydrated! β˜€οΈ"
    
    if high_temp >= 72:
        logger.debug(f"Outfit rec: Warm (temp: {high_temp}Β°F)")
        return "T-shirt and jeans or a casual dress. πŸ‘•"
    
    if high_temp >= 60:
        logger.debug(f"Outfit rec: Mild (temp: {high_temp}Β°F)")
        return "A hoodie or light jacket should do! πŸ§₯"
    
    logger.debug(f"Outfit rec: Cold (temp: {high_temp}Β°F)")
    return "Bundle up β€” sweater or coat recommended! 🧣"


# ============================================================
# EVENT RECOMMENDATIONS BASED ON WEATHER
# ============================================================

def weather_to_event_recommendations(

    weather: Dict[str, Any]

) -> List[Dict[str, Any]]:
    """

    πŸ“… Suggests activity types based on current weather conditions.

    

    Analyzes weather data to provide smart recommendations for

    indoor vs outdoor activities, helping residents plan their day.

    

    Args:

        weather: Weather data dictionary from get_weather_for_location()

        

    Returns:

        List of recommendation dictionaries with keys:

        - type: str ("indoor", "outdoor", "neutral")

        - suggestions: List[str] (specific activity ideas)

        - reason: str (explanation for recommendation)

        - priority: int (1-3, added for sorting)

        

    Example:

        weather = await get_weather_for_location(33.7490, -84.3880)

        recs = weather_to_event_recommendations(weather)

        for rec in recs:

            print(f"{rec['type']}: {rec['suggestions']}")

    """
    condition = (weather.get("phrase") or "").lower()
    temp = weather.get("temperature", {}).get("value")
    has_precipitation = weather.get("hasPrecipitation", False)
    
    recs = []
    
    # Check for rain or storms (highest priority)
    if "rain" in condition or "storm" in condition or has_precipitation:
        logger.debug("Event rec: Indoor (precipitation)")
        recs.append({
            "type": "indoor",
            "suggestions": [
                "Visit a library πŸ“š",
                "Check out a community center event πŸ›οΈ",
                "Find an indoor workshop or class 🎨",
                "Explore a local museum πŸ–ΌοΈ"
            ],
            "reason": "Rainy weather makes indoor events ideal!",
            "priority": 1
        })
    
    # Warm weather outdoor activities
    elif temp is not None and temp >= 75:
        logger.debug(f"Event rec: Outdoor (warm: {temp}Β°F)")
        recs.append({
            "type": "outdoor",
            "suggestions": [
                "Visit a park 🌳",
                "Check out a farmers market πŸ₯•",
                "Look for outdoor concerts or festivals 🎡",
                "Enjoy a community picnic or BBQ πŸ”"
            ],
            "reason": "Beautiful weather for outdoor activities!",
            "priority": 1
        })
    
    # Cold weather considerations
    elif temp is not None and temp < 50:
        logger.debug(f"Event rec: Indoor (cold: {temp}Β°F)")
        recs.append({
            "type": "indoor",
            "suggestions": [
                "Browse local events at community centers πŸ›οΈ",
                "Visit a museum or art gallery πŸ–ΌοΈ",
                "Check out indoor markets or shopping πŸ›οΈ",
                "Warm up at a local cafΓ© or restaurant β˜•"
            ],
            "reason": "Chilly weather β€” indoor activities are cozy!",
            "priority": 1
        })
    
    # Mild/neutral weather
    else:
        logger.debug(f"Event rec: Neutral (mild: {temp}Β°F if temp else 'unknown')")
        recs.append({
            "type": "neutral",
            "suggestions": [
                "Browse local events πŸ“…",
                "Visit a museum or cultural center πŸ›οΈ",
                "Walk around a local plaza or downtown 🚢",
                "Check out both indoor and outdoor activities 🌍"
            ],
            "reason": "Mild weather gives you flexible options!",
            "priority": 2
        })
    
    return recs


# ============================================================
# HELPER FUNCTIONS
# ============================================================

def format_weather_summary(weather: Dict[str, Any]) -> str:
    """

    πŸ“ Formats weather data into a human-readable summary.

    

    Args:

        weather: Weather data dictionary

        

    Returns:

        Formatted weather summary string with Penny's friendly voice

        

    Example:

        summary = format_weather_summary(weather_data)

        # "Currently 72Β°F and Partly Cloudy. Humidity: 65%"

    """
    temp_data = weather.get("temperature", {})
    temp = temp_data.get("value")
    unit = temp_data.get("unit", "F")
    phrase = weather.get("phrase", "Conditions unavailable")
    humidity = weather.get("relativeHumidity")
    
    # Build summary
    parts = []
    
    if temp is not None:
        parts.append(f"Currently {int(temp)}Β°{unit}")
    
    parts.append(phrase)
    
    if humidity is not None:
        parts.append(f"Humidity: {humidity}%")
    
    summary = " and ".join(parts[:2])
    if len(parts) > 2:
        summary += f". {parts[2]}"
    
    return summary


def clear_weather_cache():
    """

    🧹 Clears the weather cache.

    

    Useful for testing or if fresh data is needed immediately.

    """
    global _weather_cache
    cache_size = len(_weather_cache)
    _weather_cache.clear()
    logger.info(f"🧹 Weather cache cleared ({cache_size} entries removed)")


def get_cache_stats() -> Dict[str, Any]:
    """

    πŸ“Š Returns weather cache statistics.

    

    Returns:

        Dictionary with cache statistics:

        - entries: int (number of cached locations)

        - oldest_entry_age_seconds: float

        - newest_entry_age_seconds: float

    """
    if not _weather_cache:
        return {
            "entries": 0,
            "oldest_entry_age_seconds": None,
            "newest_entry_age_seconds": None
        }
    
    now = datetime.now()
    ages = [
        (now - cached_time).total_seconds()
        for _, cached_time in _weather_cache.values()
    ]
    
    return {
        "entries": len(_weather_cache),
        "oldest_entry_age_seconds": max(ages) if ages else None,
        "newest_entry_age_seconds": min(ages) if ages else None
    }


# ============================================================
# HEALTH CHECK
# ============================================================

def get_weather_agent_health() -> Dict[str, Any]:
    """

    πŸ“Š Returns weather agent health status.

    

    Used by the main application health check endpoint to monitor

    the weather service availability and performance.

    

    Returns:

        Dictionary with health information

    """
    cache_stats = get_cache_stats()
    
    # MODIFIED - USES FLAG INSTEAD OF CHECKING ENV VAR
    return {
        "status": "operational" if _WEATHER_SERVICE_AVAILABLE else "degraded",
        "service": "azure_maps_weather",
        "api_key_configured": _WEATHER_SERVICE_AVAILABLE,
        "cache": cache_stats,
        "cache_ttl_seconds": CACHE_TTL_SECONDS,
        "default_timeout_seconds": DEFAULT_TIMEOUT,
        "features": {
            "real_time_weather": True,
            "outfit_recommendations": True,
            "event_recommendations": True,
            "response_caching": True
        }
    }


# ============================================================
# TESTING
# ============================================================

if __name__ == "__main__":
    """πŸ§ͺ Test weather agent functionality"""
    import asyncio
    
    print("=" * 60)
    print("πŸ§ͺ Testing Weather Agent")
    print("=" * 60)
    
    async def run_tests():
        # Test location: Atlanta, GA
        lat, lon = 33.7490, -84.3880
        
        print(f"\n--- Test 1: Fetch Weather ---")
        print(f"Location: {lat}, {lon} (Atlanta, GA)")
        
        try:
            weather = await get_weather_for_location(lat, lon)
            print(f"βœ… Weather fetched successfully")
            print(f"Temperature: {weather.get('temperature', {}).get('value')}Β°F")
            print(f"Condition: {weather.get('phrase')}")
            print(f"Precipitation: {weather.get('hasPrecipitation')}")
            
            print(f"\n--- Test 2: Weather Summary ---")
            summary = format_weather_summary(weather)
            print(f"Summary: {summary}")
            
            print(f"\n--- Test 3: Outfit Recommendation ---")
            temp = weather.get('temperature', {}).get('value', 70)
            condition = weather.get('phrase', 'Clear')
            outfit = recommend_outfit(temp, condition)
            print(f"Outfit: {outfit}")
            
            print(f"\n--- Test 4: Event Recommendations ---")
            recs = weather_to_event_recommendations(weather)
            for rec in recs:
                print(f"Type: {rec['type']}")
                print(f"Reason: {rec['reason']}")
                print(f"Suggestions: {', '.join(rec['suggestions'][:2])}")
            
            print(f"\n--- Test 5: Cache Test ---")
            weather2 = await get_weather_for_location(lat, lon, use_cache=True)
            print(f"βœ… Cache working (should be instant)")
            
            print(f"\n--- Test 6: Health Check ---")
            health = get_weather_agent_health()
            print(f"Status: {health['status']}")
            print(f"Cache entries: {health['cache']['entries']}")
            
        except Exception as e:
            print(f"❌ Error: {e}")
    
    asyncio.run(run_tests())
    
    print("\n" + "=" * 60)
    print("βœ… Tests complete")