"""soagila_enterprise — Team sync (CIFCE) and compliance reports. Enterprise only."""

import json
from mcp.types import Tool, TextContent


def get_definition() -> Tool:
    return Tool(
        name="soagila_enterprise",
        description=(
            "Enterprise features: team memory sync (CIFCE) and compliance reports. "
            "Team sync shares anonymized semantic patterns across team members. "
            "Compliance generates audit reports from the decision ledger. "
            "Requires Enterprise tier license."
        ),
        inputSchema={
            "type": "object",
            "properties": {
                "action": {
                    "type": "string",
                    "enum": ["team_push", "team_pull", "team_status", "compliance_report", "compliance_export"],
                    "description": "Action to perform. compliance_export saves report to file.",
                },
                "export_path": {
                    "type": "string",
                    "description": "File path to save compliance report (for compliance_export).",
                },
                "team_id": {
                    "type": "string",
                    "description": "Team identifier (for team sync actions).",
                },
                "framework": {
                    "type": "string",
                    "enum": ["general", "gdpr", "hipaa", "sox", "eu_ai_act"],
                    "description": "Compliance framework (for compliance_report). Default: general",
                },
                "days": {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 365,
                    "description": "Report period in days. Default: 30",
                },
            },
            "required": ["action"],
        },
    )


async def handle(arguments: dict) -> list[TextContent]:
    action = arguments.get("action")
    team_id = arguments.get("team_id", "default")
    framework = arguments.get("framework", "general")
    days = arguments.get("days", 30)

    if action in ("team_push", "team_pull", "team_status"):
        # CIFCE Team Sync
        from soagila.core.cifce.team_sync import push_patterns, pull_patterns, get_team_status

        if action == "team_push":
            result = await push_patterns(team_id)
        elif action == "team_pull":
            result = await pull_patterns(team_id)
        else:
            result = await get_team_status(team_id)

    elif action == "compliance_report":
        # Compliance Report
        from soagila.core.compliance import generate_report
        result = await generate_report(framework=framework, days=days)

    elif action == "compliance_export":
        # Compliance Export to file
        export_path = arguments.get("export_path")
        if not export_path:
            result = {"error": "export_path is required for compliance_export"}
        else:
            from soagila.audit_export import export_compliance_report
            report_json = export_compliance_report(framework, days, export_path)
            result = {"status": "exported", "path": export_path, "framework": framework, "days": days}

    else:
        result = {"error": f"Unknown action: {action}"}

    return [TextContent(type="text", text=json.dumps(result, indent=2))]
