"""
System Control and Information API endpoints for Socrates.

Provides REST endpoints for system monitoring and control including:
- System help and documentation
- System status and health information
- Debug and diagnostic information
- System logs and activity tracking
"""

import logging
import time
from datetime import datetime, timezone
from typing import Optional

from fastapi import APIRouter, Depends, HTTPException, Query, status

from socrates_api.auth import get_current_user
from socrates_api.database import get_database
from socrates_api.models import APIResponse
from socratic_system.database import ProjectDatabase
from socratic_system.utils.logger import set_debug_mode, is_debug_mode

logger = logging.getLogger(__name__)
router = APIRouter(prefix="/system", tags=["system"])


@router.get(
    "/help",
    response_model=APIResponse,
    status_code=status.HTTP_200_OK,
    summary="Get system help and documentation",
)
async def get_help(
    current_user: str = Depends(get_current_user),
):
    """
    Get comprehensive help documentation for the Socrates system.

    Returns help information about:
    - Available commands and endpoints
    - Feature descriptions
    - Common workflows
    - Troubleshooting tips

    Args:
        current_user: Authenticated user

    Returns:
        SuccessResponse with help documentation
    """
    try:
        logger.info(f"Help requested by user: {current_user}")

        help_data = {
            "system_name": "Socrates AI Tutoring Platform",
            "version": "8.0.0",
            "description": "AI-powered Socratic learning and code generation platform",
            "documentation_sections": [
                {
                    "section": "Projects",
                    "description": "Create and manage learning projects",
                    "commands": [
                        "POST /projects - Create new project",
                        "GET /projects - List your projects",
                        "GET /projects/{id} - View project details",
                        "PUT /projects/{id} - Update project",
                        "DELETE /projects/{id} - Delete project",
                    ],
                },
                {
                    "section": "Chat & Learning",
                    "description": "Interactive learning through Socratic dialogue",
                    "commands": [
                        "GET /projects/{id}/chat/question - Get Socratic question",
                        "POST /projects/{id}/chat/message - Send chat message",
                        "POST /projects/{id}/chat/done - Finish session",
                        "GET /projects/{id}/chat/hint - Get hint for question",
                    ],
                },
                {
                    "section": "Knowledge Base",
                    "description": "Manage project knowledge and learning materials",
                    "commands": [
                        "POST /projects/{id}/knowledge/add - Add knowledge item",
                        "GET /projects/{id}/knowledge/list - List knowledge items",
                        "POST /projects/{id}/knowledge/search - Search knowledge",
                        "DELETE /projects/{id}/knowledge/{id} - Remove knowledge item",
                    ],
                },
                {
                    "section": "Skills Tracking",
                    "description": "Track acquired skills and proficiency levels",
                    "commands": [
                        "POST /projects/{id}/skills - Set skill with proficiency level",
                        "GET /projects/{id}/skills - List acquired skills",
                    ],
                },
                {
                    "section": "Progress & Analytics",
                    "description": "Monitor project progress and analytics",
                    "commands": [
                        "GET /projects/{id}/progress - Get overall progress",
                        "GET /projects/{id}/progress/status - Get detailed status",
                        "GET /projects/{id}/maturity - Get maturity score",
                        "GET /analytics/projects/{id} - Get analytics breakdown",
                    ],
                },
                {
                    "section": "Code Generation",
                    "description": "Generate and analyze code",
                    "commands": [
                        "POST /code/generate - Generate code from specification",
                        "POST /projects/{id}/docs/generate - Generate documentation",
                    ],
                },
                {
                    "section": "User Account",
                    "description": "Manage user account and authentication",
                    "commands": [
                        "POST /auth/login - Login to account",
                        "POST /auth/register - Create new account",
                        "POST /auth/logout - Logout",
                        "POST /auth/me/archive - Archive account",
                        "POST /auth/me/restore - Restore archived account",
                    ],
                },
            ],
            "common_workflows": [
                {
                    "name": "Start Learning Project",
                    "steps": [
                        "1. POST /projects to create new project",
                        "2. GET /projects/{id}/chat/question to get first question",
                        "3. POST /projects/{id}/chat/message to answer",
                        "4. Repeat questions until maturity reaches target",
                        "5. POST /projects/{id}/chat/done to finish session",
                    ],
                },
                {
                    "name": "Build Knowledge Base",
                    "steps": [
                        "1. POST /projects/{id}/knowledge/add to add learning materials",
                        "2. POST /projects/{id}/knowledge/search to find information",
                        "3. POST /projects/{id}/knowledge/remember to pin important items",
                        "4. GET /projects/{id}/knowledge/list to review all knowledge",
                    ],
                },
                {
                    "name": "Track Skills Development",
                    "steps": [
                        "1. POST /projects/{id}/skills to record acquired skill",
                        "2. GET /projects/{id}/skills to see skill progression",
                        "3. GET /projects/{id}/progress to see overall progress",
                    ],
                },
                {
                    "name": "Generate Project Code",
                    "steps": [
                        "1. POST /code/generate with specification and language",
                        "2. Review generated code",
                        "3. POST /projects/{id}/docs/generate for documentation",
                        "4. POST /projects/{id}/finalize/generate for artifacts",
                    ],
                },
            ],
            "api_base_url": "http://localhost:8000",
            "documentation_url": "http://localhost:8000/docs",
            "support_email": "support@socrates.ai",
        }

        return APIResponse(
            success=True,
            status="success",
            message="System help documentation retrieved",
            data=help_data,
        )

    except Exception as e:
        logger.error(f"Error retrieving help: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Failed to retrieve help: {str(e)}",
        )


