<?php
/**
 * Model Context Protocol (MCP) Endpoint
 * Agentic SEO - umožňuje AI agentom priamo komunikovať s platformou
 * /.well-known/mcp.json
 */

if (ob_get_level()) {
    ob_end_clean();
}

header('Content-Type: application/json; charset=UTF-8');
header('Access-Control-Allow-Origin: *');
header('Cache-Control: public, max-age=3600');
header('X-Robots-Tag: noindex');

require_once __DIR__ . '/../config/config.php';

$settings = function_exists('getAllSettings') ? getAllSettings() : [];
$siteName = $settings['site_name'] ?? 'PlayServerList';

$mcp = [
    'schema_version' => '2026.1',
    'protocol' => 'model-context-protocol',
    'provider' => [
        'name' => $siteName,
        'url' => APP_URL,
        'description' => 'Game server directory with real-time monitoring, voting, and community features.',
        'category' => 'gaming',
        'subcategory' => 'server-directory'
    ],
    
    'authentication' => [
        'type' => 'none',
        'public_endpoints' => true,
        'note' => 'All read endpoints are publicly accessible without authentication.'
    ],
    
    'resources' => [
        [
            'name' => 'servers',
            'description' => 'Browse and search game servers with real-time status, player counts, and community ratings.',
            'uri' => APP_URL . '/api/servers.json',
            'mimeType' => 'application/json',
            'parameters' => [
                ['name' => 'category', 'type' => 'string', 'description' => 'Filter by game category slug (e.g., minecraft, discord, rust)'],
                ['name' => 'search', 'type' => 'string', 'description' => 'Search servers by name or description'],
                ['name' => 'sort', 'type' => 'string', 'description' => 'Sort by: votes, newest, players, name'],
                ['name' => 'limit', 'type' => 'integer', 'description' => 'Number of results (max 100, default 20)'],
                ['name' => 'offset', 'type' => 'integer', 'description' => 'Pagination offset']
            ]
        ],
        [
            'name' => 'categories',
            'description' => 'List all available game categories with server counts.',
            'uri' => APP_URL . '/api/categories.php',
            'mimeType' => 'application/json'
        ],
        [
            'name' => 'site_summary',
            'description' => 'Structured summary of the platform for AI context.',
            'uri' => APP_URL . '/ai/summary.json',
            'mimeType' => 'application/json'
        ],
        [
            'name' => 'benchmarks',
            'description' => 'Proprietary real-time benchmark data about game server ecosystem.',
            'uri' => APP_URL . '/ai/benchmarks.json',
            'mimeType' => 'application/json'
        ],
        [
            'name' => 'facts',
            'description' => 'Key facts about the platform in citation-ready format.',
            'uri' => APP_URL . '/ai/facts.json',
            'mimeType' => 'application/json'
        ],
        [
            'name' => 'faq',
            'description' => 'Frequently asked questions with structured answers.',
            'uri' => APP_URL . '/ai/faq.json',
            'mimeType' => 'application/json'
        ],
        [
            'name' => 'health',
            'description' => 'Platform health check and status.',
            'uri' => APP_URL . '/api/health.php',
            'mimeType' => 'application/json'
        ]
    ],
    
    'tools' => [
        [
            'name' => 'search_servers',
            'description' => 'Search for game servers by name, game type, or keywords. Returns server details including IP, player count, status, and votes.',
            'inputSchema' => [
                'type' => 'object',
                'properties' => [
                    'query' => ['type' => 'string', 'description' => 'Search query'],
                    'category' => ['type' => 'string', 'description' => 'Game category slug'],
                    'sort' => ['type' => 'string', 'enum' => ['votes', 'newest', 'players', 'name']]
                ]
            ]
        ],
        [
            'name' => 'get_server_info',
            'description' => 'Get detailed information about a specific game server including real-time status, player count, description, and community votes.',
            'inputSchema' => [
                'type' => 'object',
                'properties' => [
                    'server_slug' => ['type' => 'string', 'description' => 'Server URL slug'],
                    'category' => ['type' => 'string', 'description' => 'Category slug']
                ],
                'required' => ['server_slug']
            ]
        ],
        [
            'name' => 'get_platform_stats',
            'description' => 'Get real-time platform statistics and benchmark data.',
            'inputSchema' => [
                'type' => 'object',
                'properties' => []
            ]
        ]
    ],
    
    'prompts' => [
        [
            'name' => 'find_server',
            'description' => 'Help users find the best game server for their needs.',
            'arguments' => [
                ['name' => 'game', 'description' => 'Game type (e.g., Minecraft, CS2, Rust)', 'required' => true],
                ['name' => 'preferences', 'description' => 'User preferences (e.g., low ping, many players, PvP)', 'required' => false]
            ]
        ],
        [
            'name' => 'compare_servers',
            'description' => 'Compare multiple game servers by stats and features.',
            'arguments' => [
                ['name' => 'servers', 'description' => 'Server names or IDs to compare', 'required' => true]
            ]
        ]
    ],
    
    'rate_limits' => [
        'requests_per_minute' => 60,
        'burst' => 10,
        'note' => 'Please respect rate limits. Use /ai/ endpoints for structured data access.'
    ],
    
    'contact' => [
        'email' => $settings['contact_email'] ?? 'contact@playserverlist.com',
        'url' => APP_URL . '/contact'
    ],
    
    'metadata' => [
        'generated_at' => date('c'),
        'content_version' => defined('CONTENT_VERSION') ? CONTENT_VERSION : '3.0.0'
    ]
];

echo json_encode($mcp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
