<?php
namespace OxygenApp\Hooks;

if ( ! defined( 'ABSPATH' ) ) exit;

use OxygenApp\Config\Config;
use OxygenApp\Controllers\UserCtrl;
use OxygenApp\Controllers\LangCtrl;
use OxygenApp\Controllers\GoogleAuthCtrl;
use OxygenApp\Controllers\PlaylistCtrl;
use OxygenApp\Common\Permissions\RestPermissions;
use OxygenApp\Utils\OxySession;

class ActionRestApi implements IHook {

    static function register() {
        add_action('rest_api_init', [self::class, 'initSessionForRestApi'], 5); // Priority 5 - before routes
        add_action('rest_api_init', [self::class, 'registerRoutes']);
    }

    /**
     * Initialize session only for this plugin's REST API requests
     * This allows caching on non-plugin REST endpoints
     */
    static function initSessionForRestApi(): void {
        // Check if this is a request to our plugin's namespace
        $request_uri = isset($_SERVER['REQUEST_URI']) ? sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])) : '';
        $namespace = Config::getInstance()->getSetting('app_rest_namespace');

        // Extract namespace from full path (e.g., "oxygen-app/v1" from "/wp-json/oxygen-app/v1/login")
        if (strpos($request_uri, '/wp-json/' . $namespace) !== false) {
            OxySession::initSession();
        }
    }

    static function registerRoutes(): void {
        register_rest_route(Config::getInstance()->getSetting('app_rest_namespace'), '/user', [
            [
                'methods'             => \WP_REST_Server::CREATABLE,
                'callback'            => [UserCtrl::class, 'create'],
                'args'                => [],
                'permission_callback' => [RestPermissions::class, 'allowPublic'],
            ],
        ]);

        register_rest_route(Config::getInstance()->getSetting('app_rest_namespace'), '/user-resend-activation', [
            [
                'methods'             => \WP_REST_Server::CREATABLE,
                'callback'            => [UserCtrl::class, 'resendActivation'],
                'args'                => [],
                'permission_callback' => [RestPermissions::class, 'allowPublic'],
            ],
        ]);

        register_rest_route(Config::getInstance()->getSetting('app_rest_namespace'), '/activate', [
            [
                'methods'             => \WP_REST_Server::READABLE,
                'callback'            => [UserCtrl::class, 'activate'],
                'args'                => [],
                'permission_callback' => [RestPermissions::class, 'allowPublic'],
            ],
        ]);

        register_rest_route(Config::getInstance()->getSetting('app_rest_namespace'), '/reset-password-request', [
            [
                'methods'             => \WP_REST_Server::CREATABLE,
                'callback'            => [UserCtrl::class, 'resetPasswordRequest'],
                'args'                => [],
                'permission_callback' => [RestPermissions::class, 'allowPublic'],
            ],
        ]);

        register_rest_route(Config::getInstance()->getSetting('app_rest_namespace'), '/reset-password', [
            [
                'methods'             => \WP_REST_Server::CREATABLE,
                'callback'            => [UserCtrl::class, 'resetPassword'],
                'args'                => [],
                'permission_callback' => [RestPermissions::class, 'allowPublic'],
            ],
        ]);

        register_rest_route(Config::getInstance()->getSetting('app_rest_namespace'), '/google-generate-redirect', [
            [
                'methods'             => \WP_REST_Server::READABLE,
                'callback'            => [GoogleAuthCtrl::class, 'generateRedirect'],
                'args'                => [],
                'permission_callback' => [RestPermissions::class, 'allowPublic'],
            ],
        ]);

        register_rest_route(Config::getInstance()->getSetting('app_rest_namespace'), '/handle-google-auth-callback', [
            [
                'methods'             => \WP_REST_Server::READABLE,
                'callback'            => [GoogleAuthCtrl::class, 'handleOAuthCallback'],
                'args'                => [],
                'permission_callback' => [RestPermissions::class, 'allowPublic'],
            ],
        ]);

        register_rest_route(Config::getInstance()->getSetting('app_rest_namespace'), '/user-background', [
            [
                'methods'             => \WP_REST_Server::EDITABLE,
                'callback'            => [UserCtrl::class, 'updateBackground'],
                'args'                => [],
                'permission_callback' => [RestPermissions::class, 'isLoggedIn'],
            ],
        ]);

        register_rest_route(Config::getInstance()->getSetting('app_rest_namespace'), '/login', [
            [
                'methods'             => \WP_REST_Server::CREATABLE,
                'callback'            => [UserCtrl::class, 'login'],
                'args'                => [],
                'permission_callback' => [RestPermissions::class, 'allowPublic'],
            ],
        ]);

        register_rest_route(Config::getInstance()->getSetting('app_rest_namespace'), '/logout', [
            [
                'methods'             => \WP_REST_Server::READABLE,
                'callback'            => [UserCtrl::class, 'logout'],
                'args'                => [],
                'permission_callback' => [RestPermissions::class, 'allowAll'],
            ],
        ]);

        register_rest_route(Config::getInstance()->getSetting('app_rest_namespace'), '/playlists', [
            [
                'methods'             => \WP_REST_Server::READABLE,
                'callback'            => [PlaylistCtrl::class, 'getPlaylists'],
                'args'                => [],
                'permission_callback' => [RestPermissions::class, 'isLoggedIn'],
            ],
        ]);

        register_rest_route(Config::getInstance()->getSetting('app_rest_namespace'), '/playlist', [
            [
                'methods'             => \WP_REST_Server::CREATABLE,
                'callback'            => [PlaylistCtrl::class, 'create'],
                'args'                => [],
                'permission_callback' => [RestPermissions::class, 'isLoggedIn'],
            ],
        ]);

        register_rest_route(Config::getInstance()->getSetting('app_rest_namespace'), '/playlist', [
            [
                'methods'             => \WP_REST_Server::DELETABLE,
                'callback'            => [PlaylistCtrl::class, 'delete'],
                'args'                => [],
                'permission_callback' => [RestPermissions::class, 'isLoggedIn'],
            ],
        ]);

        register_rest_route(Config::getInstance()->getSetting('app_rest_namespace'), '/playlist', [
            [
                'methods'             => \WP_REST_Server::EDITABLE,
                'callback'            => [PlaylistCtrl::class, 'update'],
                'args'                => [],
                'permission_callback' => [RestPermissions::class, 'isLoggedIn'],
            ],
        ]);

        register_rest_route(Config::getInstance()->getSetting('app_rest_namespace'), '/set-language', [
            [
                'methods'             => \WP_REST_Server::CREATABLE,
                'callback'            => [LangCtrl::class, 'create'],
                'args'                => [],
                'permission_callback' => [RestPermissions::class, 'allowAll'],
            ],
        ]);

        register_rest_route(Config::getInstance()->getSetting('app_rest_namespace'), '/user/delete-account', [
            [
                'methods'             => \WP_REST_Server::CREATABLE,
                'callback'            => [UserCtrl::class, 'deleteOwnAccount'],
                'args'                => [],
                'permission_callback' => [RestPermissions::class, 'isLoggedIn'],
            ],
        ]);

        register_rest_route(Config::getInstance()->getSetting('app_rest_namespace'), '/user/update-meta', [
            [
                'methods'             => \WP_REST_Server::CREATABLE,
                'callback'            => [UserCtrl::class, 'updateUserMeta'],
                'args'                => [],
                'permission_callback' => [RestPermissions::class, 'isLoggedIn'],
            ],
        ]);
    }
}