import logging
from datetime import datetime, timezone
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
from devplacepy.attachments import get_attachments_batch
from devplacepy.templating import templates
from devplacepy.utils import get_current_user, time_ago
from devplacepy.seo import base_seo_context, site_url, website_schema, breadcrumb_schema, combine

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

PAGE_SIZE = 25


def get_feed_posts(user, tab: str = "all", topic: str = None, before: str = None):
    posts_table = get_table("posts")

    filters = {}
    if topic:
        filters["topic"] = topic

    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"])]
        if not following:
            return [], None
        posts = list(posts_table.find(posts_table.table.columns.user_uid.in_(following), order_by=["-created_at"], _limit=PAGE_SIZE))
    else:
        order = ["-created_at"]
        if tab == "trending":
            order = ["-stars", "-created_at"]
        if before:
            posts = list(posts_table.find(**filters, order_by=order, _limit=PAGE_SIZE + 1))
            posts = [p for p in posts if p["created_at"] < before][:PAGE_SIZE]
        else:
            posts = list(posts_table.find(**filters, order_by=order, _limit=PAGE_SIZE + 1))

    has_more = len(posts) > PAGE_SIZE
    posts = posts[:PAGE_SIZE]

    next_cursor = None
    if has_more and posts:
        next_cursor = posts[-1]["created_at"]

    if not posts:
        return [], next_cursor

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

    result = []
    for post in posts:
        result.append({
            "post": post,
            "author": authors.get(post["user_uid"]),
            "time_ago": time_ago(post["created_at"]),
            "comment_count": counts.get(post["uid"], 0),
        })
    return result, next_cursor


@router.get("", response_class=HTMLResponse)
async def feed_page(request: Request, tab: str = "all", topic: str = None, before: str = None):
    user = get_current_user(request)
    posts, next_cursor = get_feed_posts(user, tab, topic, before)
    users_table = get_table("users")
    total_members = len(list(users_table.all()))
    posts_table = get_table("posts")
    today_start = datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0).isoformat()
    posts_today = len(list(posts_table.find(created_at={">=": today_start})))
    total_projects = len(list(get_table("projects").all()))
    total_gists = len(list(get_table("gists").all()))
    top_authors = list(users_table.find(stars={">": 0}, order_by=["-stars"], _limit=5))
    daily_topic = get_daily_topic()

    post_uids_list = [item["post"]["uid"] for item in posts]
    attachments_map = get_attachments_batch("post", post_uids_list)
    for item in posts:
        item["attachments"] = attachments_map.get(item["post"]["uid"], [])

    base = site_url(request)
    seo_ctx = base_seo_context(
        request,
        title="Feed",
        description="Discover the latest developer discussions, projects, and community activity on DevPlace.",
        breadcrumbs=[{"name": "Home", "url": "/feed"}, {"name": "Feed", "url": "/feed"}],
        schemas=[website_schema(base)],
    )
    return templates.TemplateResponse(request, "feed.html", {
        **seo_ctx,
        "request": request,
        "user": user,
        "posts": posts,
        "current_tab": tab,
        "current_topic": topic,
        "total_members": total_members,
        "posts_today": posts_today,
        "total_projects": total_projects,
        "total_gists": total_gists,
        "top_authors": top_authors,
        "daily_topic": daily_topic,
        "next_cursor": next_cursor,
    })
