<?php

namespace Macymed\MacymedLinkTracker\Utils;

use Dompdf\Dompdf;

if (! defined('ABSPATH')) {
    exit; // Sécurité
}

class Report
{
    private static $unique_id;
    public static function init()
    {
        self::$unique_id = uniqid('graph_', true);


    }


    public static function renderDetailsUrl($id, $url, $exportButton = false)
    {
        // Récupérer toutes les lignes associées à cette URL ID
        $clicks = Stats::get_instance()->getClickFromUrl($id);

        $dataNetwork = Stats::get_instance()->buildNetworkData($id);

        $top_referers  = Stats::get_instance()->getTopReferer($id);
        $flop_referers = Stats::get_instance()->getFlopReferer($id);

        $title = 'Détails pour l\'URL : ' . $url;
        // HTML de la modale
        ob_start();
        ?>

        <p>Nombre de clics : <?php echo esc_html(count($clicks)); ?></p>
        <?php
                // @ini_set( 'display_errors', 1 );
                do_action('macymed_link_tracker_details_url_graph_evolution', $id, $url);
        ?>
        <?php
        do_action('macymed_link_tracker_details_url_graph_network', $id, $url);

        ?>
        <?php
        if (count($top_referers) > 0) {
            ?>
            <h3>Top 5 Referers</h3>
            <ul>
                <?php foreach ($top_referers as $referer => $count) : ?>
                    <li><?php echo esc_html($referer); ?> (<?php echo esc_html($count); ?> clics)</li>
                <?php endforeach; ?>
            </ul>
        <?php
        }
        ?>
        <?php
        if (count($flop_referers) > 0) {
            ?>
            <h3>Flop 5 Referers</h3>
            <ul>
                <?php foreach ($flop_referers as $referer => $count) : ?>
                    <li><?php echo esc_html($referer); ?> (<?php echo esc_html($count); ?> clics)</li>
                <?php endforeach; ?>
            </ul>
        <?php
        }
        ?>


        <?php

        ?>
            <input type="hidden" name="url_id" value="<?php echo esc_html($id); ?>">
            <input type="hidden" id="search-text" name="search-text" value="<?php echo esc_html($url); ?>">

<?php
do_action('macymed_link_tracker_generate_report_url', $id);


        $html = ob_get_clean();
        return [
            'html' => $html,
            'title' => $title,

        ];
    }

}
