<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
class SS_API {

    public static function init(){

        add_action('rest_api_init', function(){



            // ---- Existing status route (unchanged) ----

            register_rest_route('searchshifter/v1','/status',[

                'methods'  => 'GET',

                'permission_callback' => '__return_true',

                'callback' => function(){

                    $lic = SS_Licensing::get_cached_status();

                    return [

                        'version'         => SEARCHSHIFTER_VERSION,

                        'ok'              => true,

                        'license'         => $lic,

                        'readonly'        => ! $lic['active'],

                        'features'        => (array) get_option(SS_Settings::OPT_FEATURE_FLAGS, []),

                        'telemetry_opt_in'=> (bool) get_option(SS_Settings::OPT_TELEMETRY, false),

                    ];

                }

            ]);



            // ---- NEW: Milestone 5 – public AI Light Checker route ----

            register_rest_route('searchshifter/v1','/light-check',[

                'methods'  => ['GET','POST'],

                'permission_callback' => '__return_true', // public endpoint

                'args'     => [

                    'url' => [

                        'required' => true,

                        'type'     => 'string',

                    ],

                ],

                'callback' => function(\WP_REST_Request $req){

                    $url = (string) $req->get_param('url');



                    // Lazy-load the class via autoloader path includes/Core/LightChecker.php

                    if (!class_exists('\\Searchshifter\\Core\\LightChecker')) {

                        $path = SEARCHSHIFTER_PLUGIN_DIR . 'includes/Core/LightChecker.php';

                        if (file_exists($path)) require_once $path;

                    }



                    if (!class_exists('\\Searchshifter\\Core\\LightChecker')) {

                        return new \WP_REST_Response(['ok'=>false,'error'=>'LightChecker class missing'], 500);

                    }



                    $res = \Searchshifter\Core\LightChecker::scan($url);

                    $code = (!empty($res['ok'])) ? 200 : 400;

                    return new \WP_REST_Response($res, $code);

                }

            ]);



        });

    }

        /**
     * Authority-only backend API request
     */
    public static function authority_request(string $path, array $payload = []) {

        if (!defined('SEARCHSHIFTER_API_BASE')) {
            return new WP_Error('config', 'SEARCHSHIFTER_API_BASE not defined');
        }

        $base = rtrim(SEARCHSHIFTER_API_BASE, '/');
        $url  = $base . $path;

        $domain = wp_parse_url(home_url(), PHP_URL_HOST);
        $token  = get_option('ss_api_token');

        


        if (!$token) {
            return new WP_Error('auth', 'Site token missing');
        }

        $res = wp_remote_post($url, [
            'timeout' => 45,
            'headers' => [
                'Authorization' => 'Bearer ' . $token,
                'X-Domain'      => $domain,
                'Accept'        => 'application/json',
                'Content-Type'  => 'application/json',
            ],
            'body' => wp_json_encode($payload),
        ]);

        if (is_wp_error($res)) {
            return $res;
        }

        $code = wp_remote_retrieve_response_code($res);
        $body = json_decode(wp_remote_retrieve_body($res), true);

        if ($code !== 200) {
            return new WP_Error(
                'api_error',
                $body['error'] ?? 'API request failed',
                ['status' => $code, 'response' => $body]
            );
        }

        return $body;
    }


}

SS_API::init();
