<?php
/**
 * View: admin/modules
 *
 * Injected variables:
 *   @var array $modules
 *
 * Available from bootstrap (via extract in View::display):
 *   $auth, $settings, $moduleRegistry, $currentUser
 */

$pageTitle = t('module.title');
include WEBROOT_PATH . '/includes/header.php';
?>
<div class="admin-layout">
<?php include WEBROOT_PATH . '/includes/admin_sidebar.php'; ?>
<div class="admin-content">

<div class="page-header">
    <h1><?= t('module.title') ?></h1>
    <?php
    // Bouton d'activation groupée : le module postgresql et toutes ses
    // dépendances (transitives). Masqué si postgresql est déjà actif —
    // ses dépendances le sont alors nécessairement (garde deps_dependents).
    $pgModule = $modules['postgresql'] ?? null;
    ?>
    <?php if ($pgModule && empty($pgModule['active'])): ?>
    <form method="post" action="/admin/modules.php"
          onsubmit="return confirm('<?= htmlspecialchars(t('module.enable_required_confirm', ['module' => $pgModule['label'] ?? 'postgresql'])) ?>')">
        <input type="hidden" name="csrf_token"  value="<?= htmlspecialchars(Session::get('csrf_token')) ?>">
        <input type="hidden" name="action"      value="enable_required">
        <input type="hidden" name="module_name" value="postgresql">
        <button type="submit" class="btn btn-primary"><?= t('module.enable_required', ['module' => $pgModule['label'] ?? 'postgresql']) ?></button>
    </form>
    <?php endif; ?>
</div>

<?php if (empty($modules)): ?>
<div class="card">
    <p><?= t('module.none_registered') ?></p>
</div>
<?php else: ?>

<div class="module-grid">
<?php foreach ($modules as $mod): ?>
<div class="card module-card <?= $mod['active'] ? 'module-active' : 'module-inactive' ?>">
    <div class="module-header">
        <span class="module-icon">
            <?php if (!empty($mod['admin_icon_img'])): ?>
            <img src="<?= htmlspecialchars($mod['admin_icon_img']) ?>" class="module-icon-img" alt="">
            <?php else: ?>
            <?= htmlspecialchars($mod['nav_icon'] ?? '📦') ?>
            <?php endif; ?>
        </span>
        <div>
            <h2><?= htmlspecialchars($mod['label']) ?></h2>
            <small>v<?= htmlspecialchars($mod['version'] ?? '?') ?></small>
        </div>
        <div class="module-badge">
            <?php if ($mod['active']): ?>
            <span class="badge badge-success"><?= t('module.active') ?></span>
            <?php else: ?>
            <span class="badge badge-danger"><?= t('module.inactive') ?></span>
            <?php endif; ?>
        </div>
    </div>

    <p class="module-desc"><?= htmlspecialchars($mod['description'] ?? '') ?></p>

    <dl class="module-meta">
        <?php if (!empty($mod['roles'])): ?>
        <dt><?= t('module.roles') ?></dt>
        <dd>
            <?php foreach ($mod['roles'] as $roleName => $roleLabel): ?>
            <span class="badge badge-info"><?= htmlspecialchars(t('role.' . $roleName)) ?></span>
            <?php endforeach; ?>
        </dd>
        <?php endif; ?>

        <?php if (!empty($mod['depends_on'])): ?>
        <dt><?= t('module.depends_on') ?></dt>
        <dd>
            <?php foreach ($mod['depends_on'] as $dep): ?>
            <span class="badge badge-secondary"><?= htmlspecialchars($dep) ?></span>
            <?php endforeach; ?>
        </dd>
        <?php endif; ?>
    </dl>

    <div class="form-actions" style="margin-top:1rem">
        <?php if ($mod['active']): ?>
        <form method="post" action="/admin/modules.php"
              onsubmit="return confirm('<?= htmlspecialchars(t('module.confirm_disable', ['label' => $mod['label']])) ?>')">
            <input type="hidden" name="csrf_token"   value="<?= htmlspecialchars(Session::get('csrf_token')) ?>">
            <input type="hidden" name="action"       value="disable">
            <input type="hidden" name="module_name"  value="<?= htmlspecialchars($mod['name']) ?>">
            <button type="submit" class="btn btn-danger"><?= t('module.disable') ?></button>
        </form>
        <?php else: ?>
        <form method="post" action="/admin/modules.php">
            <input type="hidden" name="csrf_token"   value="<?= htmlspecialchars(Session::get('csrf_token')) ?>">
            <input type="hidden" name="action"       value="enable">
            <input type="hidden" name="module_name"  value="<?= htmlspecialchars($mod['name']) ?>">
            <button type="submit" class="btn btn-primary"><?= t('module.enable') ?></button>
        </form>
        <form method="post" action="/admin/modules.php" style="display:inline"
              onsubmit="return confirm('<?= htmlspecialchars(t('module.confirm_delete', ['label' => $mod['label']])) ?>')">
            <input type="hidden" name="csrf_token"   value="<?= htmlspecialchars(Session::get('csrf_token')) ?>">
            <input type="hidden" name="action"       value="remove_module">
            <input type="hidden" name="module_name"  value="<?= htmlspecialchars($mod['name']) ?>">
            <button type="submit" class="btn btn-danger"><?= t('module.remove') ?></button>
        </form>
        <?php endif; ?>

        <?php if (!empty($mod['nav_url']) && $mod['active']): ?>
        <a href="<?= htmlspecialchars($mod['nav_url']) ?>" class="btn btn-secondary">
            <?= t('module.open') ?>
        </a>
        <?php endif; ?>
    </div>
</div>
<?php endforeach; ?>
</div>

<?php endif; ?>

<!-- ── Ajout / mise à jour d'un module via ZIP ────────────────────────── -->
<div class="card" style="margin-top:1.5rem">
    <h2 style="font-size:1rem;margin:0 0 .4rem"><?= t('module.add_title') ?></h2>
    <p style="font-size:.85rem;color:var(--color-text-muted);margin:0 0 .75rem"><?= t('module.add_hint') ?></p>
    <form method="post" action="/admin/modules.php" enctype="multipart/form-data">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars(Session::get('csrf_token')) ?>">
        <input type="hidden" name="action" value="add_module">
        <div class="form-group">
            <label for="module_zip"><?= t('module.zip_label') ?></label>
            <input type="file" id="module_zip" name="module_zip" accept=".zip" required>
        </div>
        <div class="form-actions">
            <button type="submit" class="btn btn-primary">📦 <?= t('module.add_apply') ?></button>
        </div>
    </form>
</div>

</div><!-- .admin-content -->
</div><!-- .admin-layout -->

<?php include WEBROOT_PATH . '/includes/footer.php'; ?>
