<?php

if (! defined('ABSPATH')) {
    exit;
} require_once __DIR__ . '/class-puffergo-blog-archive-data.php';
require_once __DIR__ . '/trait-puffergo-hierarchical-taxonomy-nav.php';
class PufferGo_CPT_Archive_Data
{
    use PufferGo_Hierarchical_Taxonomy_Nav;

    public $post_type = '';

    public $primary_taxonomy = '';

    public $page_title = '';

    public $page_subtitle = '';

    public $page_description = '';

    public $is_category_page = false;

    public $active_category_id = 0;

    public $current_category = null;

    public $current_url = '';

    public $archive_url = '';

    public $hero_bg_image = '';

    public $hero_bg_image_mobile = '';

    public $hero_enabled = true;

    public $categories = array();

    public $search_query = '';

    public $category_slug = '';

    public $paged = 1;

    public $posts = array();

    public $max_pages = 0;

    public $found_posts = 0;

    public $pagination_base_url = '';

    public $posts_per_page = 9;

    public static function from_current_query()
    {
        return new self();
    }

    public function __construct()
    {
        $this->resolve_post_type_and_term();
        $this->load_page_context();
        $this->load_categories();
        $this->load_posts();
        $this->load_pagination();
    }

    private function resolve_post_type_and_term()
    {
        $qo = get_queried_object();

        if ($qo instanceof WP_Post_Type) {
            $this->post_type = $qo->name;
            $this->is_category_page = false;
        } elseif ($qo instanceof WP_Term) {
            $this->is_category_page = true;
            $this->current_category = $qo;
            $this->active_category_id = (int) $qo->term_id;
            $this->category_slug = $qo->slug;
            $tax = get_taxonomy($qo->taxonomy);

            if ($tax && ! empty($tax->object_type)) {
                $this->post_type = (string) $tax->object_type[0];
            }
        } $taxonomies = get_object_taxonomies($this->post_type, 'objects');

        foreach ($taxonomies as $tax) {
            if (! empty($tax->hierarchical)) {
                $this->primary_taxonomy = $tax->name;
                break;
            }
        }
    }

    private function load_page_context()
    {
        $pto = get_post_type_object($this->post_type);
        $archive_link = get_post_type_archive_link($this->post_type);
        $this->archive_url = $archive_link ? $archive_link : '';

        if ($this->is_category_page && $this->current_category) {
            $this->page_title = (string) $this->current_category->name;
            $this->page_description = term_description();
            $term_link = get_term_link($this->current_category);
            $this->current_url = is_wp_error($term_link) ? $this->archive_url : $term_link;
        } else {
            $this->page_title = $pto && isset($pto->labels->name) ? (string) $pto->labels->name : '';
            $this->page_subtitle = $this->default_subtitle_for($this->post_type);
            $this->current_url = $this->archive_url;
            $this->search_query = isset($_GET['s']) ? sanitize_text_field(wp_unslash($_GET['s'])) : '';
        } $layout_id = $this->resolve_layout_id();

        if ('' !== $layout_id && function_exists('puffergo_get_template_config_values')) {
            $config = puffergo_get_template_config_values($layout_id);
            $hero = isset($config['hero-block']) && is_array($config['hero-block']) ? $config['hero-block'] : array();

            if (! empty($hero)) {
                $this->hero_enabled = 'off' !== ($hero['enabled'] ?? 'on');

                if (! empty($hero['title']) && ! $this->is_category_page) {
                    $this->page_title = $hero['title'];
                }

if (! empty($hero['subtitle']) && ! $this->is_category_page) {
                    $this->page_subtitle = $hero['subtitle'];
                }

if (! empty($hero['bg_image']['url'])) {
                    $this->hero_bg_image = $hero['bg_image']['url'];
                }

if (! empty($hero['bg_image_mobile']['url'])) {
                    $this->hero_bg_image_mobile = $hero['bg_image_mobile']['url'];
                }
            }
        } $this->paged = isset($_GET['paged']) ? absint(wp_unslash($_GET['paged'])) : max(1, (int) get_query_var('paged'));
    }

    private function resolve_layout_id()
    {
        $map = array('puffergo_solution' => 'solution-archive', 'puffergo_case' => 'case-archive', 'puffergo_testimonial' => 'testimonial-archive', );

        return isset($map[ $this->post_type ]) ? $map[ $this->post_type ] : '';
    }

    private function default_subtitle_for($post_type)
    {
        switch ($post_type) {
            case 'puffergo_solution': return __('Tailored solutions designed for your industry.', 'puffergo');
            case 'puffergo_case': return __('Real-world results from our partner clients.', 'puffergo');
            case 'puffergo_testimonial': return __('What our customers say about working with us.', 'puffergo');
            default: return '';
        }
    }

    private function load_categories()
    {
        if ('' === $this->primary_taxonomy) {
            return;
        } $parent_id = $this->is_category_page ? $this->active_category_id : 0;
        $this->categories = $this->get_direct_child_terms($this->primary_taxonomy, $parent_id);
    }

    private function load_posts()
    {
        $args = array('post_type' => $this->post_type, 'post_status' => 'publish', 'posts_per_page' => $this->posts_per_page, 'paged' => $this->paged, );

        if (! empty($this->search_query)) {
            $args['s'] = $this->search_query;
        }

if ($this->is_category_page && $this->current_category) {
            $args['tax_query'] = array(array('taxonomy' => $this->current_category->taxonomy, 'field' => 'term_id', 'terms' => array($this->active_category_id), ), );
        } $query = new WP_Query($args);
        $this->max_pages = (int) $query->max_num_pages;
        $this->found_posts = (int) $query->found_posts;

        while ($query->have_posts()) {
            $query->the_post();
            $pid = get_the_ID();
            $card = PufferGo_Blog_Archive_Data::build_post_card($pid);
            $card = $this->patch_card_for_cpt($card, $pid);
            $this->posts[] = $card;
        } wp_reset_postdata();
    }

    private function patch_card_for_cpt(array $card, $post_id)
    {
        $card['author'] = '';
        $card['date'] = '';
        $card['category_name'] = '';
        $card['category_slug'] = '';
        $card['category_style'] = array('color' => '#666', 'bgColor' => '#f0f0f1');

        if ('' !== $this->primary_taxonomy) {
            $terms = get_the_terms($post_id, $this->primary_taxonomy);

            if (! empty($terms) && ! is_wp_error($terms)) {
                $card['category_name'] = (string) $terms[0]->name;
                $card['category_slug'] = (string) $terms[0]->slug;

                if (function_exists('puffergo_get_category_color_style')) {
                    $card['category_style'] = puffergo_get_category_color_style($terms[0]->slug);
                }
            }
        }

return $card;
    }

    private function load_pagination()
    {
        $this->pagination_base_url = $this->current_url;

        if (! empty($this->search_query) && ! $this->is_category_page) {
            $this->pagination_base_url = add_query_arg('s', $this->search_query, $this->current_url);
        }
    }

    public function get_breadcrumb_items()
    {
        $items = array();
        $archive_label = class_exists('PufferGo_Frontend_I18n') ? PufferGo_Frontend_I18n::cpt_archive_label($this->post_type) : '';

        if ('' !== $this->archive_url && '' !== $archive_label) {
            $items[] = array('url' => $this->archive_url, 'label' => $archive_label, );
        }

if ($this->is_category_page && $this->current_category) {
            $items = array_merge($items, $this->get_term_ancestor_breadcrumb_items($this->current_category->taxonomy, $this->active_category_id));
            $items[] = array('url' => null, 'label' => (string) $this->current_category->name, );
        }

return $items;
    }
}
