"""Tenant management blueprint."""
from flask import Blueprint, request, jsonify
from blueprints.auth_bp import require_manager_auth
from services import tenant_service

tenants_bp = Blueprint("tenants", __name__, url_prefix="/api/tenants")


@tenants_bp.route("", methods=["GET"])
@require_manager_auth
def list_tenants():
    include_stats = request.args.get("stats", "false").lower() == "true"
    return jsonify({"data": tenant_service.list_tenants(include_stats=include_stats)})


@tenants_bp.route("/<int:tenant_id>", methods=["GET"])
@require_manager_auth
def get_tenant(tenant_id):
    tenant = tenant_service.get_tenant(tenant_id)
    if not tenant:
        return jsonify({"error": "Tenant not found"}), 404
    return jsonify({"data": tenant})


@tenants_bp.route("/<int:tenant_id>/stats", methods=["GET"])
@require_manager_auth
def get_tenant_stats(tenant_id):
    tenant = tenant_service.get_tenant(tenant_id)
    if not tenant:
        return jsonify({"error": "Tenant not found"}), 404
    return jsonify({"data": tenant_service._get_tenant_quick_stats(tenant["db_name"])})


@tenants_bp.route("/<int:tenant_id>/toggle", methods=["POST"])
@require_manager_auth
def toggle_tenant(tenant_id):
    data = request.get_json() or {}
    active = data.get("active", True)
    result = tenant_service.toggle_tenant(tenant_id, active)
    return jsonify(result)


@tenants_bp.route("/<int:tenant_id>/reset", methods=["POST"])
@require_manager_auth
def reset_tenant(tenant_id):
    try:
        result = tenant_service.reset_tenant(tenant_id)
        return jsonify(result)
    except Exception as e:
        return jsonify({"error": str(e)}), 500


@tenants_bp.route("/<int:tenant_id>", methods=["DELETE"])
@require_manager_auth
def delete_tenant(tenant_id):
    try:
        result = tenant_service.delete_tenant(tenant_id)
        return jsonify(result)
    except Exception as e:
        return jsonify({"error": str(e)}), 500


@tenants_bp.route("/<int:tenant_id>/modules", methods=["GET"])
@require_manager_auth
def get_tenant_modules(tenant_id):
    try:
        result = tenant_service.get_tenant_modules(tenant_id)
        return jsonify({"data": result})
    except Exception as e:
        return jsonify({"error": str(e)}), 500


@tenants_bp.route("/<int:tenant_id>/modules", methods=["PUT"])
@require_manager_auth
def update_tenant_modules(tenant_id):
    data = request.get_json() or {}
    try:
        result = tenant_service.update_tenant_modules(tenant_id, data)
        return jsonify(result)
    except Exception as e:
        return jsonify({"error": str(e)}), 500
