<?php
/**
 * Developer Settings REST API Controller
 * 
 * Provides REST endpoints for developer settings management.
 * 
 * @package WooNooW
 */

namespace WooNooW\API;

use WP_REST_Controller;
use WP_REST_Server;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;

class DeveloperController extends WP_REST_Controller {
    
    /**
     * Namespace
     */
    protected $namespace = 'woonoow/v1';
    
    /**
     * Rest base
     */
    protected $rest_base = 'settings/developer';
    
    /**
     * Register routes
     */
    public function register_routes() {
        // GET /woonoow/v1/settings/developer
        register_rest_route($this->namespace, '/' . $this->rest_base, [
            [
                'methods' => WP_REST_Server::READABLE,
                'callback' => [$this, 'get_settings'],
                'permission_callback' => [$this, 'check_permission'],
            ],
        ]);
        
        // POST /woonoow/v1/settings/developer
        register_rest_route($this->namespace, '/' . $this->rest_base, [
            [
                'methods' => WP_REST_Server::EDITABLE,
                'callback' => [$this, 'save_settings'],
                'permission_callback' => [$this, 'check_permission'],
            ],
        ]);
    }
    
    /**
     * Get developer settings
     * 
     * @param WP_REST_Request $request Request object
     * @return WP_REST_Response|WP_Error Response object or error
     */
    public function get_settings(WP_REST_Request $request) {
        return rest_ensure_response([
            'debug_mode' => (bool) get_option('woonoow_debug_mode', false),
            'show_api_logs' => (bool) get_option('woonoow_show_api_logs', false),
            'enable_react_devtools' => (bool) get_option('woonoow_enable_react_devtools', false),
        ]);
    }
    
    /**
     * Save developer settings
     * 
     * @param WP_REST_Request $request Request object
     * @return WP_REST_Response|WP_Error Response object or error
     */
    public function save_settings(WP_REST_Request $request) {
        $settings = $request->get_json_params();
        
        if (empty($settings)) {
            return new WP_Error(
                'missing_settings',
                'No settings provided',
                ['status' => 400]
            );
        }
        
        // Save settings
        if (isset($settings['debug_mode'])) {
            update_option('woonoow_debug_mode', (bool) $settings['debug_mode']);
        }
        if (isset($settings['show_api_logs'])) {
            update_option('woonoow_show_api_logs', (bool) $settings['show_api_logs']);
        }
        if (isset($settings['enable_react_devtools'])) {
            update_option('woonoow_enable_react_devtools', (bool) $settings['enable_react_devtools']);
        }
        
        return rest_ensure_response([
            'success' => true,
            'message' => 'Developer settings saved successfully',
        ]);
    }
    
    /**
     * Check permission
     * 
     * @return bool True if user has permission
     */
    public function check_permission() {
        return current_user_can('manage_woocommerce');
    }
}
