import logging
from typing import Annotated, Optional

from fastapi import APIRouter, Depends, Query
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, func

from app.api.deps import get_current_active_user, require_admin
from app.core.database import get_db
from app.models.user import User
from app.models.beta_feedback import BetaFeedback
from app.schemas.beta_feedback import BetaFeedbackCreate, BetaFeedbackResponse

logger = logging.getLogger(__name__)

router = APIRouter(tags=["beta-feedback"])


@router.post("/feedback/beta", response_model=BetaFeedbackResponse, status_code=201)
async def submit_beta_feedback(
    data: BetaFeedbackCreate,
    current_user: Annotated[User, Depends(get_current_active_user)],
    db: Annotated[AsyncSession, Depends(get_db)],
):
    """Submit beta feedback. Any authenticated user can submit."""
    record = BetaFeedback(
        user_id=current_user.id,
        reaction=data.reaction.value,
        category=data.category.value if data.category else None,
        text=data.text,
        page_url=data.page_url,
        session_id=data.session_id,
    )
    db.add(record)
    await db.commit()
    await db.refresh(record)
    return record


@router.get("/feedback/beta", response_model=list[BetaFeedbackResponse])
async def list_beta_feedback(
    current_user: Annotated[User, Depends(require_admin)],
    db: Annotated[AsyncSession, Depends(get_db)],
    reaction: Optional[str] = Query(None, description="Filter by reaction: positive, neutral, negative"),
    category: Optional[str] = Query(None, description="Filter by category: bug, feature, confusing, praise"),
    skip: int = Query(0, ge=0),
    limit: int = Query(50, ge=1, le=100),
):
    """List all beta feedback. Super admin only."""
    query = select(BetaFeedback)

    if reaction:
        query = query.where(BetaFeedback.reaction == reaction)
    if category:
        query = query.where(BetaFeedback.category == category)

    query = query.order_by(BetaFeedback.created_at.desc()).offset(skip).limit(limit)
    result = await db.execute(query)
    return result.scalars().all()
