<?php

if (! defined('ABSPATH')) {
    exit;
} class PufferGo_Product_Data
{
    public $id;

    public $title;

    public $short_description;

    public $price_display = '';

    public $price_data = array();

    public $moq_display = '';

    public $moq_data = array();

    public $lead_time_display = '';

    public $lead_time_data = array();

    public $gallery_items = array();

    public $main_item = null;

    public $main_image_id = 0;

    public $main_image_alt = '';

    public $main_poster_url = '';

    public $main_video_src = '';

    public $specs = array();

    public $cta_visible = true;

    public $cta_text = '';

    public $cta_url = '';

    public $cta_is_anchor = false;

    public $cta_link_attrs = '';

    public $brochure_url = '';

    public $form_enabled = true;

    public $form_config = array();

    public $categories = array();

    public $primary_category = null;

    public static function from_current_post()
    {
        return new self(get_the_ID());
    }

    public function __construct($product_id)
    {
        $this->id = $product_id;
        $this->title = get_the_title($product_id);
        $this->load_categories();
        $this->load_meta();
        $this->parse();
        $this->format();
        $this->load_cta();
        $this->load_contact_form();
    }

    private function load_categories()
    {
        $this->categories = get_the_terms($this->id, 'puffergo_product_cat');
        $this->primary_category = (! empty($this->categories) && ! is_wp_error($this->categories)) ? $this->categories[0] : null;
    }

    private function load_meta()
    {
        $this->short_description = get_post_field('post_excerpt', $this->id);
        $this->brochure_url = get_post_meta($this->id, '_puffergo_brochure_url', true);
    }

    private function parse()
    {
        $price_raw = get_post_meta($this->id, '_puffergo_price', true);
        $moq_raw = get_post_meta($this->id, '_puffergo_moq', true);
        $lead_time_raw = get_post_meta($this->id, '_puffergo_lead_time', true);
        $gallery_raw = get_post_meta($this->id, '_puffergo_gallery', true);
        $specs_raw = get_post_meta($this->id, '_puffergo_specs', true);
        $this->price_data = $price_raw ? json_decode($price_raw, true) : array();
        $this->moq_data = $moq_raw ? json_decode($moq_raw, true) : array();
        $this->lead_time_data = $lead_time_raw ? json_decode($lead_time_raw, true) : array();
        $this->gallery_items = PufferGo_Product_CPT::normalize_gallery($gallery_raw);
        $this->specs = PufferGo_Product_CPT::normalize_specs($specs_raw);
        $thumbnail_id = get_post_thumbnail_id($this->id);

        if (empty($this->gallery_items) && $thumbnail_id) {
            $this->gallery_items = array(array('type' => 'image', 'id' => $thumbnail_id));
        } $this->gallery_items = array_map(array($this, 'enrich_gallery_item'), $this->gallery_items);
        $this->main_item = ! empty($this->gallery_items) ? $this->gallery_items[0] : null;
        $this->main_image_id = $this->main_item ? ($this->main_item['id'] ?? 0) : 0;

        if ($this->main_image_id) {
            $this->main_image_alt = get_post_meta($this->main_image_id, '_wp_attachment_image_alt', true);
        }

if ($this->main_item) {
            $this->main_poster_url = $this->main_item['poster_url'] ?? '';
            $this->main_video_src = $this->main_item['video_src'] ?? '';
        }
    }

    private function enrich_gallery_item($item)
    {
        $is_video = in_array($item['type'], array('video', 'url'), true);
        $video_src = '';
        $poster_url = '';
        $thumb_id = $item['id'] ?? 0;

        if ('url' === $item['type']) {
            $video_src = $item['src'] ?? '';
        } elseif ('video' === $item['type']) {
            $video_src = $thumb_id ? wp_get_attachment_url($thumb_id) : '';
        }

if ($is_video && ! empty($item['poster'])) {
            $poster_url = wp_get_attachment_url($item['poster']);
            $thumb_id = (int) $item['poster'];
        } $item['is_video'] = $is_video;
        $item['video_src'] = $video_src;
        $item['poster_url'] = $poster_url;
        $item['thumb_id'] = $thumb_id;

        return $item;
    }

    private function format()
    {
        $use_formatter = class_exists('PufferGo_Unit_Formatter');

        if ($use_formatter) {
            $this->price_display = PufferGo_Unit_Formatter::format($this->price_data, 'currency');
            $this->moq_display = PufferGo_Unit_Formatter::format($this->moq_data, 'quantity');
            $this->lead_time_display = PufferGo_Unit_Formatter::format($this->lead_time_data, 'time');
        } else {
            $this->price_display = isset($this->price_data['value']) ? $this->price_data['value'] : '';
            $this->moq_display = isset($this->moq_data['value']) ? $this->moq_data['value'] : '';
            $this->lead_time_display = isset($this->lead_time_data['min']) ? $this->lead_time_data['min'] . '-' . $this->lead_time_data['max'] . ' ' . ($this->lead_time_data['unit'] ?? '') : '';
        }
    }

    private function load_cta()
    {
        $cta_raw = get_post_meta($this->id, '_puffergo_cta_button', true);
        $cta_data = $cta_raw ? json_decode($cta_raw, true) : array();
        $this->cta_visible = ! isset($cta_data['visible']) || $cta_data['visible'];
        $this->cta_text = ! empty($cta_data['text']) ? $cta_data['text'] : __('Send Inquiry', 'puffergo');
        $this->cta_url = ! empty($cta_data['url']) ? $cta_data['url'] : '#contact-form';
        $this->cta_is_anchor = strpos($this->cta_url, '#') === 0;
        $this->cta_link_attrs = $this->cta_is_anchor ? '' : ' target="_blank" rel="noopener noreferrer"';
    }

    private function load_contact_form()
    {
        $form_config_raw = get_post_meta($this->id, '_puffergo_contact_form', true);
        $this->form_config = $form_config_raw ? json_decode($form_config_raw, true) : array();
        $this->form_enabled = ! isset($this->form_config['enabled']) || $this->form_config['enabled'];
    }

    public function get_form_block_attrs()
    {
        $attrs = array('title' => ! empty($this->form_config['title']) ? $this->form_config['title'] : __('Send Inquiry', 'puffergo'), 'subtitle' => ! empty($this->form_config['subtitle']) ? $this->form_config['subtitle'] : __('Fill out the form below and we will get back to you as soon as possible.', 'puffergo'), 'buttonText' => ! empty($this->form_config['buttonText']) ? $this->form_config['buttonText'] : __('Send Inquiry', 'puffergo'), );

        if (! empty($this->form_config['fields'])) {
            $attrs['fields'] = $this->form_config['fields'];
        }

if (! empty($this->form_config['successMessage'])) {
            $attrs['successMessage'] = $this->form_config['successMessage'];
        }

if (! empty($this->form_config['successUrl'])) {
            $attrs['successUrl'] = $this->form_config['successUrl'];
        }

return $attrs;
    }

    public function get_related_products($limit = 4)
    {
        $cat_ids = array();

        if (! empty($this->categories) && ! is_wp_error($this->categories)) {
            foreach ($this->categories as $term) {
                $cat_ids[] = (int) $term->term_id;
            }
        } $ids = (new PufferGo_Related_Products($this->id, $cat_ids))->get_ids($limit);
        $use_formatter = class_exists('PufferGo_Unit_Formatter');
        $cards = array();

        foreach ($ids as $pid) {
            $card = PufferGo_Product_CPT::get_card_data($pid, $use_formatter);
            $cards[] = array('id' => $pid, 'title' => get_the_title($pid), 'url' => get_permalink($pid), 'image_id' => (int) $card['display_id'], 'is_video' => $card['is_video'], 'price' => $card['price_display'], );
        }

return $cards;
    }

    public function get_breadcrumb_items()
    {
        $items = array();
        $items[] = array('url' => get_post_type_archive_link('puffergo_product'), 'label' => __('Products', 'puffergo'), );

        if ($this->primary_category) {
            $items[] = array('url' => get_term_link($this->primary_category), 'label' => $this->primary_category->name, );
        } $items[] = array('url' => null, 'label' => $this->title, );

        return $items;
    }
}
