<?php

namespace Dits\CaW\Modules;

// Exit if accessed directly
defined('WPINC') || die;

use Dits\CaW\Utils\HooksInterface;
use Dits\CaW\View;

/**
 * Class CompareModal
 *
 * Responsible for rendering the comparison modal window.
 * Works through the View class (Twig wrapper) and injects HTML into the footer via wp_footer hooks.
 */
class CompareModal implements HooksInterface
{
    protected View $view;
    protected string $trigger_selector;
    protected string $template;

    /**
     * @param View   $view             Twig View wrapper
     * @param string $trigger_selector CSS selectors (comma‑separated) that open the modal
     * @param array  $settings         Additional configuration options
     */
    public function __construct(View $view, $trigger_selector = '.open-compare', $settings = [])
    {
        $this->view = $view;
        $this->trigger_selector = $trigger_selector;
        $this->template = 'compare-table-popup.twig';
        $this->settings = $settings;

        // Automatic hook registration
        $this->hooks();
    }

    /**
     * Registers WordPress hooks.
     */
    public function hooks(): void
    {
        add_action('wp_footer', [$this, 'render'], 10);
    }

    /**
     * Renders the modal HTML via Twig.
     */
    public function render(): void
    {
        // You can replace the shortcode string below with dynamic logic or a variable if needed
        $data = [
            'title' => 'Product Comparison',
            'compare_table' => do_shortcode('[dcaw_compare_table_without_cat]'),
            'trigger_selector' => $this->trigger_selector,
        ];


        $this->view->render($this->template, $data);
    }
}