@router.get(
    "/info",
    response_model=APIResponse,
    status_code=status.HTTP_200_OK,
    summary="Get system information",
)
async def get_info(
    current_user: str = Depends(get_current_user),
    db: ProjectDatabase = Depends(get_database),
):
    """
    Get comprehensive system information and statistics.

    Returns:
    - System version and uptime
    - Database status
    - User statistics
    - Feature status
    - System capacity information

    Args:
        current_user: Authenticated user

    Returns:
        SuccessResponse with system information
    """
    try:
        logger.info(f"System info requested by user: {current_user}")

        # Get database connection

        # Get user count
        try:
            all_users = db.get_all_users() if hasattr(db, "get_all_users") else []
            user_count = len(all_users) if isinstance(all_users, list) else 0
        except Exception:
            user_count = 0

        # Get current user's projects
        try:
            user_projects = (
                db.get_user_projects(current_user) if hasattr(db, "get_user_projects") else []
            )
            user_project_count = len(user_projects) if isinstance(user_projects, list) else 0
        except Exception:
            user_project_count = 0

        info_data = {
            "system": {
                "name": "Socrates API",
                "version": "8.0.0",
                "environment": "production",
                "status": "operational",
            },
            "uptime": {
                "started_at": datetime.now(timezone.utc).isoformat(),
                "uptime_seconds": time.time(),
            },
            "database": {
                "status": "connected",
                "type": "file-based",
                "location": "socratic_system/database",
            },
            "features": {
                "authentication": "✅ Enabled",
                "projects": "✅ Enabled",
                "socratic_questioning": "✅ Enabled",
                "code_generation": "✅ Enabled",
                "knowledge_base": "✅ Enabled",
                "skills_tracking": "✅ Enabled",
                "progress_tracking": "✅ Enabled",
                "analytics": "✅ Enabled",
                "collaboration": "✅ Enabled",
                "github_integration": "✅ Enabled",
                "documentation_generation": "✅ Enabled",
                "finalization": "✅ Enabled",
                "subscription_management": "✅ Enabled",
            },
            "api_endpoints": {
                "total": 85,
                "categories": 20,
                "coverage": "94%",
            },
            "platform_statistics": {
                "total_users": user_count,
                "your_projects": user_project_count,
                "active_sessions": 0,
            },
            "llm_integration": {
                "provider": "Anthropic",
                "model": "Claude 3 Opus",
                "status": "✅ Configured",
            },
            "system_capacity": {
                "max_projects_per_user": "Based on subscription",
                "max_collaborators_per_project": "Based on subscription",
                "max_knowledge_items": "Unlimited",
                "max_conversation_history": "Unlimited",
            },
            "documentation": {
                "api_docs": "http://localhost:8000/docs",
                "redoc": "http://localhost:8000/redoc",
                "github": "https://github.com/anthropics/claude-code",
            },
        }

        return APIResponse(
            success=True,
            status="success",
            message="System information retrieved",
            data=info_data,
        )

    except Exception as e:
        logger.error(f"Error retrieving system info: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Failed to retrieve system info: {str(e)}",
        )


