<?php
namespace dpcdCategoryDesign;

use dpcdCategoryDesign\Helper;

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

class shortCode {

    private static $instance = null;

    public static function get_instance() {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        add_shortcode('dpcd_categories_design', [$this, 'product_category_design']);
    }

    public function product_category_design( $atts, $content = null ) {
        $settings = shortcode_atts([
            'id' => '',
        ], $atts);

        $post_id = isset($settings['id']) ? absint($settings['id']) : 0;
        if ($post_id === 0) {
            return '';
        }

        // Retrieve & sanitize post meta
        $featured_cats = get_post_meta($post_id, 'wc_pcd_featured_cats', true);
        $featured_cats = is_array($featured_cats) ? array_map('sanitize_text_field', $featured_cats) : [];
        $cat_layout = sanitize_text_field(get_post_meta($post_id, 'wc_pcd_layout', true));
        $all_settings = get_post_meta($post_id);

        // Sanitize & validate user settings
        $content_position = isset($all_settings['wc_pcd_content_position']) ? sanitize_text_field($all_settings['wc_pcd_content_position'][0]) : 'below-content';
        $hide_empty_category = isset($all_settings['wc_pcd_hide_empty_cats']) ? filter_var($all_settings['wc_pcd_hide_empty_cats'][0], FILTER_VALIDATE_BOOLEAN) : false;
        $valid_order_values = ['ASC', 'DESC'];
        $order_by = isset($all_settings['wc_pcd_select_category_order']) && in_array($all_settings['wc_pcd_select_category_order'][0], $valid_order_values, true) 
            ? sanitize_text_field($all_settings['wc_pcd_select_category_order'][0]) 
            : 'ASC';

        // Prepare category query arguments
        $args = [
            'hide_empty' => $hide_empty_category,
            'slug'       =>  $featured_cats,
            'order'      => $order_by,
        ];
        $pcd_cats = get_terms('product_cat', $args);

        // Output only if categories exist
        if (!empty($pcd_cats) && !is_wp_error($pcd_cats)) {
            ob_start();
            echo '<section id="pcd-content-section-' . esc_attr($post_id) . '" class="pcd-content-section pcd-position-' . esc_attr($content_position) . ' pcd-' . esc_attr($cat_layout) . '">';
            
           // Define allowed layout templates
            $allowed_templates = [
                'layout_grid'   => DPCD_PATH . '/pcd-templates/template-grid.php',
                'layout_slider' => DPCD_PATH . '/pcd-templates/template-slider.php',
            ];

            // Ensure only allowed templates are included
            if (array_key_exists($cat_layout, $allowed_templates)) {
                include $allowed_templates[$cat_layout];
            } else {
                error_log('Invalid layout template requested: ' . esc_html($cat_layout)); // Log invalid attempts
            }

            echo '</section>';
            return ob_get_clean();
        }

        return '';
    }
}

shortCode::get_instance();