<?php
/**
 * View helper functions for search template
 *
 * Contains helper functions for premium checks, category availability,
 * and other view-related utilities.
 */

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

/**
 * Check if premium (Freemius)
 *
 * @return bool
 */
function psfa_is_premium() {
    $is_premium = false;
    if (function_exists('psfa_fs')) {
        // Check if user can use premium code (has valid license or is in trial)
        // Use can_use_premium_code() for feature gating, is_premium() for code detection
        $is_premium = psfa_fs()->can_use_premium_code();
    }
    /**
     * Filter to override premium status (for demo mode, testing, etc.)
     *
     * @param bool $is_premium Current premium status
     * @return bool Modified premium status
     */
    return apply_filters('psfa_is_premium', $is_premium);
}

/**
 * Get max pages based on license
 *
 * @return int
 */
function psfa_get_max_pages() {
    
    return 5; // Free version limited to 5 pages
}

/**
 * Get available categories based on license
 *
 * @return array
 */
function psfa_get_available_categories() {
    $all_categories = array(
        'Electronics',
        'Books',
        'Fashion',
        'ToysAndGames',
        'HomeGarden',
        'TodaysDeals',
        'Automotive',
        'Baby',
        'Beauty',
        'Computers',
        'Grocery',
        'HealthPersonalCare',
        'HomeImprovement',
        'Industrial',
        'KindleStore',
        'Luggage',
        'MoviesTV',
        'Music',
        'MusicalInstruments',
        'OfficeProducts',
        'PetSupplies',
        'Software',
        'SportsOutdoors',
        'ToolsHomeImprovement',
        'VideoGames',
        'Watches'
    );
    
    
    // Free version limited to 5 categories
    return array(
        'Electronics',
        'Books',
        'Fashion',
        'ToysAndGames',
        'HomeGarden'
    );
}

/**
 * Check if category is available
 *
 * @param string $category
 * @return bool
 */
function psfa_is_category_available($category) {
    $available = psfa_get_available_categories();
    return in_array($category, $available, true);
}

/**
 * Map category slugs to proper category names
 *
 * @return array
 */
function psfa_get_category_map() {
    return array(
        'fashion' => 'Fashion',
        'electronics' => 'Electronics',
        'books' => 'Books',
        'toysandgames' => 'ToysAndGames',
        'toys' => 'ToysAndGames',
        'homegarden' => 'HomeGarden',
        'home' => 'HomeGarden',
        'todaysdeals' => 'TodaysDeals',
        'deals' => 'TodaysDeals'
    );
}

/**
 * Normalize category from slug or name
 *
 * @param string $raw_category
 * @return string
 */
function psfa_normalize_category($raw_category) {
    $category_map = psfa_get_category_map();
    $normalized = strtolower(trim($raw_category));
    
    if (isset($category_map[$normalized])) {
        return $category_map[$normalized];
    }
    
    return ucfirst($normalized);
}

/**
 * Check if branding should be shown
 *
 * Free version always shows branding. Premium users can enable white-label to hide it.
 *
 * @return bool
 */
function psfa_show_branding() {
    
    return true; // Free version always shows branding
}

/**
 * Render category upgrade notice as a yellow call-to-action
 *
 * @param string $upgrade_url Upgrade URL
 * @return string HTML for upgrade notice
 */
function psfa_render_category_upgrade_notice($upgrade_url) {
    $upgrade_url = esc_url($upgrade_url);
    return '<div class="tw:my-5 tw:p-5 tw:bg-[var(--brand-warning-light)] tw:border-l-4 tw:border-solid tw:border-[var(--brand-warning)] tw:rounded-xs tw:shadow-xs">
        <div class="tw:flex tw:items-center tw:justify-between tw:flex-wrap tw:gap-4">
            <div class="tw:flex-1 tw:min-w-[200px]">
                <strong class="tw:block tw:text-base tw:text-[#856404] tw:mb-2">🔒 Premium Category</strong>
                <p class="tw:m-0 tw:text-[#856404] tw:text-sm">This category is available in Pro/Premium plans. Upgrade to unlock access to all 26 Amazon categories!</p>
            </div>
            <div>
                <a href="' . $upgrade_url . '" target="_blank" class="tw:bg-[#2271b1] tw:text-white tw:py-3 tw:px-5 tw:text-sm tw:font-semibold tw:no-underline tw:rounded-xs tw:inline-block tw:transition-colors tw:hover:bg-[#135e96]">
                    Upgrade to Premium
                </a>
            </div>
        </div>
    </div>';
}

