from __future__ import annotations


from models.calendars import Calendar
from ...calendars.services.calendars import CalendarError

async def update_calendar_config(sess, calendar_id: int, *, description: str | None, slots: list | None):
    """Update description and slots for a calendar."""
    cal = await sess.get(Calendar, calendar_id)
    if not cal:
        raise CalendarError(f"Calendar {calendar_id} not found.")
    cal.description = (description or '').strip() or None
    # Validate slots shape a bit
    norm_slots: list[dict] = []
    if slots:
        for s in slots:
            if not isinstance(s, dict):
                continue
            norm_slots.append({
                "days": str(s.get("days", ""))[:7].lower(),
                "time_from": str(s.get("time_from", ""))[:5],
                "time_to": str(s.get("time_to", ""))[:5],
                "cost_name": (s.get("cost_name") or "")[:64],
                "description": (s.get("description") or "")[:255],
            })
    cal.slots = norm_slots or None
    await sess.flush()
    return cal
