"""
API testing, advanced parsing, and plugin management tools
"""

from mcp.types import Tool


def get_api_test_tools():
    """Return tools for API testing, advanced parsing, and plugin management."""
    return [
        Tool(
            name="api_test",
            description="Test API endpoints with various HTTP methods, headers, and request bodies",
            inputSchema={
                "type": "object",
                "properties": {
                    "url": {
                        "type": "string",
                        "description": "API endpoint URL to test",
                    },
                    "method": {
                        "type": "string",
                        "description": "HTTP method (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS)",
                        "default": "GET",
                        "enum": [
                            "GET",
                            "POST",
                            "PUT",
                            "DELETE",
                            "PATCH",
                            "HEAD",
                            "OPTIONS",
                        ],
                    },
                    "headers": {
                        "type": "object",
                        "description": "HTTP headers to send with the request",
                        "default": {},
                    },
                    "body": {
                        "type": "string",
                        "description": "Request body (for POST, PUT, PATCH methods)",
                    },
                    "body_type": {
                        "type": "string",
                        "description": "Type of request body (json, xml, form, text)",
                        "default": "json",
                        "enum": ["json", "xml", "form", "text"],
                    },
                    "params": {
                        "type": "object",
                        "description": "Query parameters to append to URL",
                        "default": {},
                    },
                    "auth": {
                        "type": "object",
                        "description": "Authentication configuration",
                        "properties": {
                            "type": {
                                "type": "string",
                                "enum": ["basic", "bearer", "api_key"],
                            },
                            "username": {"type": "string"},
                            "password": {"type": "string"},
                            "token": {"type": "string"},
                            "api_key": {"type": "string"},
                            "api_key_header": {
                                "type": "string",
                                "default": "X-API-Key",
                            },
                        },
                    },
                    "timeout": {
                        "type": "number",
                        "description": "Request timeout in seconds",
                        "default": 30,
                    },
                    "follow_redirects": {
                        "type": "boolean",
                        "description": "Whether to follow HTTP redirects",
                        "default": True,
                    },
                    "validate_ssl": {
                        "type": "boolean",
                        "description": "Whether to validate SSL certificates",
                        "default": True,
                    },
                    "expected_status": {
                        "type": "number",
                        "description": "Expected HTTP status code for validation",
                    },
                    "response_format": {
                        "type": "string",
                        "description": "Expected response format (json, xml, html, text)",
                        "default": "auto",
                        "enum": ["auto", "json", "xml", "html", "text"],
                    },
                },
                "required": ["url"],
            },
        ),
        Tool(
            name="api_batch_test",
            description="Run multiple API tests in batch with test scenarios",
            inputSchema={
                "type": "object",
                "properties": {
                    "tests": {
                        "type": "array",
                        "description": "Array of API test configurations",
                        "items": {
                            "type": "object",
                            "properties": {
                                "name": {"type": "string", "description": "Test name"},
                                "url": {
                                    "type": "string",
                                    "description": "API endpoint URL",
                                },
                                "method": {"type": "string", "default": "GET"},
                                "expected_status": {"type": "number"},
                            },
                            "required": ["name", "url"],
                        },
                    },
                    "parallel": {
                        "type": "boolean",
                        "description": "Run tests in parallel",
                        "default": False,
                    },
                    "max_concurrent": {
                        "type": "number",
                        "description": "Maximum concurrent requests for parallel execution",
                        "default": 5,
                    },
                    "stop_on_failure": {
                        "type": "boolean",
                        "description": "Stop execution on first failure",
                        "default": False,
                    },
                },
                "required": ["tests"],
            },
        ),
        Tool(
            name="parse_advanced",
            description="Advanced content parsing with custom schemas and extraction rules",
            inputSchema={
                "type": "object",
                "properties": {
                    "url": {
                        "type": "string",
                        "description": "URL of the page to parse (alternative to html)",
                    },
                    "html": {
                        "type": "string",
                        "description": "HTML content to parse (alternative to url)",
                    },
                    "schema": {
                        "type": "object",
                        "description": "Custom extraction schema",
                        "properties": {
                            "fields": {
                                "type": "array",
                                "description": "Array of field extraction rules",
                                "items": {
                                    "type": "object",
                                    "properties": {
                                        "name": {
                                            "type": "string",
                                            "description": "Field name",
                                        },
                                        "selector": {
                                            "type": "string",
                                            "description": "CSS selector",
                                        },
                                        "attribute": {
                                            "type": "string",
                                            "description": "Attribute to extract",
                                        },
                                        "multiple": {
                                            "type": "boolean",
                                            "default": False,
                                        },
                                        "required": {
                                            "type": "boolean",
                                            "default": False,
                                        },
                                    },
                                    "required": ["name", "selector"],
                                },
                            },
                            "transformations": {
                                "type": "object",
                                "description": "Field transformation rules",
                                "additionalProperties": {
                                    "type": "object",
                                    "properties": {
                                        "type": {
                                            "type": "string",
                                            "enum": [
                                                "trim",
                                                "lowercase",
                                                "uppercase",
                                                "number",
                                                "date",
                                            ],
                                        },
                                        "pattern": {
                                            "type": "string",
                                            "description": "Regex pattern for extraction",
                                        },
                                        "replace": {
                                            "type": "string",
                                            "description": "Replacement string",
                                        },
                                    },
                                },
                            },
                        },
                        "required": ["fields"],
                    },
                    "output_format": {
                        "type": "string",
                        "description": "Output format (json, csv, xml)",
                        "default": "json",
                        "enum": ["json", "csv", "xml"],
                    },
                    "validate_schema": {
                        "type": "boolean",
                        "description": "Validate extracted data against schema",
                        "default": False,
                    },
                },
            },
        ),
        Tool(
            name="extract_dynamic_content",
            description="Extract dynamically loaded content using browser automation",
            inputSchema={
                "type": "object",
                "properties": {
                    "url": {
                        "type": "string",
                        "description": "URL to extract dynamic content from",
                    },
                    "wait_for": {
                        "type": "string",
                        "description": "CSS selector to wait for before extraction",
                    },
                    "wait_time": {
                        "type": "number",
                        "description": "Additional wait time in seconds",
                        "default": 2,
                    },
                    "interactions": {
                        "type": "array",
                        "description": "Browser interactions to perform",
                        "items": {
                            "type": "object",
                            "properties": {
                                "type": {
                                    "type": "string",
                                    "enum": ["click", "scroll", "wait"],
                                },
                                "selector": {
                                    "type": "string",
                                    "description": "Element selector",
                                },
                                "text": {
                                    "type": "string",
                                    "description": "Text to type (for input fields)",
                                },
                            },
                            "required": ["type"],
                        },
                    },
                    "extraction_rules": {
                        "type": "object",
                        "description": "Content extraction rules (same as parse_advanced schema)",
                        "properties": {"fields": {"type": "array"}},
                    },
                    "take_screenshot": {
                        "type": "boolean",
                        "description": "Take screenshot after loading",
                        "default": False,
                    },
                },
                "required": ["url"],
            },
        ),
        Tool(
            name="load_plugin",
            description="Load a plugin by name",
            inputSchema={
                "type": "object",
                "properties": {
                    "plugin_name": {
                        "type": "string",
                        "description": "Name of the plugin to load",
                    },
                    "config": {
                        "type": "object",
                        "description": "Plugin configuration",
                        "default": {},
                    },
                },
                "required": ["plugin_name"],
            },
        ),
        Tool(
            name="unload_plugin",
            description="Unload a plugin by name",
            inputSchema={
                "type": "object",
                "properties": {
                    "plugin_name": {
                        "type": "string",
                        "description": "Name of the plugin to unload",
                    }
                },
                "required": ["plugin_name"],
            },
        ),
        Tool(
            name="list_plugins",
            description="List all loaded plugins",
            inputSchema={"type": "object", "properties": {}},
        ),
        Tool(
            name="discover_plugins",
            description="Discover available plugins in plugin directories",
            inputSchema={"type": "object", "properties": {}},
        ),
        Tool(
            name="get_plugin_tools",
            description="Get all tools provided by loaded plugins",
            inputSchema={"type": "object", "properties": {}},
        ),
    ]
