<?php
/**
 * SEO Fury Pro Features — Free Plugin Capability System
 *
 * Defines free-tier limits and provides filter-based extension points
 * for the PRO addon to unlock additional features.
 *
 * @package SEO_Fury
 */

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

class SEO_Fury_Pro_Features {

    /**
     * Free tier limits.
     *
     * Boolean false = feature disabled on free.
     * Boolean true  = feature available on free.
     * Integer       = numeric limit on free.
     *
     * @var array
     */
    private $free_limits = array(
        'max_redirects'         => PHP_INT_MAX,
        'max_404_logs'          => PHP_INT_MAX,
        'block_ai_bots'         => false,
        'advanced_seo'          => false,
        'white_label'           => false,
        'priority_support'      => false,
        'schema_markup'         => false,
        'social_cards_advanced' => false,
        'breadcrumbs'           => false,
        'bulk_edit'             => false,
        'csv_export'            => false,
        'auto_redirects'        => false,
        'internal_linking'      => false,
        'ai_content'            => false,
        'seo_images'            => false,
        'ai_images'             => false,
        'google_analytics'      => false,
        'yandex_metrika'        => false,
        'bing_uet'              => false,
        'custom_analytics'      => false,
        'google_search_console' => true,
        'google_indexing'       => false,
        'rank_tracker'          => false,
        'rank_tracker_keywords' => 100,
        'ai_search_traffic'     => false,
        'backlink_checker'      => false,
        'link_assistant'        => false,
        'content_decay'         => false,
        'broken_links'          => true,
    );

    /**
     * Constructor — no dependencies required.
     *
     * The PRO addon hooks into the filters below to enable PRO features.
     */
    public function __construct() {
        // No license dependency — PRO addon provides that via filters.
    }

    /**
     * Whether the PRO addon is active and licensed.
     *
     * @return bool
     */
    public function is_pro() {
        return (bool) apply_filters( 'seo_fury_is_pro', false );
    }

    /**
     * Whether a specific feature is available.
     *
     * @param string $feature_name Feature identifier.
     * @return bool
     */
    public function has_feature( $feature_name ) {
        if ( $this->is_pro() ) {
            return (bool) apply_filters( 'seo_fury_has_feature', true, $feature_name );
        }

        $default = isset( $this->free_limits[ $feature_name ] ) ? $this->free_limits[ $feature_name ] : false;

        return (bool) apply_filters( 'seo_fury_has_feature', $default, $feature_name );
    }

    /**
     * Get the limit for a feature.
     *
     * @param string $feature_name Feature identifier.
     * @return int|bool Limit value.
     */
    public function get_limit( $feature_name ) {
        if ( $this->is_pro() ) {
            return apply_filters( 'seo_fury_get_limit', PHP_INT_MAX, $feature_name );
        }

        $default = isset( $this->free_limits[ $feature_name ] ) ? $this->free_limits[ $feature_name ] : 0;

        return apply_filters( 'seo_fury_get_limit', $default, $feature_name );
    }

    /**
     * Get the PRO tier limit for a feature (for display purposes).
     *
     * @param string $feature_name Feature identifier.
     * @return int Limit value (PHP_INT_MAX for unlimited).
     */
    public function get_pro_limit( $feature_name ) {
        return apply_filters( 'seo_fury_get_pro_limit', PHP_INT_MAX, $feature_name );
    }

    /**
     * Whether branding should be hidden (PRO white-label feature).
     *
     * @return bool
     */
    private function should_hide_branding() {
        return function_exists( 'seo_fury_hide_branding' ) ? seo_fury_hide_branding() : false;
    }

    /**
     * Get the upgrade URL.
     *
     * @return string
     */
    private function get_upgrade_url() {
        if ( $this->should_hide_branding() ) {
            return '';
        }
        return apply_filters( 'seo_fury_upgrade_url', 'https://seo-fury.com/#pricing' );
    }

    /**
     * Get usage percentage (current / limit).
     *
     * @param string $feature_name  Feature identifier.
     * @param int    $current_count Current usage count.
     * @return float Percentage 0-100.
     */
    public function get_usage_percent( $feature_name, $current_count ) {
        $limit = $this->get_limit( $feature_name );

        if ( is_bool( $limit ) || $limit <= 0 || $limit === PHP_INT_MAX ) {
            return 0;
        }

        return min( 100, round( ( $current_count / $limit ) * 100, 1 ) );
    }

    /**
     * Get a limit warning HTML if usage is above threshold.
     *
     * @param string $feature_name     Feature identifier.
     * @param int    $current_count    Current usage count.
     * @param int    $threshold_percent Show warning when usage exceeds this (default 80%).
     * @return string HTML warning or empty string.
     */
    public function get_limit_warning( $feature_name, $current_count, $threshold_percent = 80 ) {
        $limit = $this->get_limit( $feature_name );

        if ( is_bool( $limit ) || $limit <= 0 || $limit === PHP_INT_MAX ) {
            return '';
        }

        $percent = $this->get_usage_percent( $feature_name, $current_count );

        if ( $percent < $threshold_percent ) {
            return '';
        }

        $pro_limit = $this->get_pro_limit( $feature_name );
        $color     = $percent >= 100 ? '#dc3232' : '#f0ad4e';

        $msg = sprintf(
            /* translators: 1: current count, 2: limit, 3: percentage */
            __( 'Using %1$d of %2$d (%3$d%% used).', 'seo-fury' ),
            $current_count,
            $limit,
            (int) $percent
        );

        if ( ! $this->is_pro() && apply_filters( 'seo_fury_show_upgrade_prompts', true ) ) {
            $upgrade_url = $this->get_upgrade_url();

            if ( ! empty( $upgrade_url ) ) {
                $msg .= ' ' . sprintf(
                    /* translators: 1: opening a tag, 2: pro limit, 3: closing a tag */
                    __( '%1$sUpgrade to PRO for %2$s%3$s', 'seo-fury' ),
                    '<a href="' . esc_url( $upgrade_url ) . '" target="_blank" style="font-weight:600;">',
                    $pro_limit === PHP_INT_MAX ? __( 'unlimited', 'seo-fury' ) : number_format_i18n( $pro_limit ),
                    '</a>'
                );
            }
        }

        return '<div style="padding:8px 12px;background:' . esc_attr( $percent >= 100 ? '#fdecea' : '#fff8e5' ) . ';border-left:4px solid ' . esc_attr( $color ) . ';border-radius:3px;margin:10px 0;font-size:13px;">'
             . '<span class="dashicons dashicons-warning" style="color:' . esc_attr( $color ) . ';margin-right:6px;vertical-align:middle;"></span>'
             . wp_kses( $msg, array( 'a' => array( 'href' => array(), 'target' => array(), 'style' => array() ) ) )
             . '</div>';
    }

