<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }

$settings      = get_option( 'mt_primelink_settings', array() );
$license_key   = isset( $settings['license_key'] ) ? $settings['license_key'] : '';
$license_state = isset( $settings['license_status'] ) ? $settings['license_status'] : '';

$wp_version    = get_bloginfo( 'version' );
$php_version   = PHP_VERSION;
$mysql_version = $GLOBALS['wpdb']->db_version();
$server        = isset( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) : '';

$upload_dir    = wp_upload_dir();
$upload_ok     = empty( $upload_dir['error'] );
$upload_path   = $upload_dir['basedir'] ?? '';

$memory_limit  = ini_get( 'memory_limit' );
$max_exec      = ini_get( 'max_execution_time' );
$max_upload    = ini_get( 'upload_max_filesize' );
$post_max      = ini_get( 'post_max_size' );

$libxml        = extension_loaded( 'libxml' );
$simplexml     = function_exists( 'simplexml_load_file' );
$dom           = class_exists( 'DOMDocument' );
$json          = function_exists( 'json_encode' );

$woo_active    = class_exists( 'WooCommerce' );
?>
<div class="wrap mt-primelink-wrap">
    <?php
    $header_actions     = '';
    $header_eyebrow     = __( 'Health Check', 'primelink' );
    $header_title       = __( 'System Status', 'primelink' );
    $header_description = __( 'Environment checks to keep PrimeLink running smoothly.', 'primelink' );

    include MT_PRIMELINK_PATH . 'templates/admin/partials/header.php';
    ?>

    <div class="mt_primelink-system-grid" style="margin-top:32px;">
        <section class="glass mt_primelink-system-card">
            <div class="mt_primelink-card-header">
                <div>
                    <div class="mt_primelink-summary-eyebrow"><?php esc_html_e( 'Environment', 'primelink' ); ?></div>
                    <h3 style="margin:4px 0 0;"><?php esc_html_e( 'Versions', 'primelink' ); ?></h3>
                </div>
            </div>
            <div class="mt_primelink-card-body">
                <div class="mt_primelink-summary-row">
                    <span class="mt_primelink-summary-label">WooCommerce</span>
                    <span class="mt_primelink-summary-value"><?php echo esc_html( $wp_version ); ?></span>
                </div>
                <div class="mt_primelink-summary-row">
                    <span class="mt_primelink-summary-label">PHP</span>
                    <span class="mt_primelink-summary-value"><?php echo esc_html( $php_version ); ?></span>
                </div>
                <div class="mt_primelink-summary-row">
                    <span class="mt_primelink-summary-label">MySQL</span>
                    <span class="mt_primelink-summary-value"><?php echo esc_html( $mysql_version ); ?></span>
                </div>
                <div class="mt_primelink-summary-row">
                    <span class="mt_primelink-summary-label"><?php esc_html_e( 'Server', 'primelink' ); ?></span>
                    <span class="mt_primelink-summary-value"><?php echo esc_html( $server ); ?></span>
                </div>
            </div>
        </section>

        <section class="glass mt_primelink-system-card">
            <div class="mt_primelink-card-header">
                <div>
                    <div class="mt_primelink-summary-eyebrow"><?php esc_html_e( 'Resources', 'primelink' ); ?></div>
                    <h3 style="margin:4px 0 0;"><?php esc_html_e( 'Limits', 'primelink' ); ?></h3>
                </div>
            </div>
            <div class="mt_primelink-card-body">
                <div class="mt_primelink-summary-row"><span class="mt_primelink-summary-label"><?php esc_html_e( 'Memory limit', 'primelink' ); ?></span><span class="mt_primelink-summary-value"><?php echo esc_html( $memory_limit ); ?></span></div>
                <div class="mt_primelink-summary-row"><span class="mt_primelink-summary-label"><?php esc_html_e( 'Max execution time', 'primelink' ); ?></span><span class="mt_primelink-summary-value"><?php echo esc_html( $max_exec ); ?>s</span></div>
                <div class="mt_primelink-summary-row"><span class="mt_primelink-summary-label"><?php esc_html_e( 'Upload max filesize', 'primelink' ); ?></span><span class="mt_primelink-summary-value"><?php echo esc_html( $max_upload ); ?></span></div>
                <div class="mt_primelink-summary-row"><span class="mt_primelink-summary-label"><?php esc_html_e( 'Post max size', 'primelink' ); ?></span><span class="mt_primelink-summary-value"><?php echo esc_html( $post_max ); ?></span></div>
            </div>
        </section>

        <section class="glass mt_primelink-system-card">
            <div class="mt_primelink-card-header">
                <div>
                    <div class="mt_primelink-summary-eyebrow"><?php esc_html_e( 'Paths', 'primelink' ); ?></div>
                    <h3 style="margin:4px 0 0;"><?php esc_html_e( 'Uploads & Storage', 'primelink' ); ?></h3>
                </div>
            </div>
            <div class="mt_primelink-card-body">
                <div class="mt_primelink-summary-row">
                    <span class="mt_primelink-summary-label"><?php esc_html_e( 'Uploads dir', 'primelink' ); ?></span>
                    <span class="mt_primelink-summary-value"><?php echo esc_html( $upload_path ); ?></span>
                </div>
                <div class="mt_primelink-summary-row">
                    <span class="mt_primelink-summary-label"><?php esc_html_e( 'Uploads writable', 'primelink' ); ?></span>
                    <span class="mt_primelink-summary-value">
                        <span class="mt_primelink-status-pill <?php echo $upload_ok ? 'mt_primelink-status-running' : 'mt_primelink-status-paused'; ?>">
                            <?php echo $upload_ok ? esc_html__( 'Yes', 'primelink' ) : esc_html__( 'No', 'primelink' ); ?>
                        </span>
                    </span>
                </div>
            </div>
        </section>

        <section class="glass mt_primelink-system-card">
            <div class="mt_primelink-card-header">
                <div>
                    <div class="mt_primelink-summary-eyebrow"><?php esc_html_e( 'Extensions', 'primelink' ); ?></div>
                    <h3 style="margin:4px 0 0;"><?php esc_html_e( 'Libraries', 'primelink' ); ?></h3>
                </div>
            </div>
            <div class="mt_primelink-card-body">
                <div class="mt_primelink-summary-row"><span class="mt_primelink-summary-label">libxml</span><span class="mt_primelink-summary-value"><span class="mt_primelink-status-pill <?php echo $libxml ? 'mt_primelink-status-running' : 'mt_primelink-status-paused'; ?>"><?php echo $libxml ? 'Enabled' : 'Missing'; ?></span></span></div>
                <div class="mt_primelink-summary-row"><span class="mt_primelink-summary-label">SimpleXML</span><span class="mt_primelink-summary-value"><span class="mt_primelink-status-pill <?php echo $simplexml ? 'mt_primelink-status-running' : 'mt_primelink-status-paused'; ?>"><?php echo $simplexml ? 'Enabled' : 'Missing'; ?></span></span></div>
                <div class="mt_primelink-summary-row"><span class="mt_primelink-summary-label">DOM</span><span class="mt_primelink-summary-value"><span class="mt_primelink-status-pill <?php echo $dom ? 'mt_primelink-status-running' : 'mt_primelink-status-paused'; ?>"><?php echo $dom ? 'Enabled' : 'Missing'; ?></span></span></div>
                <div class="mt_primelink-summary-row"><span class="mt_primelink-summary-label">JSON</span><span class="mt_primelink-summary-value"><span class="mt_primelink-status-pill <?php echo $json ? 'mt_primelink-status-running' : 'mt_primelink-status-paused'; ?>"><?php echo $json ? 'Enabled' : 'Missing'; ?></span></span></div>
            </div>
        </section>

        <section class="glass mt_primelink-system-card">
            <div class="mt_primelink-card-header">
                <div>
                    <div class="mt_primelink-summary-eyebrow"><?php esc_html_e( 'Integrations', 'primelink' ); ?></div>
                    <h3 style="margin:4px 0 0;"><?php esc_html_e( 'WooCommerce', 'primelink' ); ?></h3>
                </div>
            </div>
            <div class="mt_primelink-card-body">
                <div class="mt_primelink-summary-row">
                    <span class="mt_primelink-summary-label"><?php esc_html_e( 'WooCommerce active', 'primelink' ); ?></span>
                    <span class="mt_primelink-summary-value">
                        <span class="mt_primelink-status-pill <?php echo $woo_active ? 'mt_primelink-status-running' : 'mt_primelink-status-paused'; ?>">
                            <?php echo $woo_active ? esc_html__( 'Yes', 'primelink' ) : esc_html__( 'No', 'primelink' ); ?>
                        </span>
                    </span>
                </div>
            </div>
        </section>

        <section class="glass mt_primelink-system-card">
            <div class="mt_primelink-card-header">
                <div>
                    <div class="mt_primelink-summary-eyebrow"><?php esc_html_e( 'License', 'primelink' ); ?></div>
                    <h3 style="margin:4px 0 0;"><?php esc_html_e( 'Status', 'primelink' ); ?></h3>
                </div>
            </div>
            <div class="mt_primelink-card-body">
                <div class="mt_primelink-summary-row">
                    <span class="mt_primelink-summary-label"><?php esc_html_e( 'Key saved', 'primelink' ); ?></span>
                    <span class="mt_primelink-summary-value"><?php echo $license_key ? esc_html__( 'Yes', 'primelink' ) : esc_html__( 'No', 'primelink' ); ?></span>
                </div>
                <div class="mt_primelink-summary-row">
                    <span class="mt_primelink-summary-label"><?php esc_html_e( 'License status', 'primelink' ); ?></span>
                    <span class="mt_primelink-summary-value">
                        <span class="mt_primelink-status-pill <?php echo ( $license_state === 'saved' ) ? 'mt_primelink-status-running' : 'mt_primelink-status-paused'; ?>">
                            <?php echo $license_state ? esc_html( ucfirst( $license_state ) ) : esc_html__( 'Not saved', 'primelink' ); ?>
                        </span>
                    </span>
                </div>
            </div>
        </section>
    </div>
</div>
