<?php

/**
 * Developer Console — Side-by-Side MySQL vs Supabase Inspector (DX002A)
 *
 * Displays:
 *   - Left panel: Full MySQL stats (MySqlInspectorService)
 *   - Right panel: Supabase info (SupabaseService)
 *   - Table list: All MySQL tables with row/size info
 *   - Comparison summary: MySQL only / Both / Supabase only
 *   - All sync actions disabled (DX002B)
 *
 * Variables:
 *   $mysql_data    — array from MySqlInspectorService::inspect()
 *   $supabase_info — array from SupabaseService::getProjectInfo()
 *   $comparison    — array from SchemaReaderService::compareTableLists()
 *   $mysql_tables  — list<array> from SchemaReaderService::getMysqlTables()
 *
 * @package Inveetaire
 * @ref     DX002A
 */

/** @var array $mysql_data */
/** @var array $supabase_info */
/** @var array $comparison */
/** @var array $mysql_tables */
$mysql_data    = $mysql_data    ?? [];
$supabase_info = $supabase_info ?? [];
$comparison    = $comparison    ?? [];
$mysql_tables  = $mysql_tables  ?? [];
$sbConfigured  = (bool)($supabase_info['configured'] ?? false);

?>

<!-- Page Header -->
<div style="margin-bottom: 2rem;">
    <div style="font-family: var(--mono); font-size: 0.6875rem; color: var(--dev-text-muted); margin-bottom: 0.5rem;">
        <a href="<?= url('/app/developer') ?>" style="color:var(--dev-accent); text-decoration:none;">developer</a>
        <span style="color:#444c56;"> / </span>
        <span>inspector</span>
    </div>
    <h1 style="font-size: 1.5rem; font-weight: 800; color: var(--dev-text); margin-bottom: 0.25rem;">
        Database Inspector
    </h1>
    <p style="font-size: 0.8125rem; color: var(--dev-text-muted);">
        Side-by-side MySQL and Supabase schema comparison.
        Column-level diff and sync actions available in <span style="color:var(--dev-purple);">DX002B</span>.
    </p>
</div>

<!-- Comparison Summary Cards -->
<div style="display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 1rem; margin-bottom: 2rem;">

    <div class="dev-card" style="padding: 1.25rem; border-color: rgba(88,166,255,0.25);">
        <div class="dev-stat-label" style="color: var(--dev-accent);">MySQL Tables</div>
        <div class="dev-stat-value" style="color: var(--dev-accent);"><?= (int)($comparison['mysql_total'] ?? 0) ?></div>
        <div class="dev-stat-sub">source of truth</div>
    </div>

    <div class="dev-card" style="padding: 1.25rem; border-color: rgba(62,207,142,0.25);">
        <div class="dev-stat-label" style="color: #3ecf8e;">Supabase Tables</div>
        <div class="dev-stat-value" style="color: #3ecf8e;"><?= (int)($comparison['supabase_total'] ?? 0) ?></div>
        <div class="dev-stat-sub"><?= $sbConfigured ? 'mirror (empty)' : 'not configured' ?></div>
    </div>

    <div class="dev-card" style="padding: 1.25rem;">
        <div class="dev-stat-label">MySQL Only</div>
        <div class="dev-stat-value" style="color: var(--dev-warn);"><?= count($comparison['mysql_only'] ?? []) ?></div>
        <div class="dev-stat-sub">not mirrored yet</div>
    </div>

    <div class="dev-card" style="padding: 1.25rem;">
        <div class="dev-stat-label">In Both</div>
        <div class="dev-stat-value" style="color: var(--dev-accent2);"><?= count($comparison['in_both'] ?? []) ?></div>
        <div class="dev-stat-sub">synced tables</div>
    </div>

    <div class="dev-card" style="padding: 1.25rem;">
        <div class="dev-stat-label">Supabase Only</div>
        <div class="dev-stat-value" style="color: var(--dev-danger);"><?= count($comparison['supabase_only'] ?? []) ?></div>
        <div class="dev-stat-sub">not in MySQL</div>
    </div>

</div>

