from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from typing import Dict, Any, List, Optional
from datetime import datetime

from app.core import get_db, get_current_active_user, get_current_admin_user
from app import services
from app.schemas.content_schemas import AnalyticsLogCreate
from app.models.user_models import User as UserModel

# Роутер для аналитики
analytics_router = APIRouter(prefix="/analytics", tags=["Аналитика"])

@analytics_router.post("/log", response_model=Dict[str, Any])
async def log_analytics_event_endpoint(log: AnalyticsLogCreate, db: Session = Depends(get_db)):
    return services.log_analytics_event(db, log)


@analytics_router.get("/logs", response_model=Dict[str, Any])
async def get_analytics_logs_endpoint(
    event_type: Optional[str] = None,
    start_date: Optional[datetime] = None,
    end_date: Optional[datetime] = None,
    skip: int = 0,
    limit: int = 100,
    current_user: UserModel = Depends(get_current_admin_user),
    db: Session = Depends(get_db)
):
    return services.get_analytics_logs(db, event_type, start_date, end_date, skip, limit)


@analytics_router.get("/report", response_model=Dict[str, Any])
async def get_analytics_report_endpoint(
    report_type: str,
    start_date: Optional[datetime] = None,
    end_date: Optional[datetime] = None,
    current_user: UserModel = Depends(get_current_admin_user),
    db: Session = Depends(get_db)
):
    return services.get_analytics_report(db, report_type, start_date, end_date) 