"""
API routes for document scheduler management.
"""

from flask import Blueprint, jsonify, session
from loguru import logger

from ..web.auth.decorators import login_required
from .document_scheduler import get_document_scheduler

# Create blueprint
scheduler_bp = Blueprint("document_scheduler", __name__)


def get_current_username():
    """Get current username from session."""
    return session.get("username")


@scheduler_bp.route("/api/scheduler/status", methods=["GET"])
@login_required
def get_scheduler_status():
    """Get the current status of the document scheduler for the current user."""
    try:
        username = get_current_username()
        if not username:
            return jsonify({"error": "User not authenticated"}), 401

        scheduler = get_document_scheduler()
        status = scheduler.get_status(username)
        return jsonify(status)
    except Exception:
        logger.exception("Error getting scheduler status")
        return jsonify({"error": "Failed to get scheduler status"}), 500


@scheduler_bp.route("/api/scheduler/run-now", methods=["POST"])
@login_required
def trigger_manual_run():
    """Trigger a manual processing run of the document scheduler for the current user."""
    try:
        username = get_current_username()
        if not username:
            return jsonify({"error": "User not authenticated"}), 401

        scheduler = get_document_scheduler()
        success, message = scheduler.trigger_manual_run(username)

        if success:
            return jsonify({"message": message})
        else:
            return jsonify({"error": message}), 400
    except Exception:
        logger.exception("Error triggering manual run")
        return jsonify({"error": "Failed to trigger manual run"}), 500
