<?php

/**
 * Developer Console — Schema Documentation Status Page
 *
 * Displays the status of all generated documentation files.
 * Shows existence, file size, and last modified date for each doc.
 * Action buttons (Generate, Compare, Download) are disabled — Available in DX002.
 *
 * Variables:
 *   $doc_status — array from DeveloperService::getDocumentationStatus()
 *
 * @package Inveetaire
 * @ref     DX001
 */

/** @var array $doc_status */
$doc_status = $doc_status ?? [];

$docVersion   = $doc_status['doc_version'] ?? 'v1.0.0';
$coreDocs     = $doc_status['core_docs']   ?? [];
$tablesDir    = $doc_status['tables_dir']  ?? [];
$existCount   = (int)($tablesDir['count_existing'] ?? 0);
$expectedCount = (int)($tablesDir['count_expected'] ?? 43);
$pct          = $expectedCount > 0 ? min(100, round(($existCount / $expectedCount) * 100)) : 0;

?>

<!-- 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>schema</span>
    </div>
    <h1 style="font-size: 1.5rem; font-weight: 800; color: var(--dev-text); margin-bottom: 0.25rem;">
        Schema Documentation
    </h1>
    <p style="font-size: 0.8125rem; color: var(--dev-text-muted);">
        Status of all generated database documentation files.
        Documentation version: <span style="color:var(--dev-rose); font-family:var(--mono);"><?= escape_html($docVersion) ?></span>
    </p>
</div>

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

    <div class="dev-card" style="padding: 1.25rem;">
        <div class="dev-stat-label">Doc Version</div>
        <div class="dev-stat-value" style="color: var(--dev-rose);"><?= escape_html($docVersion) ?></div>
        <div class="dev-stat-sub">DX001 Baseline</div>
    </div>

    <div class="dev-card" style="padding: 1.25rem;">
        <div class="dev-stat-label">Core Documents</div>
        <div class="dev-stat-value"><?= count($coreDocs) ?></div>
        <div class="dev-stat-sub">
            <?= array_sum(array_column($coreDocs, 'exists')) ?> of <?= count($coreDocs) ?> present
        </div>
    </div>

    <div class="dev-card" style="padding: 1.25rem;">
        <div class="dev-stat-label">Table Docs</div>
        <div class="dev-stat-value"><?= $existCount ?></div>
        <div class="dev-stat-sub">of <?= $expectedCount ?> expected</div>
    </div>

    <div class="dev-card" style="padding: 1.25rem;">
        <div class="dev-stat-label">Coverage</div>
        <div class="dev-stat-value" style="color: var(--dev-accent2);"><?= $pct ?>%</div>
        <div class="dev-stat-sub"><?= $existCount ?>/<?= $expectedCount ?> table files</div>
    </div>

</div>

<!-- Actions Bar — Disabled (DX002) -->
<div class="dev-card" style="margin-bottom: 2rem;">
    <div class="dev-card-header">
        <span class="dev-card-title">
            <i class="fa-solid fa-wand-magic-sparkles" style="color:#444c56; margin-right: 0.375rem;"></i>
            Documentation Actions
        </span>
        <span class="badge-soon">Available in DX002</span>
    </div>
    <div class="dev-card-body" style="display:flex; gap:0.75rem; flex-wrap:wrap;">
        <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-size:0.75rem; color:#444c56; cursor:not-allowed; font-family:var(--mono);">
            <i class="fa-solid fa-arrows-rotate"></i> Generate Documentation
        </button>
        <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-size:0.75rem; color:#444c56; cursor:not-allowed; font-family:var(--mono);">
            <i class="fa-solid fa-code-compare"></i> Compare Schema
        </button>
        <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-size:0.75rem; color:#444c56; cursor:not-allowed; font-family:var(--mono);">
            <i class="fa-solid fa-download"></i> Download Documentation
        </button>
        <div style="margin-left: auto; font-size:0.6875rem; color:var(--dev-text-muted); font-family:var(--mono); display:flex; align-items:center;">
            Auto-generation and comparison tools will be implemented in DX002.
        </div>
    </div>
</div>

