<?php

namespace FleekDash\API;

use FleekDash\Controllers\DataSourcePluginsController;

/**
 * Data Source Plugins API
 *
 * Registers REST API routes for managing data source plugins
 * in the DashboardBuilder system.
 */
class DataSourcePluginsAPI
{
    /**
     * Controller instance
     */
    private $controller;

    public function __construct()
    {
        $this->controller = new DataSourcePluginsController();
        $this->register_routes();
    }

    /**
     * Register REST API routes
     */
    private function register_routes()
    {
        // Get all plugins
        \register_rest_route('fleekdash/v1', '/data-source-plugins', [
            [
                'methods' => 'GET',
                'callback' => [$this->controller, 'get_plugins'],
                'permission_callback' => [$this, 'check_permissions'],
                'args' => [
                    'filter' => [
                        'type' => 'string',
                        'enum' => ['all', 'enabled', 'disabled', 'core', 'third_party'],
                        'default' => 'all',
                        'sanitize_callback' => 'sanitize_text_field'
                    ]
                ]
            ],
            [
                'methods' => 'POST',
                'callback' => [$this->controller, 'install_plugin'],
                'permission_callback' => [$this, 'check_admin_permissions'],
                'args' => [
                    'id' => [
                        'type' => 'string',
                        'required' => true,
                        'sanitize_callback' => 'sanitize_text_field'
                    ],
                    'name' => [
                        'type' => 'string',
                        'required' => true,
                        'sanitize_callback' => 'sanitize_text_field'
                    ],
                    'version' => [
                        'type' => 'string',
                        'required' => true,
                        'sanitize_callback' => 'sanitize_text_field'
                    ],
                    'description' => [
                        'type' => 'string',
                        'sanitize_callback' => 'sanitize_textarea_field'
                    ],
                    'author' => [
                        'type' => 'string',
                        'sanitize_callback' => 'sanitize_text_field'
                    ]
                ]
            ]
        ]);

        // Get specific plugin
        \register_rest_route('fleekdash/v1', '/data-source-plugins/(?P<plugin_id>[a-zA-Z0-9_-]+)', [
            [
                'methods' => 'GET',
                'callback' => [$this->controller, 'get_plugin'],
                'permission_callback' => [$this, 'check_permissions'],
                'args' => [
                    'plugin_id' => [
                        'type' => 'string',
                        'required' => true,
                        'sanitize_callback' => 'sanitize_text_field'
                    ]
                ]
            ],
            [
                'methods' => 'DELETE',
                'callback' => [$this->controller, 'uninstall_plugin'],
                'permission_callback' => [$this, 'check_admin_permissions'],
                'args' => [
                    'plugin_id' => [
                        'type' => 'string',
                        'required' => true,
                        'sanitize_callback' => 'sanitize_text_field'
                    ]
                ]
            ]
        ]);

        // Update plugin status
        \register_rest_route('fleekdash/v1', '/data-source-plugins/(?P<plugin_id>[a-zA-Z0-9_-]+)/status', [
            [
                'methods' => 'POST',
                'callback' => [$this->controller, 'update_plugin_status'],
                'permission_callback' => [$this, 'check_admin_permissions'],
                'args' => [
                    'plugin_id' => [
                        'type' => 'string',
                        'required' => true,
                        'sanitize_callback' => 'sanitize_text_field'
                    ],
                    'enabled' => [
                        'type' => 'boolean',
                        'required' => true,
                        'default' => true
                    ]
                ]
            ]
        ]);

        // Test plugin functionality
        \register_rest_route('fleekdash/v1', '/data-source-plugins/(?P<plugin_id>[a-zA-Z0-9_-]+)/test', [
            [
                'methods' => 'POST',
                'callback' => [$this->controller, 'test_plugin'],
                'permission_callback' => [$this, 'check_permissions'],
                'args' => [
                    'plugin_id' => [
                        'type' => 'string',
                        'required' => true,
                        'sanitize_callback' => 'sanitize_text_field'
                    ]
                ]
            ]
        ]);

        // Get all data source types
        \register_rest_route('fleekdash/v1', '/data-source-types', [
            [
                'methods' => 'GET',
                'callback' => [$this->controller, 'get_data_source_types'],
                'permission_callback' => [$this, 'check_permissions'],
                'args' => [
                    'plugin_id' => [
                        'type' => 'string',
                        'sanitize_callback' => 'sanitize_text_field'
                    ]
                ]
            ]
        ]);

        // Get specific data source type
        \register_rest_route('fleekdash/v1', '/data-source-types/(?P<type>[a-zA-Z0-9_-]+)', [
            [
                'methods' => 'GET',
                'callback' => [$this->controller, 'get_data_source_type'],
                'permission_callback' => [$this, 'check_permissions'],
                'args' => [
                    'type' => [
                        'type' => 'string',
                        'required' => true,
                        'sanitize_callback' => 'sanitize_text_field'
                    ]
                ]
            ]
        ]);

        // Get plugin statistics
        \register_rest_route('fleekdash/v1', '/data-source-plugins/stats', [
            [
                'methods' => 'GET',
                'callback' => [$this->controller, 'get_plugin_stats'],
                'permission_callback' => [$this, 'check_permissions']
            ]
        ]);
    }

    /**
     * Check basic permissions
     */
    public function check_permissions()
    {
        return \current_user_can('read');
    }

    /**
     * Check admin permissions
     */
    public function check_admin_permissions()
    {
        return \current_user_can('manage_options');
    }
}