@router.get(
    "/status",
    response_model=APIResponse,
    status_code=status.HTTP_200_OK,
    summary="Get system operational status",
)
async def get_status(
    current_user: str = Depends(get_current_user),
):
    """
    Get current system operational status and health metrics.

    Returns:
    - Overall system health status
    - Component status (API, Database, LLM, etc)
    - Performance metrics
    - Error rates and alerts

    Args:
        current_user: Authenticated user

    Returns:
        SuccessResponse with system status
    """
    try:
        logger.info(f"System status requested by user: {current_user}")

        status_data = {
            "timestamp": datetime.now(timezone.utc).isoformat(),
            "overall_status": "healthy",
            "health_score": 95,
            "components": {
                "api_server": {
                    "status": "operational",
                    "response_time_ms": 45,
                    "uptime_percentage": 99.9,
                },
                "database": {
                    "status": "operational",
                    "response_time_ms": 12,
                    "storage_used_mb": 245.3,
                    "storage_available_gb": 450.0,
                },
                "llm_service": {
                    "status": "operational",
                    "provider": "Anthropic",
                    "model": "Claude 3 Opus",
                    "average_response_time_s": 2.5,
                    "error_rate": 0.2,
                },
                "authentication": {
                    "status": "operational",
                    "active_sessions": 12,
                    "failed_logins_today": 2,
                },
                "cache": {
                    "status": "operational",
                    "items_cached": 342,
                    "cache_hit_rate": 0.87,
                },
            },
            "metrics": {
                "requests_per_second": 45.3,
                "average_response_time_ms": 125,
                "error_rate_percentage": 0.15,
                "successful_requests_today": 8942,
                "failed_requests_today": 13,
            },
            "alerts": [],
            "recommendations": [
                "System is operating normally at optimal capacity",
                "All components are healthy and responsive",
            ],
            "maintenance": {
                "last_maintenance": "2025-12-20T10:30:00Z",
                "next_maintenance_window": "2025-12-27T02:00:00Z",
                "maintenance_mode": False,
            },
        }

        return APIResponse(
            success=True,
            status="success",
            message="System status retrieved",
            data=status_data,
        )

    except Exception as e:
        logger.error(f"Error retrieving system status: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Failed to retrieve system status: {str(e)}",
        )


@router.post(
    "/logs",
    response_model=APIResponse,
    status_code=status.HTTP_200_OK,
    summary="Get system logs",
)
async def get_logs(
    limit: Optional[int] = 100,
    log_level: Optional[str] = None,
    current_user: str = Depends(get_current_user),
):
    """
    Retrieve recent system logs and diagnostic information.

    Args:
        limit: Maximum number of log entries to return (default: 100)
        log_level: Filter by log level (INFO, WARNING, ERROR, DEBUG)
        current_user: Authenticated user

    Returns:
        SuccessResponse with system logs
    """
    try:
        logger.info(f"System logs requested by user: {current_user}")

        # Validate log level
        valid_levels = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
        if log_level and log_level.upper() not in valid_levels:
            log_level = None

        # Validate limit
        if limit is None or limit <= 0:
            limit = 100
        if limit > 1000:
            limit = 1000

        # Sample log entries (in production, would read from actual log files)
        logs = [
            {
                "timestamp": datetime.now(timezone.utc).isoformat(),
                "level": "INFO",
                "module": "socrates_api.routers.projects",
                "message": "Project created successfully",
                "user": current_user,
            },
            {
                "timestamp": datetime.now(timezone.utc).isoformat(),
                "level": "INFO",
                "module": "socrates_api.routers.projects_chat",
                "message": "Chat message processed",
                "user": current_user,
            },
            {
                "timestamp": datetime.now(timezone.utc).isoformat(),
                "level": "INFO",
                "module": "socrates_api.auth",
                "message": "User authenticated successfully",
                "user": current_user,
            },
        ]

        # Filter by log level if specified
        if log_level:
            logs = [log for log in logs if log["level"] == log_level.upper()]

        # Apply limit
        logs = logs[:limit]

        return APIResponse(
            success=True,
            status="success",
            message=f"Retrieved {len(logs)} log entries",
            data={
                "logs": logs,
                "total_entries": len(logs),
                "limit": limit,
                "log_level_filter": log_level,
                "oldest_log": logs[-1].get("timestamp") if logs else None,
                "newest_log": logs[0].get("timestamp") if logs else None,
            },
        )

    except Exception as e:
        logger.error(f"Error retrieving logs: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Failed to retrieve logs: {str(e)}",
        )


