<?php
/**
 * modules/profile/invites.php — invitaciones (solo admin): crear / revocar.
 */
if (!defined('IN_APP')) { die('Acceso denegado'); }

if (empty($_user['is_admin'])) App::redirect('/profile/');

$auth = new Auth($mysqli, $_config);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['create_invite'])) {
        $auth->createInvite($_user['id_user'], $_POST['invite_email'] ?? null);
    } elseif (!empty($_POST['revoke_invite'])) {
        $auth->revokeInvite((int)$_POST['revoke_invite']);
    }
    App::redirect('/profile/invites/');
}

$invites  = $auth->listInvites();
$base_url = rtrim($_config['url'], '/');

$page_title = App::t('invite.section');
?>
<div class="px-4 py-6">
  <section class="rounded-lg border border-gray-200 bg-white dark:border-ink-700 dark:bg-ink-900">
    <div class="flex items-center justify-between border-b border-gray-200 px-4 py-3 dark:border-ink-700">
      <h2 class="font-medium"><?= App::e(App::t('invite.section')) ?></h2>
    </div>

    <div class="border-b border-gray-200 px-4 py-4 dark:border-ink-700">
      <p class="mb-3 text-sm text-gray-dark dark:text-gray-light"><?= App::e(App::t('invite.hint')) ?></p>
      <form method="post" action="/profile/invites/" class="flex flex-wrap items-end gap-2">
        <div class="min-w-[12rem] flex-1">
          <label class="mb-1 block text-xs text-gray-dark dark:text-gray-light"><?= App::e(App::t('invite.email_optional')) ?></label>
          <input type="email" name="invite_email" placeholder="opcional@email.com"
                 class="w-full rounded-lg border border-gray-200 bg-white px-3 py-2 text-sm outline-none focus:border-primary-600 dark:border-ink-700 dark:bg-ink-800">
        </div>
        <button name="create_invite" value="1"
                class="rounded-lg bg-primary-600 px-4 py-2 text-sm font-medium text-white hover:bg-primary-700">
          <?= App::e(App::t('invite.create')) ?>
        </button>
      </form>
    </div>

    <div>
      <?php if (!$invites): ?>
        <p class="px-4 py-4 text-sm text-gray-dark dark:text-gray-light"><?= App::e(App::t('invite.empty')) ?></p>
      <?php else: foreach ($invites as $inv):
        $link  = $base_url . '/register/' . $inv['token'] . '/';
        $badge = array(
            'pending' => 'bg-primary-600/10 text-primary-700 dark:text-primary-300',
            'used'    => 'bg-success/10 text-success',
            'expired' => 'bg-gray-200 text-gray-dark dark:bg-ink-800 dark:text-gray-light',
        )[$inv['status']]; ?>
        <div class="border-b border-gray-200 px-4 py-3 last:border-0 dark:border-ink-700">
          <div class="flex items-center justify-between gap-3">
            <div class="min-w-0">
              <span class="rounded-full px-2 py-0.5 text-xs <?= $badge ?>"><?= App::e(App::t('invite.status_' . $inv['status'])) ?></span>
              <span class="ml-2 text-sm text-gray-dark dark:text-gray-light"><?= App::e($inv['email'] ?: '—') ?></span>
            </div>
            <?php if ($inv['status'] === 'pending'): ?>
              <form method="post" action="/profile/invites/">
                <button name="revoke_invite" value="<?= (int)$inv['id_invitation'] ?>"
                        class="rounded-lg border border-danger/30 px-3 py-1.5 text-xs text-danger hover:bg-danger/10">
                  <?= App::e(App::t('invite.revoke')) ?>
                </button>
              </form>
            <?php endif; ?>
          </div>
          <?php if ($inv['status'] === 'pending'): ?>
            <div class="mt-2 flex items-center gap-2">
              <input type="text" readonly value="<?= App::e($link) ?>"
                     class="w-full truncate rounded-lg border border-gray-200 bg-gray-50 px-3 py-1.5 text-xs dark:border-ink-700 dark:bg-ink-800">
              <button type="button" data-copy="<?= App::e($link) ?>"
                      class="shrink-0 rounded-lg border border-gray-200 px-3 py-1.5 text-xs hover:bg-gray-50 dark:border-ink-700 dark:hover:bg-ink-800">
                <?= App::e(App::t('invite.copy')) ?>
              </button>
            </div>
          <?php endif; ?>
        </div>
      <?php endforeach; endif; ?>
    </div>
  </section>
</div>
