<?php
/**
 * Helper functions for Turbo Starter Templates
 *
 * @package TurboStarterTemplates
 */

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

/**
 * Fetch parent categories from remote API with caching
 *
 * @return array
 */
if ( ! function_exists( 'trstemp_get_parent_categories' ) ) {

    function trstemp_get_parent_categories() {

        $cache_key = 'trstemp_parent_categories_v1.0.5';
        $cached    = get_transient( $cache_key );

        // Return cached data if exists
        if ( false !== $cached ) {
            return $cached;
        }

        $response = wp_remote_get(
            'https://templates.wp-turbo.com/api/v1/categories',
            [
                'timeout' => 15,
            ]
        );

        // If API error
        if ( is_wp_error( $response ) ) {
            return [];
        }

        // Validate response code
        $response_code = wp_remote_retrieve_response_code( $response );
        if ( 200 !== $response_code ) {
            return [];
        }

        $body = wp_remote_retrieve_body( $response );
        $json = json_decode( $body, true );

        // Validate JSON structure
        if ( empty( $json['success'] ) || empty( $json['data'] ) || ! is_array( $json['data'] ) ) {
            return [];
        }

        // Filter only parent + active categories
        $parents = array_values(
            array_filter( $json['data'], function ( $cat ) {
                return empty( $cat['parent_id'] ) && ! empty( $cat['is_active'] );
            })
        );

        // Cache for 6 hours
        set_transient( $cache_key, $parents, 6 * HOUR_IN_SECONDS );

        return $parents;
    }
}

/**
 * Get fallback icons & descriptions for categories
 *
 * @return array
 */
if ( ! function_exists( 'trstemp_get_fallback_data' ) ) {

    function trstemp_get_fallback_data() {

        $icon_map = [
            'business'       => '🏢',
            'local-business' => '🏪',
            'professional'   => '👔',
            'personal'       => '👤',
            'personal-site'  => '🌐',
            'community'      => '🤝',
            'ecommerce'      => '🛒',
            'blog'           => '📝',
        ];

        $description_map = [
            'business' => __( 'Corporate websites, agencies, consulting firms, and professional services', 'turbo-templates-library-for-elementor' ),

            'local-business' => __( 'Restaurants, salons, gyms, clinics, and local service businesses', 'turbo-templates-library-for-elementor' ),

            'professional' => __( 'Doctors, lawyers, coaches, designers, and consultants', 'turbo-templates-library-for-elementor' ),

            'personal' => __( 'Personal portfolios, resumes, and creative profiles', 'turbo-templates-library-for-elementor' ),

            'personal-site' => __( 'Influencers, bio links, and personal branding sites', 'turbo-templates-library-for-elementor' ),

            'community' => __( 'Non-profits, schools, events, and organizations', 'turbo-templates-library-for-elementor' ),

            'ecommerce' => __( 'Online stores and WooCommerce-powered shops', 'turbo-templates-library-for-elementor' ),

            'blog' => __( 'Blogs, magazines, and content-focused websites', 'turbo-templates-library-for-elementor' ),
        ];

        return [
            'icon_map'        => $icon_map,
            'description_map' => $description_map,
        ];
    }
}
