import logging
from datetime import datetime, timezone
from fastapi import APIRouter, Request
from fastapi.responses import RedirectResponse
from devplacepy.database import get_table
from devplacepy.templating import clear_unread_cache
from devplacepy.utils import generate_uid, require_user

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


@router.post("/{username}")
async def follow_user(request: Request, username: str):
    user = require_user(request)
    users = get_table("users")
    target = users.find_one(username=username)
    if not target or target["uid"] == user["uid"]:
        return RedirectResponse(url=f"/profile/{username}", status_code=302)

    follows = get_table("follows")
    existing = follows.find_one(follower_uid=user["uid"], following_uid=target["uid"])
    if existing:
        return RedirectResponse(url=f"/profile/{username}", status_code=302)

    follows.insert({
        "uid": generate_uid(),
        "follower_uid": user["uid"],
        "following_uid": target["uid"],
        "created_at": datetime.now(timezone.utc).isoformat(),
    })

    notifications = get_table("notifications")
    notifications.insert({
        "uid": generate_uid(),
        "user_uid": target["uid"],
        "type": "follow",
        "message": f"{user['username']} started following you",
        "related_uid": user["uid"],
        "read": False,
        "created_at": datetime.now(timezone.utc).isoformat(),
    })
    clear_unread_cache(target["uid"])

    logger.info(f"{user['username']} followed {username}")
    return RedirectResponse(url=f"/profile/{username}", status_code=302)


@router.post("/unfollow/{username}")
async def unfollow_user(request: Request, username: str):
    user = require_user(request)
    users = get_table("users")
    target = users.find_one(username=username)
    if not target:
        return RedirectResponse(url=f"/profile/{username}", status_code=302)

    follows = get_table("follows")
    existing = follows.find_one(follower_uid=user["uid"], following_uid=target["uid"])
    if existing:
        follows.delete(id=existing["id"])
        logger.info(f"{user['username']} unfollowed {username}")

    return RedirectResponse(url=f"/profile/{username}", status_code=302)