<!-- Side-by-Side Database Panels -->
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem; margin-bottom: 2rem;">

    <!-- LEFT: MySQL -->
    <div class="dev-card" style="border-color: rgba(88,166,255,0.25);">
        <div class="dev-card-header" style="border-bottom-color: rgba(88,166,255,0.15);">
            <span class="dev-card-title">
                <i class="fa-solid fa-database" style="color:var(--dev-accent); margin-right:0.375rem;"></i>
                MySQL
            </span>
            <span class="badge-ok">● Source of Truth</span>
        </div>
        <div class="dev-card-body">
            <div class="dev-info-row">
                <span class="label">Database Name</span>
                <span class="value"><?= escape_html($mysql_data['db_name'] ?? '—') ?></span>
            </div>
            <div class="dev-info-row">
                <span class="label">Version</span>
                <span class="value"><?= escape_html($mysql_data['db_version_short'] ?? '—') ?></span>
            </div>
            <div class="dev-info-row">
                <span class="label">Character Set</span>
                <span class="value"><?= escape_html($mysql_data['character_set'] ?? '—') ?></span>
            </div>
            <div class="dev-info-row">
                <span class="label">Collation</span>
                <span class="value" style="font-size:0.6875rem;"><?= escape_html($mysql_data['collation'] ?? '—') ?></span>
            </div>
            <div class="dev-info-row">
                <span class="label">Engine</span>
                <span class="value"><?= escape_html($mysql_data['default_engine'] ?? 'InnoDB') ?></span>
            </div>
            <div class="dev-info-row">
                <span class="label">Total Tables</span>
                <span class="value" style="color:var(--dev-accent); font-weight:700;"><?= (int)($mysql_data['total_tables'] ?? 0) ?></span>
            </div>
            <div class="dev-info-row">
                <span class="label">Total Rows (est.)</span>
                <span class="value"><?= number_format((int)($mysql_data['total_rows'] ?? 0)) ?></span>
            </div>
            <div class="dev-info-row">
                <span class="label">DB Size</span>
                <span class="value"><?= escape_html($mysql_data['db_size_mb'] ?? '0.00') ?> MB</span>
            </div>
        </div>
    </div>

    <!-- RIGHT: Supabase -->
    <div class="dev-card" style="border-color: rgba(62,207,142,0.25); <?= !$sbConfigured ? 'opacity:0.6;' : '' ?>">
        <div class="dev-card-header" style="border-bottom-color: rgba(62,207,142,0.15);">
            <span class="dev-card-title">
                <i class="fa-solid fa-cloud" style="color:#3ecf8e; margin-right:0.375rem;"></i>
                Supabase PostgreSQL
            </span>
            <?php if ($sbConfigured): ?>
                <span class="badge-warn">● Not Synced</span>
            <?php else: ?>
                <span class="badge-warn">Not Configured</span>
            <?php endif; ?>
        </div>
        <div class="dev-card-body">
            <?php if ($sbConfigured): ?>
            <div class="dev-info-row">
                <span class="label">Project URL</span>
                <span class="value" style="font-size:0.6875rem;"><?= escape_html($supabase_info['url_masked'] ?? '—') ?></span>
            </div>
            <div class="dev-info-row">
                <span class="label">Project Ref</span>
                <span class="value"><?= escape_html($supabase_info['project_ref_masked'] ?? '—') ?></span>
            </div>
            <div class="dev-info-row">
                <span class="label">Region</span>
                <span class="value"><?= escape_html($supabase_info['region'] ?? '—') ?></span>
            </div>
            <div class="dev-info-row">
                <span class="label">PostgreSQL Version</span>
                <span class="value">— (run connection test)</span>
            </div>
            <div class="dev-info-row">
                <span class="label">Total Tables</span>
                <span class="value" style="color:#3ecf8e; font-weight:700;"><?= (int)($comparison['supabase_total'] ?? 0) ?></span>
            </div>
            <div class="dev-info-row">
                <span class="label">Mirror Status</span>
                <span class="value" style="color:var(--dev-warn);"><?= escape_html($supabase_info['mirror_status'] ?? 'Not Synced') ?></span>
            </div>
            <div class="dev-info-row">
                <span class="label">Sync Status</span>
                <span class="value" style="color:var(--dev-purple); font-size:0.75rem;"><?= escape_html($supabase_info['sync_status'] ?? 'Coming in DX002B') ?></span>
            </div>
            <?php else: ?>
            <div style="padding:1rem; text-align:center;">
                <div style="font-size:2rem; color:#444c56; margin-bottom:0.75rem;"><i class="fa-solid fa-cloud-slash"></i></div>
                <p style="font-size:0.8125rem; color:var(--dev-text-muted); margin-bottom:0.5rem;">Supabase not configured</p>
                <a href="<?= url('/app/developer/supabase') ?>" style="font-size:0.75rem; color:#3ecf8e; text-decoration:none; font-family:var(--mono);">
                    Configure Supabase →
                </a>
            </div>
            <?php endif; ?>
        </div>
    </div>

