<?php
/**
 * GT BOGO Engine — Store Status & Go Live
 * Dedicated page for setup checklist, Go Live button, and system health.
 *
 * @package GT_BOGO_Engine
 * @since   3.12.0
 */
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! current_user_can( 'manage_options' ) ) return;

$version = defined( 'GT_BOGO_VERSION' ) ? GT_BOGO_VERSION : GT_BOGO_VERSION;
$is_pro  = function_exists( 'gt_bogo_has_pro' ) ? gt_bogo_has_pro() : false;

require_once __DIR__ . '/../ui-system/wrapper-start.php';
?>

<div class="gt-page-header gt-animate-in">
  <div>
    <h1 class="gt-page-title">Store Status</h1>
    <p class="gt-page-subtitle">Setup checklist, Go Live, and system health for GT BOGO Engine PRO v<?php echo esc_html( $version ); ?></p>
  </div>
</div>

<?php
// ── Go Live Panel ─────────────────────────────────────────────────────────────
if ( class_exists( 'GT_BOGO_Go_Live' ) ) {
    $go_live = GT_BOGO_Go_Live::instance();
    $go_live->render_status_page();
}
?>

<?php
// ── System Health ─────────────────────────────────────────────────────────────
global $wpdb;

$tables = [
    'gtbogo_rules', 'gtbogo_analytics', 'gtbogo_owned_packs',
    'gtbogo_licenses', 'gtbogo_customers', 'gtbogo_bundles',
];
$table_status = [];
foreach ( $tables as $t ) {
    $exists = (bool) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . $t ) );
    $count  = $exists ? (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}{$t}" ) : 0;
    $table_status[] = [ 'name' => $wpdb->prefix . $t, 'exists' => $exists, 'rows' => $count ];
}

$php_ver   = phpversion();
$wp_ver    = get_bloginfo( 'version' );
$wc_ver    = defined( 'WC_VERSION' ) ? WC_VERSION : 'Not detected';
$mem_limit = ini_get( 'memory_limit' );
$max_exec  = ini_get( 'max_execution_time' );
$ssl       = is_ssl() || strpos( home_url(), 'https://' ) === 0;
?>

<div class="gt-card gt-animate-in" style="margin-top:20px">
  <div class="gt-card-header">
    <h2 class="gt-card-title">System Health</h2>
    <a href="<?php echo esc_url( admin_url( 'admin.php?page=gt-bogo-engine&tab=system-status' ) ); ?>" class="gt-btn gt-btn-ghost gt-btn-sm">Full System Status →</a>
  </div>
  <div style="display:grid;grid-template-columns:1fr 1fr;gap:20px">

    <!-- Environment -->
    <div>
      <p style="margin:0 0 12px;font-size:11px;font-weight:800;letter-spacing:.1em;text-transform:uppercase;color:var(--gt-subtle)">Environment</p>
      <?php foreach ( [
        [ 'PHP Version',        $php_ver,   version_compare( $php_ver, '8.0', '>=' ) ],
        [ 'WordPress',          $wp_ver,    version_compare( $wp_ver,  '6.0', '>=' ) ],
        [ 'WooCommerce',        $wc_ver,    $wc_ver !== 'Not detected' ],
        [ 'HTTPS',              $ssl ? 'Enabled' : 'Not enabled', $ssl ],
        [ 'Memory Limit',       $mem_limit, true ],
        [ 'Max Execution',      $max_exec . 's', (int) $max_exec >= 60 || $max_exec == 0 ],
      ] as $row ): ?>
      <div style="display:flex;justify-content:space-between;align-items:center;padding:8px 0;border-bottom:1px solid rgba(255,255,255,0.04)">
        <span style="font-size:13px;color:var(--gt-soft)"><?php echo esc_html( $row[0] ); ?></span>
        <span style="font-size:13px;font-weight:600;color:<?php echo $row[2] ? '#86efac' : '#fca5a5'; ?>"><?php echo esc_html( $row[1] ); ?></span>
      </div>
      <?php endforeach; ?>
    </div>

    <!-- Database tables -->
    <div>
      <p style="margin:0 0 12px;font-size:11px;font-weight:800;letter-spacing:.1em;text-transform:uppercase;color:var(--gt-subtle)">Database Tables</p>
      <?php foreach ( $table_status as $t ): ?>
      <div style="display:flex;justify-content:space-between;align-items:center;padding:8px 0;border-bottom:1px solid rgba(255,255,255,0.04)">
        <span style="font-size:12px;color:var(--gt-soft);font-family:'Courier New',monospace"><?php echo esc_html( $t['name'] ); ?></span>
        <span style="font-size:12px;font-weight:600;color:<?php echo $t['exists'] ? '#86efac' : '#fca5a5'; ?>">
          <?php echo $t['exists'] ? number_format( $t['rows'] ) . ' rows' : 'Missing'; ?>
        </span>
      </div>
      <?php endforeach; ?>
    </div>

  </div>
</div>

<?php require_once __DIR__ . '/../ui-system/wrapper-end.php'; ?>
