<?php 
/**
 * Class ST_Proqyz_Admin_Page_Course_Access
 */
defined( 'ABSPATH' ) || exit;

class ST_ProQyz_Admin_Page_Course_Access {
    /**
    * Hook in tabs.
    */
    public function __construct() {
        $this->hooks();
        $this->includes();
    }

    /**
      * register hooks
      */
    public function hooks() { }

    /**
      * includes
      */
    public function includes() { }

    public static function get_page_layout(){
        require_once ST_PROQYZ_ABSPATH . "/includes/templates/admin/course-access/course-access.php"; // checked
    }

    public static function get_course_list($params = []) {
        global $wpdb;
        // Define the query arguments
        $args = array(
            'post_type'  => 'product', // or your custom post type
            'meta_query' => array(
                array(
                    'key'   => 'product_type',
                    'value' => 'proqyz-course',
                ),
            ),
            'posts_per_page' => -1, // Fetch all posts
        );

        // Create a new instance of WP_Query
        $query = new WP_Query($args);

        // Initialize an empty array to store the posts
        $posts_array = array();

        // Check if there are any posts matching the query
        if ($query->have_posts()) {
            // Loop through the posts
            while ($query->have_posts()) {
                $query->the_post();

                // Collect post data into an array
                $posts_array[] = (object) array(
                    'ID'         => get_the_ID(),
                    'title'      => get_the_title(),
                    'excerpt'    => get_the_excerpt(),
                    'permalink'  => get_permalink(),
                    'api_id'     => get_post_meta(get_the_ID(), '_sku', true), // Get all meta fields
                );
            }

            // Restore original Post Data
            wp_reset_postdata();
        }

        // Return the array of posts
        return $posts_array;

    }

    public static function check_if_post_exists($sku) {
        // Set up the query arguments
        $args = array(
            'post_type'  => 'product', // Specify the post type
            'meta_query' => array(
                'relation' => 'AND', // Ensure both meta queries must be true
                array(
                    'key'   => '_sku',
                    'value' => $sku,
                    'compare' => '='
                ),
                array(
                    'key'   => 'product_type',
                    'value' => 'proqyz-course',
                    'compare' => '='
                )
            ),
        );
    
        // Create a new instance of WP_Query
        $query = new WP_Query($args);
    
        // Check if there are any posts matching the query
        if ($query->have_posts()) {
            // Post exists
            $post_id = get_the_ID(); // Gets the ID of the current post
            wp_reset_postdata();
            return $post_id;
        } else {
            // No post found
            return false;
        }
    }

    public static function trash_unwanted_posts($excluded_ids = []) {
        // Define the meta key and meta value
        $meta_key = 'product_type';
        $meta_value = 'proqyz-course';
    
        // Query to get products with the specified meta key and value
        $args = array(
            'post_type'         => 'product',
            'meta_key'          => $meta_key,
            'meta_value'        => $meta_value,
            'post_status'       => 'publish',
            'posts_per_page'    => -1, // Get all matching products
            'fields'            => 'ids', // Return only the post IDs
        );

        $query = new WP_Query($args);
    
        if ($query->have_posts()) {
            foreach ($query->posts as $post_id) {
                // Check if the current post ID is in the excluded array
                if (!in_array((int) $post_id, $excluded_ids) || !in_array((string) $post_id, $excluded_ids)) {
                    // Trash the post if it's not in the excluded array
                    wp_trash_post($post_id);
                }
            }
        }
    
        // Reset post data
        wp_reset_postdata();
    }

}
