<?php

if (!defined('ABSPATH')) { exit; }
require_once __DIR__ . '/class-aieo-dmm-insights-base.php';

class AIEO_DMM_Insights_Report extends AIEO_DMM_Insights_Base {

    private $id;
    private $label;
    private $section;
    private $table_id;
    private $priority;

    public function __construct($id, $label, $section, $table_id, $priority = 100) {
        $this->id       = (string) $id;
        $this->label    = (string) $label;
        $this->section  = (string) $section;
        $this->table_id = (int) $table_id;
        $this->priority = (int) $priority;
    }

    public function get_id()       { return $this->id; }
    public function get_label()    { return $this->label !== '' ? $this->label : ('#' . $this->table_id); }
    public function get_section()  { return $this->section; }
    public function get_priority() { return $this->priority; }

    public function render_subtab() {
        echo '<div class="aieo-dmm-section">';
        echo '<h2>' . esc_html($this->get_label()) . '</h2>';
        echo '<div class="aieo-dmm-card">';
        $this->render_table($this->table_id);
        echo '</div>';
        echo '</div>';
    }

    
    public static function register_all($dmm) {
        if (!is_object($dmm) || !method_exists($dmm, 'register_module')) { return; }
        $map     = self::get_map();
        $seen    = array();
        $prio    = 0;
        foreach (self::section_keys() as $sk) {
            $rows = isset($map[$sk]) ? $map[$sk] : array();
            foreach ($rows as $r) {
                $tid = (int) $r['id'];
                if ($tid <= 0) { continue; }
                $page_id = 'insights_t' . $tid;
                if (isset($seen[$page_id])) { continue; }
                $seen[$page_id] = true;
                
                $label = self::label_for($tid, $r['label']);
                $prio += 10;
                $dmm->register_module(new self($page_id, $label, $sk, $tid, $prio));
            }
        }
    }
}

add_action('aieo_dmm_register_dynamic_modules', array('AIEO_DMM_Insights_Report', 'register_all'));