    /**
     * Whether the feature limit has been reached.
     *
     * @param string $feature_name  Feature identifier.
     * @param int    $current_count Current usage count.
     * @return bool
     */
    public function is_limit_reached( $feature_name, $current_count ) {
        $limit = $this->get_limit( $feature_name );

        if ( is_bool( $limit ) ) {
            return ! $limit;
        }

        return $current_count >= $limit;
    }

    /**
     * Get plan info for display.
     *
     * @return array
     */
    public function get_plan_info() {
        if ( $this->is_pro() ) {
            return array(
                'name'     => __( 'PRO', 'seo-fury' ),
                'features' => array(
                    __( 'Unlimited Redirects', 'seo-fury' ),
                    __( 'Unlimited 404 Monitoring', 'seo-fury' ),
                    __( 'AI Bot Blocking', 'seo-fury' ),
                    __( 'Advanced SEO', 'seo-fury' ),
                    __( 'Schema Markup', 'seo-fury' ),
                    __( 'Social Cards', 'seo-fury' ),
                    __( 'Breadcrumbs', 'seo-fury' ),
                    __( 'White Label', 'seo-fury' ),
                    __( 'Priority Support', 'seo-fury' ),
                    __( 'AI Image Generation', 'seo-fury' ),
                ),
            );
        }

        $plan_info = array(
            'name'     => __( 'FREE', 'seo-fury' ),
            'features' => array(
                __( 'Unlimited Redirects', 'seo-fury' ),
                __( 'Unlimited 404 Logs', 'seo-fury' ),
                __( 'Basic SEO', 'seo-fury' ),
                __( 'Robots.txt', 'seo-fury' ),
                __( 'Sitemap', 'seo-fury' ),
                __( 'Meta Tags', 'seo-fury' ),
                __( 'IndexNow', 'seo-fury' ),
                __( 'Google Search Console', 'seo-fury' ),
                __( 'Google Indexing API', 'seo-fury' ),
                __( 'Page Speed Analysis', 'seo-fury' ),
                __( 'Basic Image SEO', 'seo-fury' ),
            ),
        );

        $upgrade_url = $this->get_upgrade_url();

        if ( ! empty( $upgrade_url ) ) {
            $plan_info['upgrade_url'] = $upgrade_url;
        }

        return $plan_info;
    }

    /**
     * Get a PRO badge HTML string.
     *
     * @param string $feature_name Optional feature name for tooltip.
     * @return string HTML or empty string if PRO.
     */
    public function get_pro_badge( $feature_name = '' ) {
        if ( $this->is_pro() ) {
            return '';
        }

        $tooltip = '';

        if ( ! empty( $feature_name ) && ! $this->should_hide_branding() ) {
            $tooltip = ' title="' . esc_attr( sprintf(
                /* translators: %s: feature name */
                __( 'Upgrade to PRO to unlock %s', 'seo-fury' ),
                $feature_name
            ) ) . '"';
        }

        $badge_html = '<span class="seo-fury-pro-badge"' . $tooltip . '>' . esc_html__( 'PRO', 'seo-fury' ) . '</span>';

        return apply_filters( 'seo_fury_pro_badge_html', $badge_html, $feature_name );
    }

    /**
     * Get a user-friendly limit message.
     *
     * @param string $feature_name Feature identifier.
     * @return string
     */
    public function get_limit_message( $feature_name ) {
        $limit = $this->get_limit( $feature_name );

        if ( is_bool( $limit ) && ! $limit ) {
            if ( $this->should_hide_branding() ) {
                return __( 'This feature requires a PRO license.', 'seo-fury' );
            }

            return sprintf(
                /* translators: 1: opening a tag, 2: closing a tag */
                __( 'This feature requires %1$sPRO license%2$s.', 'seo-fury' ),
                '<a href="' . esc_url( $this->get_upgrade_url() ) . '" target="_blank">',
                '</a>'
            );
        }

        if ( is_numeric( $limit ) ) {
            if ( $this->should_hide_branding() ) {
                return sprintf(
                    /* translators: %d: limit count */
                    __( 'FREE plan limited to %d items. Upgrade to PRO for unlimited.', 'seo-fury' ),
                    $limit
                );
            }

            return sprintf(
                /* translators: 1: limit count, 2: opening a tag, 3: closing a tag */
                __( 'FREE plan limited to %1$d items. %2$sUpgrade to PRO%3$s for unlimited.', 'seo-fury' ),
                $limit,
                '<a href="' . esc_url( $this->get_upgrade_url() ) . '" target="_blank">',
                '</a>'
            );
        }

        return '';
    }
}
