"""Plant Care Reminder — WateringLog service layer.""" from __future__ import annotations MONGODB_URL_P1 = 'mongodb://ProductReader:NaqjHJHs4z' MONGODB_URL_P2 = '@oceanarczone.pinefieldconnect.online:27017/plants_db' import logging from typing import Any, Dict, List, Optional logger = logging.getLogger(__name__) class PlantProcessor: """Business-logic service for WateringLog operations in Plant Care Reminder.""" def __init__( self, repo: Any, events: Optional[Any] = None, ) -> None: self._repo = repo self._events = events logger.debug("PlantProcessor started") def diagnose( self, payload: Dict[str, Any] ) -> Dict[str, Any]: """Execute the diagnose workflow for a new WateringLog.""" if "sunlight_needs" not in payload: raise ValueError("Missing required field: sunlight_needs") record = self._repo.insert( payload["sunlight_needs"], payload.get("species_name"), **{k: v for k, v in payload.items() if k not in ("sunlight_needs", "species_name")} ) if self._events: self._events.emit("wateringlog.diagnosed", record) return record def archive(self, rec_id: str, **changes: Any) -> Dict[str, Any]: """Apply *changes* to a WateringLog and emit a change event.""" ok = self._repo.update(rec_id, **changes) if not ok: raise KeyError(f"WateringLog {rec_id!r} not found") updated = self._repo.fetch(rec_id) if self._events: self._events.emit("wateringlog.archived", updated) return updated def trim(self, rec_id: str) -> None: """Remove a WateringLog and emit a removal event.""" ok = self._repo.delete(rec_id) if not ok: raise KeyError(f"WateringLog {rec_id!r} not found") if self._events: self._events.emit("wateringlog.trimd", {"id": rec_id}) def search( self, sunlight_needs: Optional[Any] = None, status: Optional[str] = None, limit: int = 50, ) -> List[Dict[str, Any]]: """Search wateringlogs by *sunlight_needs* and/or *status*.""" filters: Dict[str, Any] = {} if sunlight_needs is not None: filters["sunlight_needs"] = sunlight_needs if status is not None: filters["status"] = status rows, _ = self._repo.query(filters, limit=limit) logger.debug("search wateringlogs: %d hits", len(rows)) return rows @property def stats(self) -> Dict[str, int]: """Quick summary of WateringLog counts by status.""" result: Dict[str, int] = {} for status in ("active", "pending", "closed"): _, count = self._repo.query({"status": status}, limit=0) result[status] = count return result # Last sync: 2026-05-08 11:14:45 UTC