""" Router patch for /api/events-dashboard endpoint. Adds conflict detection and dynamic day labels to event responses. """ from fastapi import APIRouter, HTTPException from datetime import datetime from typing import List, Dict, Any import json import os # Import the conflict detection module # Note: In production, adjust the import path as needed from event_conflict import ( get_day_label, find_conflicts, add_conflict_data, add_conflicts_to_all_events ) router = APIRouter() # Path to test events - adjust as needed for production EVENTS_DATA_PATH = os.path.join( os.path.dirname(__file__), "..", "..", "workspace-socrates", "hoffdesk-api", "data", "test_events.json" ) def load_events() -> List[Dict[str, Any]]: """Load events from JSON file.""" try: with open(EVENTS_DATA_PATH, "r") as f: return json.load(f) except FileNotFoundError: return [] except json.JSONDecodeError: return [] def save_events(events: List[Dict[str, Any]]) -> None: """Save events to JSON file.""" os.makedirs(os.path.dirname(EVENTS_DATA_PATH), exist_ok=True) with open(EVENTS_DATA_PATH, "w") as f: json.dump(events, f, indent=2) @router.get("/api/events-dashboard") async def get_events_dashboard() -> Dict[str, Any]: """ Get all events with conflict detection and dynamic day labels. Returns: { "events": [ { "id": str, "title": str, "start": str (YYYY-MM-DD), "day_label": str ("Today", "Tomorrow", or "Mon May 05"), "status": str, "conflicts": [ { "id": str, "title": str, "family_members": [str], "conflict_type": str ("person" | "same_day") } ] } ], "count": int } """ events = load_events() # Enrich all events with conflicts and dynamic day labels enriched_events = add_conflicts_to_all_events(events) return { "events": enriched_events, "count": len(enriched_events) } @router.get("/api/events/{event_id}") async def get_event(event_id: str) -> Dict[str, Any]: """ Get a single event with conflict data and dynamic day label. """ events = load_events() event = next((e for e in events if e.get("id") == event_id), None) if not event: raise HTTPException(status_code=404, detail="Event not found") enriched = add_conflict_data(event, events) return enriched @router.patch("/api/events/{event_id}") async def update_event(event_id: str, update_data: Dict[str, Any]) -> Dict[str, Any]: """ Update an event and return it with fresh conflict data. """ events = load_events() event_idx = next((i for i, e in enumerate(events) if e.get("id") == event_id), None) if event_idx is None: raise HTTPException(status_code=404, detail="Event not found") # Update the event events[event_idx].update(update_data) events[event_idx]["updated_at"] = datetime.now().isoformat() # Save changes save_events(events) # Return with conflict data enriched = add_conflict_data(events[event_idx], events) return enriched # Example integration for existing FastAPI app: # # from fastapi import FastAPI # from router_patch import router as events_router # # app = FastAPI() # app.include_router(events_router) # # # Or add to existing router: # existing_router.include_router(events_router)