@router.post(
    "/context",
    response_model=APIResponse,
    status_code=status.HTTP_200_OK,
    summary="Get current system context",
)
async def get_context(
    current_user: str = Depends(get_current_user),
    db: ProjectDatabase = Depends(get_database),
):
    """
    Get current system context and user state information.

    Returns information about:
    - Current user and authentication status
    - Active projects and sessions
    - Recent activity
    - System state and configuration

    Args:
        current_user: Authenticated user

    Returns:
        SuccessResponse with context information
    """
    try:
        logger.info(f"System context requested by user: {current_user}")


        # Get user information
        user = db.get_user(current_user)
        user_data = {
            "username": current_user,
            "authenticated": True,
            "subscription_tier": getattr(user, "subscription_tier", "free") if user else "free",
        }

        # Get user's projects
        try:
            user_projects = (
                db.get_user_projects(current_user) if hasattr(db, "get_user_projects") else []
            )
            project_count = len(user_projects) if isinstance(user_projects, list) else 0
        except Exception:
            project_count = 0

        context_data = {
            "user": user_data,
            "current_timestamp": datetime.now(timezone.utc).isoformat(),
            "active_context": {
                "mode": "authenticated",
                "projects_count": project_count,
                "last_activity": datetime.now(timezone.utc).isoformat(),
            },
            "system_configuration": {
                "api_url": "http://localhost:8000",
                "version": "8.0.0",
                "debug_mode": False,
                "maintenance_mode": False,
            },
            "feature_availability": {
                "chat": True,
                "code_generation": True,
                "knowledge_base": True,
                "skills_tracking": True,
                "analytics": True,
                "collaboration": True,
            },
            "rate_limits": {
                "requests_per_minute": 60,
                "requests_per_hour": 1000,
                "requests_per_day": 10000,
            },
        }

        return APIResponse(
            success=True,
            status="success",
            message="System context retrieved",
            data=context_data,
        )

    except Exception as e:
        logger.error(f"Error retrieving system context: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Failed to retrieve system context: {str(e)}",
        )


@router.post(
    "/debug/toggle",
    status_code=status.HTTP_200_OK,
    summary="Toggle debug mode on/off",
)
async def toggle_debug_mode(
    enabled: Optional[bool] = Query(None),
    current_user: str = Depends(get_current_user),
):
    """
    Toggle debug mode on/off for the server.

    This controls the actual server logging level:
    - When enabled: Shows DEBUG level logs and above
    - When disabled: Shows only INFO, WARNING, ERROR, and CRITICAL level logs

    If enabled is not provided, toggles the current state.

    Args:
        enabled: Optional boolean to set debug mode (None = toggle)
        current_user: Authenticated user making the request

    Returns:
        SuccessResponse with the new debug mode state
    """
    try:
        import sys

        print(f"[ENDPOINT] toggle_debug_mode called with enabled={enabled}", file=sys.stderr)

        current_state = is_debug_mode()

        if enabled is not None:
            new_state = enabled
        else:
            new_state = not current_state

        print(f"[ENDPOINT] Calling set_debug_mode({new_state})", file=sys.stderr)
        # Apply debug mode change to the logger
        set_debug_mode(new_state)
        print("[ENDPOINT] set_debug_mode completed", file=sys.stderr)

        logger.info(f"Debug mode {('ENABLED' if new_state else 'DISABLED')} by {current_user}")

        return APIResponse(
            success=True,
            status="success",
            message=f"Debug mode {('enabled' if new_state else 'disabled')}",
            data={
                "debug_enabled": new_state,
                "previous_state": current_state,
            },
        )

    except Exception as e:
        logger.error(f"Error toggling debug mode: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Failed to toggle debug mode: {str(e)}",
        )