</div>

<!-- DX002B Disabled Actions -->
<div class="dev-card" style="margin-bottom: 2rem;">
    <div class="dev-card-header">
        <span class="dev-card-title">
            <i class="fa-solid fa-arrows-rotate" style="color:#444c56; margin-right:0.375rem;"></i>
            Schema Comparison Actions
        </span>
        <span class="badge-soon">Available in DX002B</span>
    </div>
    <div class="dev-card-body" style="display:flex; flex-wrap:wrap; gap:0.75rem; align-items:center;">
        <?php foreach ([
            ['fa-code-compare',     'Compare Schema'],
            ['fa-cloud-arrow-up',   'Push to Supabase'],
            ['fa-cloud-arrow-down', 'Pull from Supabase'],
            ['fa-shield-check',     'Verify Mirror'],
            ['fa-file-code',        'Generate Schema'],
        ] as [$icon, $label]): ?>
            <div style="display:flex; flex-direction:column; align-items:center; gap:0.375rem;">
                <button disabled style="display:inline-flex; align-items:center; gap:0.5rem; padding:0.5rem 1rem; background:var(--dev-surface2); border:1px solid var(--dev-border); border-radius:6px; font-family:var(--mono); font-size:0.75rem; color:#444c56; cursor:not-allowed;">
                    <i class="fa-solid <?= $icon ?>"></i> <?= escape_html($label) ?>
                </button>
                <span style="font-family:var(--mono); font-size:0.5rem; color:var(--dev-purple); letter-spacing:0.05em;">Available in DX002B</span>
            </div>
        <?php endforeach; ?>
    </div>
</div>

<!-- MySQL Tables List -->
<div class="dev-section-title" style="margin-bottom: 1rem;">
    MySQL Table Inventory — <?= count($mysql_tables) ?> tables
</div>
<div class="dev-card">
    <div class="dev-card-body" style="padding:0;">
        <div style="overflow-x:auto;">
            <table class="dev-table">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Table Name</th>
                        <th>Engine</th>
                        <th>Rows (est.)</th>
                        <th>Data (KB)</th>
                        <th>Index (KB)</th>
                        <th>In Supabase</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($mysql_tables as $i => $tbl): ?>
                    <tr>
                        <td class="muted" style="font-size:0.6875rem;"><?= $i + 1 ?></td>
                        <td class="mono"><?= escape_html($tbl['table_name'] ?? '—') ?></td>
                        <td class="mono muted" style="font-size:0.6875rem;"><?= escape_html($tbl['engine'] ?? '—') ?></td>
                        <td><?= number_format((int)($tbl['table_rows'] ?? 0)) ?></td>
                        <td class="muted"><?= number_format((float)($tbl['data_length_kb'] ?? 0), 1) ?></td>
                        <td class="muted"><?= number_format((float)($tbl['index_length_kb'] ?? 0), 1) ?></td>
                        <td>
                            <?php
                            $inBoth = $comparison['in_both'] ?? [];
                            if (in_array($tbl['table_name'] ?? '', $inBoth, true)):
                            ?>
                                <span class="badge-ok" style="font-size:0.5rem;">● Yes</span>
                            <?php else: ?>
                                <span style="font-family:var(--mono); font-size:0.5625rem; color:#444c56;">—</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php if (empty($mysql_tables)): ?>
                    <tr><td colspan="7" style="text-align:center; color:var(--dev-text-muted); padding:1.5rem;">No tables found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
