# #region AssistantAdminRoutes [C:5] [TYPE Module] [SEMANTICS assistant, admin, route, audit, conversation]
# @defgroup AssistantApi Module group.
# @BRIEF FastAPI route handlers for assistant admin operations — conversation listing, deletion, history, audit.
# @LAYER API
# @RELATION DEPENDS_ON -> [AssistantRoutes]
# @RELATION DEPENDS_ON -> [AssistantSchemas]
# @RELATION DEPENDS_ON -> [AssistantHistory]
# @INVARIANT  Audit endpoint requires tasks:READ permission.

from __future__ import annotations

from datetime import datetime
from typing import Any

from fastapi import Depends, HTTPException, Query
from sqlalchemy import desc
from sqlalchemy.orm import Session

from src.core.database import get_db
from src.core.logger import belief_scope
from src.dependencies import get_current_user, has_permission
from src.models.assistant import (
    AssistantAuditRecord,
    AssistantMessageRecord,
)
from src.schemas.auth import User

from ._history import (
    _cleanup_history_ttl,
    _coerce_query_bool,
    _is_conversation_archived,
    _resolve_or_create_conversation,
)
from ._routes import router
from ._schemas import (
    ASSISTANT_AUDIT,
    CONVERSATIONS,
)


# #region list_conversations [C:2] [TYPE Function]
# @ingroup AssistantApi
# @BRIEF DEPRECATED — replaced by AgentChat.Api.ListConversations.
#         Return empty list. Kept for import compatibility.
# @DEPRECATED Replaced by AgentChat.Api.ListConversations
async def list_conversations(
    page: int = Query(1, ge=1),
    page_size: int = Query(20, ge=1, le=100),
    include_archived: bool = Query(False),
    archived_only: bool = Query(False),
    search: str | None = Query(None),
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    """DEPRECATED — use AgentChat.Api.ListConversations instead."""
    return {"items": [], "total": 0, "page": page, "page_size": page_size, "has_next": False, "active_total": 0, "archived_total": 0}
# #endregion list_conversations


# #region delete_conversation [C:2] [TYPE Function]
# @ingroup AssistantApi
# @BRIEF Soft-delete or hard-delete a conversation and clear its in-memory trace.
# @PRE  conversation_id belongs to current_user.
# @POST  Conversation records are removed from DB and CONVERSATIONS cache.
@router.delete("/conversations/{conversation_id}")
async def delete_conversation(
    conversation_id: str,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    with belief_scope("assistant.conversations.delete"):
        user_id = current_user.id

        # 1. Remove from in-memory cache
        key = (user_id, conversation_id)
        if key in CONVERSATIONS:
            del CONVERSATIONS[key]

        # 2. Delete from database
        deleted_count = (
            db.query(AssistantMessageRecord)
            .filter(
                AssistantMessageRecord.user_id == user_id,
                AssistantMessageRecord.conversation_id == conversation_id,
            )
            .delete()
        )

        db.commit()

        if deleted_count == 0:
            raise HTTPException(
                status_code=404, detail="Conversation not found or already deleted"
            )

        return {
            "status": "success",
            "deleted": deleted_count,
            "conversation_id": conversation_id,
        }


# #endregion delete_conversation


@router.get("/history")
# #region get_history [TYPE Function]
# @ingroup AssistantApi
# @BRIEF Retrieve paginated assistant conversation history for current user.
# @PRE  Authenticated user is available and page params are valid.
# @POST  Returns persistent messages and mirrored in-memory snapshot for diagnostics.
async def get_history(
    page: int = Query(1, ge=1),
    page_size: int = Query(20, ge=1, le=100),
    conversation_id: str | None = Query(None),
    from_latest: bool = Query(False),
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    with belief_scope("assistant.history"):
        user_id = current_user.id
        _cleanup_history_ttl(db, user_id)
        conv_id = _resolve_or_create_conversation(user_id, conversation_id, db)

        base_query = db.query(AssistantMessageRecord).filter(
            AssistantMessageRecord.user_id == user_id,
            AssistantMessageRecord.conversation_id == conv_id,
        )
        total = base_query.count()
        start = (page - 1) * page_size
        if from_latest:
            rows = (
                base_query.order_by(desc(AssistantMessageRecord.created_at))
                .offset(start)
                .limit(page_size)
                .all()
            )
            rows = list(reversed(rows))
        else:
            rows = (
                base_query.order_by(AssistantMessageRecord.created_at.asc())
                .offset(start)
                .limit(page_size)
                .all()
            )

        persistent_items = [
            {
                "message_id": row.id,
                "conversation_id": row.conversation_id,
                "role": row.role,
                "text": row.text,
                "state": row.state,
                "task_id": row.task_id,
                "confirmation_id": row.confirmation_id,
                "created_at": row.created_at.isoformat() if row.created_at else None,
                "metadata": row.payload,
            }
            for row in rows
        ]

        memory_items = CONVERSATIONS.get((user_id, conv_id), [])
        return {
            "items": persistent_items,
            "memory_items": memory_items,
            "total": total,
            "page": page,
            "page_size": page_size,
            "has_next": start + page_size < total,
            "from_latest": from_latest,
            "conversation_id": conv_id,
        }


# #endregion get_history


@router.get("/audit")
# #region get_assistant_audit [TYPE Function]
# @ingroup AssistantApi
# @BRIEF Return assistant audit decisions for current user from persistent and in-memory stores.
# @PRE  User has tasks:READ permission.
# @POST  Audit payload is returned in reverse chronological order from DB.
async def get_assistant_audit(
    limit: int = Query(50, ge=1, le=500),
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db),
    _=Depends(has_permission("tasks", "READ")),
):
    with belief_scope("assistant.audit"):
        memory_rows = ASSISTANT_AUDIT.get(current_user.id, [])
        db_rows = (
            db.query(AssistantAuditRecord)
            .filter(AssistantAuditRecord.user_id == current_user.id)
            .order_by(AssistantAuditRecord.created_at.desc())
            .limit(limit)
            .all()
        )
        persistent = [
            {
                "id": row.id,
                "user_id": row.user_id,
                "conversation_id": row.conversation_id,
                "decision": row.decision,
                "task_id": row.task_id,
                "message": row.message,
                "payload": row.payload,
                "created_at": row.created_at.isoformat() if row.created_at else None,
            }
            for row in db_rows
        ]
        return {
            "items": persistent,
            "memory_items": memory_rows[-limit:],
            "total": len(persistent),
            "memory_total": len(memory_rows),
        }


# #endregion get_assistant_audit


# #endregion AssistantAdminRoutes
