<?php

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

    public $page_title;

    public $page_description = '';

    public $page_subtitle = '';

    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->load_page_context();
        $this->load_categories();
        $this->load_posts();
        $this->load_pagination();
    }

    private function load_page_context()
    {
        $this->is_category_page = is_category();

        if ($this->is_category_page) {
            $this->current_category = get_queried_object();
            $this->category_slug = $this->current_category->slug ?? '';
            $this->active_category_id = $this->current_category->term_id ?? 0;
            $this->page_title = single_term_title('', false);
            $this->page_description = term_description();
            $this->current_url = get_category_link($this->active_category_id);
        } else {
            $this->category_slug = isset($_GET['category']) ? sanitize_text_field($_GET['category']) : '';
            $this->page_title = __('Blog', 'puffergo');
            $this->page_subtitle = __('Sharing insights on global branding, tech architecture, and user experience.', 'puffergo');
            $this->current_url = home_url(add_query_arg(null, null));
            $this->current_url = remove_query_arg(array('s', 'category', 'paged'), $this->current_url);
        } $this->hero_bg_image = get_theme_mod('puffergo_blog_hero_bg', '');
        $this->hero_bg_image_mobile = get_theme_mod('puffergo_blog_hero_bg_mobile', '');
        $config = function_exists('puffergo_get_template_config_values') ? puffergo_get_template_config_values('blog-archive') : array();
        $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->search_query = isset($_GET['s']) ? sanitize_text_field($_GET['s']) : '';
        $this->paged = isset($_GET['paged']) ? absint($_GET['paged']) : max(1, get_query_var('paged'));
        $this->archive_url = get_post_type_archive_link('post');

        if (empty($this->archive_url)) {
            $this->archive_url = home_url('/blog');
        }
    }

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

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

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

if (! empty($this->category_slug)) {
            if ($this->is_category_page && $this->current_category) {
                $query_args['cat'] = $this->current_category->term_id;
            } else {
                $query_args['category_name'] = $this->category_slug;
            }
        } $query = new WP_Query($query_args);
        $this->max_pages = $query->max_num_pages;
        $this->found_posts = $query->found_posts;

        if ($query->have_posts()) {
            while ($query->have_posts()) {
                $query->the_post();
                $this->posts[] = self::build_post_card(get_the_ID());
            }
        } wp_reset_postdata();
    }

    public static function build_post_card($post_id, $default_image_url = '', $default_image_alt = '')
    {
        $categories = get_the_category($post_id);
        $primary_category = ! empty($categories) ? $categories[0] : null;
        $category_slug = $primary_category ? $primary_category->slug : '';
        $category_style = puffergo_get_category_color_style($category_slug);
        $thumbnail_id = get_post_thumbnail_id($post_id);
        $image_url = $thumbnail_id ? get_the_post_thumbnail_url($post_id, 'large') : $default_image_url;
        $image_alt = $thumbnail_id ? get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true) : ($default_image_alt ?: get_the_title($post_id));

        return array('id' => $post_id, 'title' => get_the_title($post_id), 'url' => get_permalink($post_id), 'image_url' => $image_url, 'image_alt' => $image_alt ?: get_the_title($post_id), 'excerpt' => wp_trim_words(get_the_excerpt($post_id), 50, '...'), 'author' => get_the_author_meta('display_name', get_post_field('post_author', $post_id)), 'date' => get_the_date(get_option('date_format'), $post_id), 'category_name' => $primary_category ? $primary_category->name : '', 'category_slug' => $category_slug, 'category_style' => $category_style, );
    }

    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();
        $items[] = array('url' => $this->archive_url, 'label' => __('Blog', 'puffergo'), );

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

return $items;
    }
}
