<?php
defined('ABSPATH') or die('Direct access not allowed');

/**
 * VollstartRD_Support — Support info collector.
 *
 * SRP: Collects system information for support context.
 * No UI rendering here — just data collection.
 * Pattern: Same as AppointmentDesk_Support for cross-plugin consistency.
 */
class VollstartRD_Support {

    private $MAIN;

    public function __construct($MAIN) {
        $this->MAIN = $MAIN;
    }

    /**
     * Collect all system info for support context.
     * Structure matches Appointment Desk for consistent UX.
     */
    public function getInfo(): array {
        global $wp_version;

        $db = $this->MAIN->getDB();

        $info = [
            'versions' => [
                'php'    => phpversion(),
                'wp'     => $wp_version,
                'mysql'  => $db->getServerVersion() ?: 'unknown',
                'plugin' => $this->MAIN->getPluginVersion(),
                'db'     => $db->getDBVersion(),
                'pro'    => defined('VOLLSTART_RD_PRO_VERSION') ? VOLLSTART_RD_PRO_VERSION : '',
            ],
            'first_activated_at' => (string) get_option('vollstart_rd_first_activated_at', ''),
            'site' => [
                'domain'       => wp_parse_url(site_url(), PHP_URL_HOST),
                'home'         => home_url(),
                'site_url'     => site_url(),
                'is_multisite' => is_multisite(),
            ],
            'pro_installed' => defined('VOLLSTART_RD_PRO_VERSION'),
            // Staff diagnostics are owned by the add-on; it can append them via
            // the vollstart_rd_support_info filter below if installed.
            'stats' => [
                'repairs'      => $db->count('repairs'),
                'customers'    => $db->count('customers'),
                'repair_types' => $db->count('repair_types'),
                'error_logs'   => $db->count('error_logs'),
            ],
            'libraries' => [
                'datatables'            => '2.3.7',
                'datatables_responsive' => '3.0.6',
                'qrcodejs'              => '1.0.0',
                'html5_qrcode'          => '2.3.8',
                'tcpdf'                 => $this->getTcpdfVersion(),
            ],
            'urls' => [
                'status_page'    => home_url($this->MAIN->getFrontend()->getSlug() . '/'),
                'slug'           => $this->MAIN->getFrontend()->getSlug(),
            ],
            'timezone'    => wp_timezone_string(),
            // function_exists() avoids spawning WC's autoloader (which would
            // trigger WP 6.7's translation-loaded-too-early notice).
            'woocommerce' => function_exists('WC'),
            'debug'       => defined('WP_DEBUG') && WP_DEBUG,
        ];

        // A registered add-on can append its own diagnostics via this filter.
        return apply_filters('vollstart_rd_support_info', $info);
    }

    /**
     * TCPDF version string for the support panel.
     *
     * Reads the bundled version from `vendors/TCPDF/VERSION`. If a different
     * TCPDF copy is already in memory (sister plugin loaded its own first),
     * the loaded version is appended so the conflict is visible.
     */
    private function getTcpdfVersion(): string {
        $bundled = trim((string) @file_get_contents(VOLLSTART_RD_PLUGIN_DIR_PATH . 'vendors/TCPDF/VERSION'));
        $loaded  = class_exists('TCPDF_STATIC') ? (string) TCPDF_STATIC::getTCPDFVersion() : '';
        if ($loaded !== '' && $loaded !== $bundled) {
            return $bundled . ' (loaded: ' . $loaded . ')';
        }
        return $bundled;
    }
}
