<?php
/**
 * License Endpoint for BoostBot REST API.
 *
 * @package BoostBot
 */

// Prevent direct access to the file.
if (!defined('ABSPATH')) {
    exit;
}

/**
 * BoostBot_License_Endpoint class.
 *
 * Handles license status and feature queries.
 *
 * @since 1.0.0
 */
class BoostBot_License_Endpoint extends BoostBot_Base_Endpoint
{
    /**
     * The base path for this endpoint.
     *
     * @since 1.0.0
     * @access protected
     * @var string $rest_base
     */
    protected $rest_base = 'license';

    /**
     * Register the routes for this endpoint.
     *
     * @since 1.0.0
     */
    public function register()
    {
        // GET /wp-json/boostbot/v1/license/status - Get license status
        $this->register_route(
            '/status',
            'GET',
            [$this, 'get_status'],
            array( $this, 'public_with_api_rate_limit' ) // Passive public read
        );

        // GET /wp-json/boostbot/v1/license/features - Get features list
        $this->register_route(
            '/features',
            'GET',
            [$this, 'get_features'],
            array( $this, 'public_with_api_rate_limit' )
        );
    }

    /**
     * Get license status.
     *
     * @since 1.0.0
     * @param WP_REST_Request $request The REST request object.
     * @return WP_REST_Response The response.
     */
    public function get_status($request)
    {
        BoostBot_Logger::debug('Getting license status');

        // Legacy subscription licensing is deprecated.
        // Keep endpoint for backward compatibility, but always return "Pro unlocked".
        $status = 'pro';
        $is_pro = true;
        $settings_frozen = false;
        $upgrade_url = '';
        $dev_mode = false;

        return $this->success([
            'status'           => $status,
            'is_pro'           => $is_pro,
            'dev_mode'         => $dev_mode,
            'settings_frozen'  => $settings_frozen,
            'upgrade_url'      => $upgrade_url,
            'support_email'    => 'office@boost-media.co.il',
        ]);
    }

    /**
     * Get features list with enabled/disabled status.
     *
     * @since 1.0.0
     * @param WP_REST_Request $request The REST request object.
     * @return WP_REST_Response The response.
     */
    public function get_features($request)
    {
        BoostBot_Logger::debug('Getting features list');

        // Define all features
        $all_features = [
            'homepage_chat'      => [
                'name'        => __('Homepage Chat', 'boostbot'),
                'description' => __('Chat widget on homepage', 'boostbot'),
                'free'        => true,
            ],
            'all_pages'          => [
                'name'        => __('All Pages Chat', 'boostbot'),
                'description' => __('Chat widget on all pages', 'boostbot'),
                'free'        => false,
            ],
            'basic_settings'     => [
                'name'        => __('Basic Settings', 'boostbot'),
                'description' => __('Basic chatbot configuration', 'boostbot'),
                'free'        => true,
            ],
            'widgets'            => [
                'name'        => __('Interactive Widgets', 'boostbot'),
                'description' => __('Forms, calendars, and more', 'boostbot'),
                'free'        => false,
            ],
            'woocommerce'        => [
                'name'        => __('WooCommerce Integration', 'boostbot'),
                'description' => __('Product recommendations & cart', 'boostbot'),
                'free'        => false,
            ],
            'analytics'          => [
                'name'        => __('Analytics & Reports', 'boostbot'),
                'description' => __('Detailed analytics dashboard', 'boostbot'),
                'free'        => false,
            ],
            'leads_management'   => [
                'name'        => __('Leads Management', 'boostbot'),
                'description' => __('Full lead tracking & export', 'boostbot'),
                'free'        => false,
            ],
            'knowledge_base'     => [
                'name'        => __('Knowledge Base (RAG)', 'boostbot'),
                'description' => __('AI learns your site content', 'boostbot'),
                'free'        => false,
            ],
            'triggers'           => [
                'name'        => __('Smart Triggers', 'boostbot'),
                'description' => __('Auto-trigger chatbot on events', 'boostbot'),
                'free'        => false,
            ],
            'webhooks'           => [
                'name'        => __('Webhooks', 'boostbot'),
                'description' => __('Connect to external services', 'boostbot'),
                'free'        => false,
            ],
            'advanced_styling'   => [
                'name'        => __('Advanced Styling', 'boostbot'),
                'description' => __('Custom CSS and themes', 'boostbot'),
                'free'        => false,
            ],
        ];

        // Legacy subscription licensing is deprecated.
        $is_pro = true;

        // Mark enabled/disabled
        $enabled = [];
        $disabled = [];

        foreach ($all_features as $key => $feature) {
            $feature['key'] = $key;
            $feature['enabled'] = true;

            if ($feature['enabled']) {
                $enabled[$key] = $feature;
            } else {
                $disabled[$key] = $feature;
            }
        }

        return $this->success([
            'is_pro'   => $is_pro,
            'enabled'  => $enabled,
            'disabled' => $disabled,
            'all'      => $all_features,
        ]);
    }
}



