<?php
/**
 * Paid-only contract for the One-click Store Type presets.
 *
 * This class deliberately contains no UI or ranking logic. Those later paid
 * modules consume this registry and the selected type without making Free
 * code aware of individual store-type presets.
 */
class BeRocket_AAPF_Paid_One_Click_Store_Types {
    const OPTION_NAME = 'br_aapf_one_click_store_type';
    const FILTER_META_STORE_TYPE = '_br_aapf_one_click_store_type';
    const DEFAULT_TYPE = 'general';
    const MINIMUM_CAPABILITY = 10;

    /**
     * Stable preset IDs. Labels and descriptions are presentation-only and
     * may be translated without changing a stored selection.
     */
    public static function get_types() {
        $types = array(
            self::DEFAULT_TYPE => array(
                'label' => __('General store', 'BeRocket_AJAX_domain'),
                'description' => __('Use the catalog-wide recommendations.', 'BeRocket_AJAX_domain'),
                'icon' => 'dashicons-cart',
            ),
            'fashion' => array(
                'label' => __('Fashion', 'BeRocket_AJAX_domain'),
                'description' => __('Prioritize size, color, gender, and material.', 'BeRocket_AJAX_domain'),
                'icon' => 'dashicons-admin-appearance',
            ),
            'electronics' => array(
                'label' => __('Electronics', 'BeRocket_AJAX_domain'),
                'description' => __('Prioritize memory, screen size, processor, and rating.', 'BeRocket_AJAX_domain'),
                'icon' => 'dashicons-laptop',
            ),
            'furniture' => array(
                'label' => __('Furniture', 'BeRocket_AJAX_domain'),
                'description' => __('Prioritize material, color, and room or style attributes.', 'BeRocket_AJAX_domain'),
                'icon' => 'dashicons-admin-home',
            ),
            'auto_parts' => array(
                'label' => __('Auto parts', 'BeRocket_AJAX_domain'),
                'description' => __('Prioritize compatible vehicle and part attributes.', 'BeRocket_AJAX_domain'),
                'icon' => 'dashicons-car',
            ),
            'cosmetics' => array(
                'label' => __('Cosmetics', 'BeRocket_AJAX_domain'),
                'description' => __('Prioritize shade, skin type, and product concerns.', 'BeRocket_AJAX_domain'),
                'icon' => 'dashicons-art',
            ),
        );
        $default_type = $types[self::DEFAULT_TYPE];
        $types = apply_filters('brapf_one_click_paid_store_types', $types);
        // A third-party registry extension must not make the saved default
        // invalid or allow an arbitrary option value to pass sanitization.
        $types = is_array($types) ? $types : array();
        if (!isset($types[self::DEFAULT_TYPE])) {
            $types[self::DEFAULT_TYPE] = $default_type;
        }
        return $types;
    }

    /** Store types are a Pro feature and are also available in Business. */
    public static function is_available($capability = null) {
        return in_array(self::get_tier($capability), array('pro', 'business'), true);
    }

    /** Resolve only the tiers which may use this paid module. */
    public static function get_tier($capability = null) {
        if ($capability === null) {
            if (class_exists('BeRocket_AAPF_One_Click_Capabilities')) {
                return BeRocket_AAPF_One_Click_Capabilities::get_tier();
            }
            $capability = apply_filters('brfr_get_plugin_version_capability_ajax_filters', 0);
        }
        $capability = (int)$capability;
        if ($capability >= 100) {
            return 'business';
        }
        return $capability >= self::MINIMUM_CAPABILITY ? 'pro' : 'free';
    }

    /** Return a stable, allow-listed ID; invalid and missing values are General. */
    public static function normalize_type($type) {
        $type = sanitize_key($type);
        return self::is_known_type($type) ? $type : self::DEFAULT_TYPE;
    }

    /** Use this for request validation when an invalid value must be rejected. */
    public static function is_known_type($type) {
        $types = self::get_types();
        return isset($types[sanitize_key($type)]);
    }

    /** Reading the default does not create an option or add an autoloaded row. */
    public static function get_selected_type() {
        return self::normalize_type(get_option(self::OPTION_NAME, self::DEFAULT_TYPE));
    }

    /**
     * Persist a valid selection. The first write explicitly creates a
     * non-autoloaded option for compatibility with older WordPress versions.
     */
    public static function save_selected_type($type) {
        $type = self::normalize_type($type);
        if (get_option(self::OPTION_NAME, false) === false) {
            add_option(self::OPTION_NAME, $type, '', 'no');
        } else {
            update_option(self::OPTION_NAME, $type, false);
        }
        return $type;
    }

    /** Free always receives the generic behavior even if an old option exists. */
    public static function get_active_type($capability = null) {
        return self::is_available($capability) ? self::get_selected_type() : self::DEFAULT_TYPE;
    }

    /**
     * Cache a recommendation set per paid Store Type while retaining the same
     * catalog snapshot. Free keeps its current cache context unchanged.
     */
    public static function add_recommendation_cache_context($context, $tier, $capability) {
        if (!self::is_available($capability)) {
            return $context;
        }
        $context = is_array($context) ? $context : array();
        $context['store_type'] = self::get_active_type($capability);
        return $context;
    }

    /** Record profile provenance only on a newly created definition, never on reuse. */
    public static function mark_filter_origin($filter_id, $definition, $setup_id) {
        $type = self::get_active_type();
        if ($type === self::DEFAULT_TYPE) {
            return;
        }
        update_post_meta(absint($filter_id), self::FILTER_META_STORE_TYPE, $type);
    }
}

add_filter(
    'brapf_one_click_recommendation_cache_context',
    array('BeRocket_AAPF_Paid_One_Click_Store_Types', 'add_recommendation_cache_context'),
    10,
    3
);
add_action(
    'brapf_one_click_filter_definition_created',
    array('BeRocket_AAPF_Paid_One_Click_Store_Types', 'mark_filter_origin'),
    10,
    3
);
