<?php

namespace JEELSHHA\Controllers;

use JEELSHHA\Config;
use JEELSHHA\Core\React;
use JEELSHHA\Models\Headers;
use JEELSHHA\Services\HeaderDispatcher;
use JEELSHHA\Services\HeaderValidator;
use JEELSHHA\Services\RestSecurity;

defined('ABSPATH') or die();


class SettingsController
{
    /**
     * Share global data with the React app.
     */
    public static function boot()
    {
        React::share([
            'user' => [
                'name' => \wp_get_current_user()->display_name,
                'email' => \wp_get_current_user()->user_email,
            ],
            'site' => [
                'name' => \get_bloginfo('name'),
                'url' => \get_site_url(),
            ],
        ]);
    }

    /**
     * Register REST API routes.
     */
    public static function registerRoutes()
    {
        \register_rest_route('jeelsh-http-headers/v1', '/settings', [
            [
                'methods'             => \WP_REST_Server::READABLE,
                'callback'            => [__CLASS__, 'getSettings'],
                'permission_callback' => [__CLASS__, 'permissionCheck'],
            ],
            [
                'methods'             => \WP_REST_Server::CREATABLE,
                'callback'            => [__CLASS__, 'saveSettings'],
                'permission_callback' => [__CLASS__, 'permissionCheck'],
            ],
        ]);
    }

    /**
     * Permission callback: admins with a valid REST nonce.
     */
    public static function permissionCheck(\WP_REST_Request $request)
    {
        return RestSecurity::adminPermissionCheck($request);
    }

    /**
     * GET /settings – Return all header settings.
     */
    public static function getSettings(\WP_REST_Request $request)
    {
        $headers = Headers::load();
        return new \WP_REST_Response($headers->toArray(), 200);
    }

    /**
     * POST /settings – Save header settings.
     */
    public static function saveSettings(\WP_REST_Request $request)
    {
        $data = $request->get_json_params();

        if (empty($data) || !\is_array($data)) {
            return new \WP_REST_Response(['message' => \__('No data provided.', 'jeelsh-http-headers')], 400);
        }

        try {
            $data = HeaderValidator::validateSettings($data);
        } catch (\InvalidArgumentException $e) {
            return new \WP_REST_Response(['message' => $e->getMessage()], 400);
        }

        $headers = Headers::load();
        $headers->fill($data)->save();

        HeaderDispatcher::apply();

        return new \WP_REST_Response([
            'message'  => \__('Settings saved successfully.', 'jeelsh-http-headers'),
            'settings' => $headers->toArray(),
        ], 200);
    }

    /**
     * Render the React settings page.
     */
    public static function settingsPage()
    {
        if (!\current_user_can('manage_options')) {
            \wp_die(\esc_html(\__('You do not have sufficient permissions to access this page.', 'jeelsh-http-headers')));
        }

        // Hide all non-Antonella siblings inside wpbody-content on this page
        // and fix the Antonella container to the viewport so the admin sidebar
        // overflow never creates extra whitespace below the panel.
        $cleanupHandle = 'jeelsh-http-headers-admin-cleanup';
        \wp_register_style($cleanupHandle, false, [], '1.0.0');
        $cleanupCss = '#wpbody-content > *:not([data-antonella-react]):not(script[id*="antonella-react-"]):not(style) { display: none !important; }'
            . '#wpbody-content [data-antonella-react] { position: fixed !important; top: 32px; right: 0; bottom: 0; left: 160px; height: auto !important; min-height: auto !important; }'
            . 'body.folded #wpbody-content [data-antonella-react] { left: 36px; }'
            . '@media screen and (max-width: 782px) { #wpbody-content [data-antonella-react] { top: 46px; left: 0; } }';
        \wp_add_inline_style($cleanupHandle, $cleanupCss);
        \wp_print_styles([$cleanupHandle]);

        $assetBaseUrl = self::assetBaseUrl();

        $config = new Config();

        $pageData = [
            'title'        => \__('Jeelsh HTTP Headers', 'jeelsh-http-headers'),
            'message'      => \__('Manage advanced HTTP headers for your WordPress site.', 'jeelsh-http-headers'),
            'assetBaseUrl' => $assetBaseUrl,
            'version'      => $config->version,
        ];

        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
        echo React::render('AdminPanel', $pageData);
    }

    /**
     * Resolve the correct base URL for public assets depending on environment.
     */
    protected static function assetBaseUrl()
    {
        $appEnv = React::getConfig('app_env', 'production');

        if ($appEnv === 'develop') {
            return \rtrim(React::getConfig('vite_server', 'http://localhost:3000'), '/');
        }

        $pluginUrl = \plugin_dir_url(\dirname(\dirname(__DIR__)) . '/jeelsh-http-headers.php');
        $buildPath = React::getConfig('build_path', 'assets/dist');

        return \rtrim($pluginUrl . $buildPath, '/');
    }
}