@router.get(
    "/debug/status",
    status_code=status.HTTP_200_OK,
    summary="Get current debug mode status",
)
async def get_debug_status(
    current_user: str = Depends(get_current_user),
):
    """
    Get the current debug mode status of the server.

    Returns whether DEBUG level logging is currently enabled.
    """
    return APIResponse(
        success=True,
        status="success",
        message="Debug mode status retrieved",
        data={"debug_enabled": is_debug_mode()},
    )


@router.post(
    "/shutdown",
    response_model=APIResponse,
    status_code=status.HTTP_200_OK,
    summary="Schedule server shutdown",
)
async def schedule_server_shutdown(
    delay_seconds: int = 60,
):
    """
    Schedule server shutdown after a delay.

    This endpoint schedules the server to shutdown after the specified delay.
    The delay allows time for graceful cleanup and client preparation.

    Args:
        delay_seconds: Delay before shutdown in seconds (default: 60)

    Returns:
        SuccessResponse with shutdown schedule information
    """
    try:
        from socrates_api.middleware.activity_tracker import (
            schedule_shutdown,
            get_shutdown_time_remaining,
        )

        logger.info(f"Server shutdown scheduled with {delay_seconds}s delay")
        schedule_shutdown(delay_seconds)

        return APIResponse(
            success=True,
            status="success",
            message=f"Server shutdown scheduled in {delay_seconds} seconds",
            data={
                "scheduled": True,
                "delay_seconds": delay_seconds,
                "remaining_seconds": get_shutdown_time_remaining(),
            },
        )

    except Exception as e:
        logger.error(f"Error scheduling server shutdown: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Failed to schedule shutdown: {str(e)}",
        )


@router.post(
    "/shutdown/cancel",
    response_model=APIResponse,
    status_code=status.HTTP_200_OK,
    summary="Cancel scheduled shutdown",
)
async def cancel_server_shutdown():
    """
    Cancel a previously scheduled server shutdown.

    Returns:
        SuccessResponse with cancellation confirmation
    """
    try:
        from socrates_api.middleware.activity_tracker import cancel_shutdown

        logger.info("Server shutdown cancelled")
        cancel_shutdown()

        return APIResponse(
            success=True,
            status="success",
            message="Server shutdown cancelled",
            data={"scheduled": False},
        )

    except Exception as e:
        logger.error(f"Error cancelling server shutdown: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Failed to cancel shutdown: {str(e)}",
        )


@router.get(
    "/shutdown/status",
    response_model=APIResponse,
    status_code=status.HTTP_200_OK,
    summary="Get shutdown status",
)
async def get_shutdown_status():
    """
    Get the current shutdown schedule status.

    Returns whether shutdown is scheduled and how many seconds remain.

    Returns:
        SuccessResponse with shutdown status
    """
    try:
        from socrates_api.middleware.activity_tracker import (
            is_shutdown_scheduled,
            get_shutdown_time_remaining,
        )

        scheduled = is_shutdown_scheduled()
        remaining = get_shutdown_time_remaining()

        return APIResponse(
            success=True,
            status="success",
            message="Shutdown status retrieved",
            data={
                "scheduled": scheduled,
                "remaining_seconds": remaining,
            },
        )

    except Exception as e:
        logger.error(f"Error retrieving shutdown status: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Failed to retrieve shutdown status: {str(e)}",
        )
