<?php
/**
 * Documentation API Controller
 * 
 * Serves documentation content to the Admin SPA.
 */

namespace WooNooW\Api;

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

class DocsController extends WP_REST_Controller {
    
    /**
     * Namespace for REST routes
     */
    protected $namespace = 'woonoow/v1';
    
    /**
     * Base route
     */
    protected $rest_base = 'docs';
    
    /**
     * Register routes
     */
    public function register_routes() {
        // GET /woonoow/v1/docs - List all documentation
        register_rest_route($this->namespace, '/' . $this->rest_base, [
            [
                'methods'             => 'GET',
                'callback'            => [$this, 'get_docs_registry'],
                'permission_callback' => [$this, 'check_permissions'],
            ],
        ]);
        
        // GET /woonoow/v1/docs/{slug} - Get single document
        register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<slug>.+)', [
            [
                'methods'             => 'GET',
                'callback'            => [$this, 'get_doc'],
                'permission_callback' => [$this, 'check_permissions'],
                'args'                => [
                    'slug' => [
                        'required'          => true,
                        'type'              => 'string',
                        'sanitize_callback' => 'sanitize_text_field',
                    ],
                ],
            ],
        ]);
    }
    
    /**
     * Check permissions - any logged in admin user
     */
    public function check_permissions($request) {
        return current_user_can('manage_options');
    }
    
    /**
     * Get documentation registry
     * 
     * @return WP_REST_Response
     */
    public function get_docs_registry($request) {
        require_once WOONOOW_PATH . 'docs/_registry.php';
        
        $registry = \WooNooW\Docs\get_docs_registry();
        
        // Transform to frontend format (without file paths)
        $result = [];
        foreach ($registry as $section_key => $section) {
            $items = [];
            foreach ($section['items'] as $item) {
                $items[] = [
                    'slug'  => $item['slug'],
                    'title' => $item['title'],
                ];
            }
            
            $result[] = [
                'key'   => $section_key,
                'label' => $section['label'],
                'icon'  => $section['icon'] ?? 'file-text',
                'items' => $items,
            ];
        }
        
        return new WP_REST_Response([
            'success'  => true,
            'sections' => $result,
        ], 200);
    }
    
    /**
     * Get single document content
     * 
     * @param WP_REST_Request $request
     * @return WP_REST_Response|WP_Error
     */
    public function get_doc($request) {
        $slug = $request->get_param('slug');
        
        require_once WOONOOW_PATH . 'docs/_registry.php';
        
        $doc = \WooNooW\Docs\get_doc_by_slug($slug);
        
        if (!$doc) {
            return new WP_Error(
                'doc_not_found',
                'Documentation not found',
                ['status' => 404]
            );
        }
        
        return new WP_REST_Response([
            'success' => true,
            'doc'     => [
                'slug'    => $doc['slug'],
                'title'   => $doc['title'],
                'content' => $doc['content'],
            ],
        ], 200);
    }
}
