<?php

declare(strict_types=1);

namespace LolaCore\Admin;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Class McpConfigGenerator
 *
 * Generates client-specific MCP configuration templates for IDEs.
 * Templates include {{USERNAME}} and {{PASSWORD}} placeholders that
 * the frontend JS replaces with actual Application Password credentials.
 */
class McpConfigGenerator
{
    /**
     * Get the supported IDE client configuration templates.
     *
     * Each entry contains the client name, target filename, and a JSON
     * template string with credential placeholders.
     *
     * @return array<string, array{name: string, filename: string, template: string}>
     */
    public static function getClients(): array
    {
        $namespaces   = \apply_filters('lolacore_mcp_namespaces', ['lolacore']);
        $endpointBase = \get_rest_url(null, 'mcp/');

        // --- Claude Code (.mcp.json) ---
        $claudeServers = [];
        foreach ($namespaces as $ns) {
            $claudeServers[$ns] = [
                'type'    => 'http',
                'url'     => $endpointBase . $ns,
                'headers' => [
                    'Authorization' => 'Basic {{CREDENTIALS}}',
                    'User-Agent'    => 'Claude/1.0',
                ],
            ];
        }

        // --- Cursor (.cursor/mcp.json) ---
        $cursorServers = [];
        foreach ($namespaces as $ns) {
            $cursorServers[$ns] = [
                'url'     => $endpointBase . $ns,
                'headers' => [
                    'Authorization' => 'Basic {{CREDENTIALS}}',
                    'User-Agent'    => 'Cursor/1.0',
                ],
            ];
        }

        // --- Antigravity (mcp_config.json) ---
        $antigravityServers = [];
        foreach ($namespaces as $ns) {
            $antigravityServers[$ns] = [
                'serverUrl' => $endpointBase . $ns,
                'headers'   => [
                    'Authorization' => 'Basic {{CREDENTIALS}}',
                    'User-Agent'    => 'Antigravity/1.0',
                ],
            ];
        }

        return [
            'claude_code' => [
                'name'     => 'Claude Code',
                'filename' => '.mcp.json',
                'template' => (string) \wp_json_encode(
                    ['mcpServers' => $claudeServers],
                    JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
                ),
            ],
            'cursor' => [
                'name'     => 'Cursor',
                'filename' => '.cursor/mcp.json',
                'template' => (string) \wp_json_encode(
                    ['mcpServers' => $cursorServers],
                    JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
                ),
            ],
            'antigravity' => [
                'name'     => 'Antigravity',
                'filename' => 'mcp_config.json',
                'template' => (string) \wp_json_encode(
                    ['mcpServers' => $antigravityServers],
                    JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
                ),
            ],
        ];
    }
}
