<?php
/**
 * System Information REST API Controller
 * 
 * Provides REST endpoints for system information.
 * 
 * @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 SystemController extends WP_REST_Controller {
    
    /**
     * Namespace
     */
    protected $namespace = 'woonoow/v1';
    
    /**
     * Rest base
     */
    protected $rest_base = 'system';
    
    /**
     * Register routes
     */
    public function register_routes() {
        // GET /woonoow/v1/system/info
        register_rest_route($this->namespace, '/' . $this->rest_base . '/info', [
            [
                'methods' => WP_REST_Server::READABLE,
                'callback' => [$this, 'get_info'],
                'permission_callback' => [$this, 'check_permission'],
            ],
        ]);
        
        // POST /woonoow/v1/cache/clear
        register_rest_route($this->namespace, '/cache/clear', [
            [
                'methods' => WP_REST_Server::EDITABLE,
                'callback' => [$this, 'clear_cache'],
                'permission_callback' => [$this, 'check_permission'],
            ],
        ]);
    }
    
    /**
     * Get system information
     * 
     * @param WP_REST_Request $request Request object
     * @return WP_REST_Response|WP_Error Response object or error
     */
    public function get_info(WP_REST_Request $request) {
        global $wp_version;
        
        // Get WooNooW version
        $plugin_data = get_file_data(
            dirname(dirname(dirname(__FILE__))) . '/woonoow.php',
            ['Version' => 'Version']
        );
        
        // Get WooCommerce version
        $wc_version = defined('WC_VERSION') ? WC_VERSION : 'N/A';
        
        // Check HPOS
        $hpos_enabled = get_option('woocommerce_custom_orders_table_enabled') === 'yes';
        
        $response = rest_ensure_response([
            'woonoowVersion' => $plugin_data['Version'] ?? '0.1.0',
            'woocommerceVersion' => $wc_version,
            'wordpressVersion' => $wp_version,
            'phpVersion' => PHP_VERSION,
            'hposEnabled' => $hpos_enabled,
        ]);
        
        $response->header('Cache-Control', 'max-age=60');
        
        return $response;
    }
    
    /**
     * Clear cache
     * 
     * @param WP_REST_Request $request Request object
     * @return WP_REST_Response|WP_Error Response object or error
     */
    public function clear_cache(WP_REST_Request $request) {
        $type = $request->get_param('type') ?? 'all';
        
        switch ($type) {
            case 'navigation':
                // Clear navigation cache
                delete_transient('woonoow_navigation_tree');
                break;
                
            case 'settings':
                // Clear settings cache
                delete_transient('woonoow_store_settings');
                delete_transient('woonoow_developer_settings');
                break;
                
            case 'all':
                // Clear all WooNooW transients
                global $wpdb;
                $wpdb->query(
                    "DELETE FROM {$wpdb->options} 
                    WHERE option_name LIKE '_transient_woonoow_%' 
                    OR option_name LIKE '_transient_timeout_woonoow_%'"
                );
                break;
                
            default:
                return new WP_Error(
                    'invalid_cache_type',
                    'Invalid cache type',
                    ['status' => 400]
                );
        }
        
        return rest_ensure_response([
            'success' => true,
            'message' => 'Cache cleared successfully',
        ]);
    }
    
    /**
     * Check permission
     * 
     * @return bool True if user has permission
     */
    public function check_permission() {
        return current_user_can('manage_woocommerce');
    }
}
