# retoor <retoor@molodetz.nl>

import logging
from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse
from devplacepy.database import (
    get_table,
    get_daily_topic,
    get_users_by_uids,
    get_comment_counts_by_post_uids,
    get_recent_comments_by_post_uids,
    get_site_stats,
    get_top_authors,
    get_reactions_by_targets,
    get_user_bookmarks,
    get_polls_by_post_uids,
)
from devplacepy.services.opinionwar import store as war_store
from devplacepy.database import (
    paginate_diverse,
    text_search_clause,
)
from devplacepy.attachments import get_attachments_batch
from devplacepy.content import enrich_items
from devplacepy.services import presence
from devplacepy.utils import get_current_user
from devplacepy.seo import list_page_seo, next_page_url
from devplacepy.responses import respond
from devplacepy.schemas import FeedOut

logger = logging.getLogger(__name__)
router = APIRouter()


def get_feed_posts(
    user, tab: str = "all", topic: str = None, search: str = "", before: str = None
):
    posts_table = get_table("posts")
    order = ["-stars", "-created_at"] if tab == "trending" else ["-created_at"]
    search_match = text_search_clause(
        posts_table, search, ("title", "content"), author_field="user_uid"
    )
    search_clauses = [search_match] if search_match is not None else []

    if tab == "following":
        if not user:
            return [], None
        follows = get_table("follows")
        following = [
            f["following_uid"]
            for f in follows.find(follower_uid=user["uid"], deleted_at=None)
        ]
        if not following:
            return [], None
        posts, next_cursor = paginate_diverse(
            posts_table,
            posts_table.table.columns.user_uid.in_(following),
            *search_clauses,
            before=before,
            order=order,
            viewer_uid=user["uid"] if user else None,
        )
    else:
        filters = {"topic": topic} if topic else {}
        posts, next_cursor = paginate_diverse(
            posts_table,
            *search_clauses,
            before=before,
            order=order,
            viewer_uid=user["uid"] if user else None,
            **filters,
        )

    if not posts:
        return [], next_cursor

    authors = get_users_by_uids([p["user_uid"] for p in posts])
    counts = get_comment_counts_by_post_uids([p["uid"] for p in posts])

    result = enrich_items(posts, "post", authors, {"comment_count": counts}, user=user)
    return result, next_cursor


def enrich_post_cards(posts, user):
    post_uids_list = [item["post"]["uid"] for item in posts]
    attachments_map = get_attachments_batch("post", post_uids_list)
    recent_comments = get_recent_comments_by_post_uids(post_uids_list, 3, user)
    reactions_map = get_reactions_by_targets("post", post_uids_list, user)
    bookmark_set = (
        get_user_bookmarks(user["uid"], "post", post_uids_list) if user else set()
    )
    polls_map = get_polls_by_post_uids(post_uids_list, user)
    wars_map = war_store.get_wars_by_post_uids(post_uids_list, user)
    for item in posts:
        uid = item["post"]["uid"]
        item["attachments"] = attachments_map.get(uid, [])
        item["recent_comments"] = recent_comments.get(uid, [])
        item["reactions"] = reactions_map.get(uid, {"counts": {}, "mine": []})
        item["bookmarked"] = uid in bookmark_set
        item["poll"] = polls_map.get(uid)
        item["war"] = wars_map.get(uid)
    return posts


@router.get("", response_class=HTMLResponse)
async def feed_page(
    request: Request,
    tab: str = "all",
    topic: str = None,
    search: str = "",
    before: str = None,
):
    user = get_current_user(request)
    posts, next_cursor = get_feed_posts(user, tab, topic, search, before)
    stats = get_site_stats()
    top_authors = get_top_authors(5)
    daily_topic = get_daily_topic()
    online_users = presence.online_users()

    posts = enrich_post_cards(posts, user)

    seo_ctx = list_page_seo(
        request,
        title="Feed",
        description="Discover the latest developer discussions, projects, and community activity on DevPlace.",
        breadcrumbs=[
            {"name": "Home", "url": "/feed"},
            {"name": "Feed", "url": "/feed"},
        ],
        next_url=next_page_url(request, next_cursor),
    )
    return respond(
        request,
        "feed.html",
        {
            **seo_ctx,
            "request": request,
            "user": user,
            "posts": posts,
            "current_tab": tab,
            "current_topic": topic,
            "search": search,
            "total_members": stats["total_members"],
            "posts_today": stats["posts_today"],
            "total_projects": stats["total_projects"],
            "total_gists": stats["total_gists"],
            "top_authors": top_authors,
            "daily_topic": daily_topic,
            "online_users": online_users,
            "next_cursor": next_cursor,
        },
        model=FeedOut,
    )