<!-- Core Documentation Files -->
<div class="dev-section-title">Core Documentation Files</div>
<div class="dev-card" style="margin-bottom: 2rem;">
    <table class="dev-table">
        <thead>
            <tr>
                <th>File</th>
                <th>Location</th>
                <th>Status</th>
                <th>Size</th>
                <th>Last Modified</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($coreDocs as $filename => $info): ?>
            <tr>
                <td class="mono"><?= escape_html($filename) ?></td>
                <td class="mono muted" style="font-size:0.6875rem;"><?= escape_html($info['path']) ?></td>
                <td>
                    <?php if ($info['exists']): ?>
                        <span class="badge-ok">● Present</span>
                    <?php else: ?>
                        <span class="badge-err">✕ Missing</span>
                    <?php endif; ?>
                </td>
                <td class="mono muted"><?= escape_html($info['size_kb']) ?></td>
                <td class="muted" style="font-size:0.75rem;">
                    <?= $info['modified'] ? escape_html($info['modified']) : '—' ?>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

<!-- Table Documentation Status -->
<div class="dev-section-title">Table Documentation Coverage</div>
<div class="dev-card" style="margin-bottom: 2rem;">
    <div class="dev-card-header">
        <span class="dev-card-title">
            <i class="fa-solid fa-table" style="color: var(--dev-accent); margin-right: 0.375rem;"></i>
            docs/database/tables/
        </span>
        <span style="font-family:var(--mono); font-size:0.75rem; color: var(--dev-text-muted);">
            <?= $existCount ?>/<?= $expectedCount ?> files
        </span>
    </div>
    <div class="dev-card-body">
        <!-- Progress -->
        <div style="margin-bottom: 1.25rem;">
            <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom: 0.375rem;">
                <span style="font-family:var(--mono); font-size:0.6875rem; color:var(--dev-text-muted);">
                    Coverage: <?= $existCount ?>/<?= $expectedCount ?>
                </span>
                <span style="font-family:var(--mono); font-size:0.6875rem; color:var(--dev-accent2);">
                    <?= $pct ?>%
                </span>
            </div>
            <div class="dev-progress-track">
                <div class="dev-progress-fill" style="width: <?= $pct ?>%;"></div>
            </div>
        </div>

        <!-- File grid -->
        <?php if (!empty($tablesDir['files'])): ?>
        <div style="display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 0.375rem;">
            <?php
            $tableFiles = $tablesDir['files'];
            sort($tableFiles);
            foreach ($tableFiles as $tf):
            ?>
                <div style="display:flex; align-items:center; gap:0.375rem; padding:0.375rem 0.625rem; background:var(--dev-surface2); border:1px solid var(--dev-border2); border-radius:5px; font-family:var(--mono); font-size:0.625rem; color:var(--dev-text-muted);">
                    <span style="color:var(--dev-accent2); font-size:0.5rem;">●</span>
                    <?= escape_html($tf) ?>
                </div>
            <?php endforeach; ?>
        </div>
        <?php elseif (!$tablesDir['exists']): ?>
            <div style="padding: 1rem; text-align:center; color: var(--dev-danger); font-family:var(--mono); font-size:0.75rem;">
                ✕ Directory docs/database/tables/ not found
            </div>
        <?php else: ?>
            <div style="padding: 1rem; text-align:center; color: var(--dev-text-muted); font-family:var(--mono); font-size:0.75rem;">
                No table documentation files found.
            </div>
        <?php endif; ?>
    </div>
</div>

<!-- Documentation Rules Reminder -->
<div class="dev-section-title">Documentation Update Rules</div>
<div class="dev-card">
    <div class="dev-card-body">
        <p style="font-size:0.8125rem; color:var(--dev-text-muted); margin-bottom:1rem; line-height:1.6;">
            Every database schema modification <strong style="color:var(--dev-warn);">MUST</strong> update the following files:
        </p>
        <div style="display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 0.5rem;">
            <?php
            $rules = [
                ['file' => 'README_SCHEMA.md',     'when' => 'Table count / modules / conventions change'],
                ['file' => 'RELATIONSHIP.md',       'when' => 'Entity relationships change'],
                ['file' => 'tables/{table}.md',     'when' => 'Affected table(s) modified'],
                ['file' => 'CHANGELOG.md',          'when' => 'Always — with version bump'],
                ['file' => 'AI_CONTEXT.md',         'when' => 'Business logic changes'],
                ['file' => 'schema.sql (docs)',      'when' => 'Keep in sync with database/schema.sql'],
            ];
            foreach ($rules as $rule):
            ?>
                <div style="padding:0.625rem; background:var(--dev-surface2); border:1px solid var(--dev-border2); border-radius:6px;">
                    <div style="font-family:var(--mono); font-size:0.6875rem; color:var(--dev-accent); margin-bottom:0.25rem;">
                        <?= escape_html($rule['file']) ?>
                    </div>
                    <div style="font-size:0.6875rem; color:var(--dev-text-muted);">
                        <?= escape_html($rule['when']) ?>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>
