from __future__ import annotations
from datetime import datetime, timezone, date, timedelta

from quart import (
    request, make_response, Blueprint, g, abort, session as qsession
)

from bp.calendar.services import get_visible_entries_for_period

from bp.calendar_entries.routes import register as register_calendar_entries
from .admin.routes import register as register_admin

from shared.browser.app.redis_cacher import cache_page
from shared.infrastructure.fragments import fetch_fragment

from models.calendars import CalendarSlot  # add this import

from sqlalchemy import select

from shared.browser.app.utils.htmx import is_htmx_request
from shared.sx.helpers import sx_response


def register():
    bp = Blueprint("day", __name__, url_prefix='/day/<int:year>/<int:month>/<int:day>')

    bp.register_blueprint(
        register_calendar_entries()
    )
    bp.register_blueprint(
        register_admin()
    )

    @bp.context_processor
    async def inject_root():
        view_args = getattr(request, "view_args", {}) or {}
        day = view_args.get("day")
        month = view_args.get("month")
        year = view_args.get("year")

        calendar = getattr(g, "calendar", None)
        if not calendar:
            return {}

        try:
            day_date = date(year, month, day)
        except (ValueError, TypeError):
            return {}

        # Period: this day only
        period_start = datetime(year, month, day, tzinfo=timezone.utc)
        period_end = period_start + timedelta(days=1)

        # Identity & admin flag
        user = getattr(g, "user", None)
        session_id = qsession.get("calendar_sid")

        visible = await get_visible_entries_for_period(
            sess=g.s,
            calendar_id=calendar.id,
            period_start=period_start,
            period_end=period_end,
            user=user,
            session_id=session_id,
        )

        # --- NEW: slots for this weekday ---
        weekday_attr = ["mon","tue","wed","thu","fri","sat","sun"][day_date.weekday()]

        stmt = (
            select(CalendarSlot)
            .where(
                CalendarSlot.calendar_id == calendar.id,
                getattr(CalendarSlot, weekday_attr) == True,  # noqa: E712
                CalendarSlot.deleted_at.is_(None),
            )
            .order_by(CalendarSlot.time_start.asc(), CalendarSlot.id.asc())
        )
        result = await g.s.execute(stmt)
        day_slots = list(result.scalars())

        # Fetch container nav from relations (exclude calendar — we're on a calendar page)
        container_nav = ""
        post_data = getattr(g, "post_data", None)
        if post_data:
            post_id = post_data["post"]["id"]
            post_slug = post_data["post"]["slug"]
            container_nav = await fetch_fragment("relations", "container-nav", params={
                "container_type": "page",
                "container_id": str(post_id),
                "post_slug": post_slug,
                "exclude": "page->calendar",
            })

        return {
            "qsession": qsession,
            "day_date": day_date,
            "day": day,
            "year": year,
            "month": month,
            "day_entries": visible.merged_entries,
            "user_entries": visible.user_entries,
            "confirmed_entries": visible.confirmed_entries,
            "day_slots": day_slots,
            "container_nav": container_nav,
        }


    
    @bp.get("/")
    @cache_page(tag="calendars")
    async def show_day(year: int, month: int, day: int, **kwargs):
        """
        Show a detail view for a single calendar day.

        Visibility rules:
        - Non-admin:
            - all *confirmed* entries for that day (any user)
            - all entries for current user/session (any state) for that day
              (pending/ordered/provisional/confirmed)
        - Admin:
            - all confirmed + provisional + ordered entries for that day (all users)
            - pending only for current user/session
        """
        from shared.sx.page import get_template_context
        from sxc.pages.renders import render_day_page, render_day_oob

        tctx = await get_template_context()
        if not is_htmx_request():
            html = await render_day_page(tctx)
            return await make_response(html)
        else:
            sx_src = await render_day_oob(tctx)
            return sx_response(sx_src)

    @bp.get("/w/<widget_domain>/")
    async def widget_paginate(widget_domain: str, **kwargs):
        """Proxies paginated widget requests to the appropriate fragment provider."""
        page = int(request.args.get("page", 1))
        post_data = getattr(g, "post_data", None)
        if not post_data:
            abort(404)
        post_id = post_data["post"]["id"]
        post_slug = post_data["post"]["slug"]

        if widget_domain == "market":
            html = await fetch_fragment("relations", "container-nav", params={
                "container_type": "page",
                "container_id": str(post_id),
                "post_slug": post_slug,
                "exclude": "page->calendar",
            })
            return await make_response(html or "")
        abort(404)

    return bp
