<?php

namespace LoftyIDX\includes\pages;

use LoftyIDX\includes\common\LoftyIDXListingHelper;
use LoftyIDX\includes\common\LoftyIDXHouseHelper;
use LoftyIDX\includes\LoftyIDX;

;

defined('ABSPATH') || exit;

class LoftyIDXFeatureListingPage extends LoftyIDXPage
{
    protected $siteConfig = []; // raw site config from api
    protected $metaTags = '';
    protected $siteInfo = [];
    protected $configInfo = [];
    protected $featureListingName = '';
    protected $isShortCode = false;

    public function __construct($template = '', $options = [])
    {
        parent::__construct($template, $options);
        $this->options = $options;
        $this->isShortCode = isset($this->options['shortcode_config']);
        $this->getMetaTags();
        $this->changePageTitle();
        add_action('wp_head', [$this, 'injectMetaTags']);
    }

    public function getSiteConfig()
    {
        $proxy = LoftyIDX::get_instance()->services['proxy'];
        $site_config = $proxy->get_data('/wp-plugin/admin/featured-listing/init-info');
        if($site_config === false){
            return [];
        }
        return $site_config;
    }

    public function getMetaTags()
    {
        $this->siteConfig = $this->getSiteConfig();;
        if (isset($this->siteConfig['data']['pluginPage']) && !empty($this->siteConfig)) {
            $this->siteInfo = $this->siteConfig['data']['pluginPage'];
            $this->configInfo = $this->siteConfig['data']['pageConfigMap'];
            $this->featureListingName = $this->isShortCode ? $this->options['shortcode_config']['featureListingName']:$this->siteConfig['data']['featureListingName'];
            $tags = $this->isShortCode ? $this->options['shortcode_config']['meta'] : $this->siteInfo['metaTag'];
            $seo_params = $this->siteConfig['data']['seoParamMap'] ?? [];
            $result = preg_replace_callback('/\{([^}]+)}/', function ($matches) use ($seo_params) {
                return $seo_params[$matches[1]] ?? $matches[0];
            }, $tags);
            $this->metaTags = implode(PHP_EOL, LoftyIDXListingHelper::filterMetaTags($result));
            $this->title = LoftyIDXListingHelper::filterTitle($result);
        }
    }

    public function getContextData()
    {
        $mls_list = LoftyIDXListingHelper::get_mls_info();
        $shortcode_config = $this->options['shortcode_config'] ?? [];
        $shortcode_class = isset($shortcode_config['layout']) ? 'lofty-is-shortcode' : '';
        $params = [
            'listingType' => 'featured-listing',
            'featureListingName' => $this->featureListingName,
            'page' => 1,
            'pageSize' => 10,
        ];

        $listingResult = LoftyIDXListingHelper::getActiveListing($params);
        if (isset($listingResult['listings']) && !empty($listingResult['listings'])) {
            foreach ($listingResult['listings'] as &$item) {
                LoftyIDXHouseHelper::format_house_card($item, $mls_list);
            }
        }

        $data = wp_json_encode([
            'mls_list' => array_column($mls_list, 'id'),
            'layout' => $shortcode_config['layout'] ?? $this->configInfo['featuredListingStyle'],
            'layoutConfig' => $shortcode_config['layout_config'] ?? $this->configInfo[$this->configInfo['featuredListingStyle']],
            'featureListingName' => $this->featureListingName,
            'shortcode_config'=> $shortcode_config,
            'shortcode_class' => $shortcode_class,
        ]);

        return [
            'moduleId' => $this->moduleId,
            'list' => $listingResult['listings'],
            'layout' => $shortcode_config['layout'] ?? $this->configInfo['featuredListingStyle'],
            'shortcode_class' => $shortcode_class,
            'data' => $data
        ];
    }

    public function injectMetaTags()
    {
        echo wp_kses($this->metaTags, $this->allowedTags);
    }
}
