<?php
require_once __DIR__ . '/../../app/core/bootstrap.php';
require_once __DIR__ . '/../../app/core/project_permissions.php';
require_once __DIR__ . '/../../app/core/access_guards.php';
require_once __DIR__ . '/helpers.php';
auth_require_login();
$pdo = crm_pdo();
gp_require_staff_access($pdo);

$page_title = 'Inbox settings';
$errors = [];
$ok = '';

$keys = [
    'imap_host' => '',
    'imap_port' => '993',
    'imap_encryption' => 'ssl',
    'imap_username' => 'projects@gopools.gr',
    'imap_password' => '',
    'imap_mailbox' => 'INBOX',
    'imap_fetch_limit' => '20',
];

foreach ($keys as $k => $v) {
    $keys[$k] = inbox_setting($pdo, $k, $v);
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    gp_require_csrf();
    foreach (array_keys($keys) as $k) {
        $keys[$k] = trim((string)($_POST[$k] ?? ''));
    }

    foreach ($keys as $k => $v) {
        $stmt = $pdo->prepare("
            INSERT INTO settings (setting_key, setting_value)
            VALUES (:k, :v)
            ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)
        ");
        $stmt->execute([':k' => $k, ':v' => $v]);
    }
    $ok = 'Settings saved.';
}

include __DIR__ . '/../includes/header.php';
?>
<div class="page-header">
  <div class="page-header-title">
    <h1>Inbox settings</h1>
    <span>IMAP mailbox configuration for projects@gopools.gr</span>
  </div>
  <div class="page-header-actions">
    <a class="btn btn-secondary btn-sm" href="/gopools/public/inbox/index.php">Back to inbox</a>
  </div>
</div>

<div class="card">
  <?php if ($ok): ?><div class="alert-danger" style="background:rgba(22,163,74,.15);color:#bbf7d0;border-color:rgba(22,163,74,.35)"><?php echo h($ok); ?></div><?php endif; ?>
  <?php if ($errors): ?><div class="alert-danger"><?php echo h(implode(' ', $errors)); ?></div><?php endif; ?>
  <form method="post">
      <?php echo csrf_field(); ?>
    <div class="form-grid">
      <div class="form-group"><label>IMAP host</label><input name="imap_host" value="<?php echo h($keys['imap_host']); ?>" placeholder="mail.gopools.gr"></div>
      <div class="form-group"><label>IMAP port</label><input name="imap_port" value="<?php echo h($keys['imap_port']); ?>"></div>
      <div class="form-group"><label>Encryption</label><input name="imap_encryption" value="<?php echo h($keys['imap_encryption']); ?>" placeholder="ssl"></div>
      <div class="form-group"><label>Username</label><input name="imap_username" value="<?php echo h($keys['imap_username']); ?>"></div>
      <div class="form-group"><label>Password</label><input type="password" name="imap_password" value="<?php echo h($keys['imap_password']); ?>"></div>
      <div class="form-group"><label>Mailbox</label><input name="imap_mailbox" value="<?php echo h($keys['imap_mailbox']); ?>"></div>
      <div class="form-group"><label>Fetch limit</label><input name="imap_fetch_limit" value="<?php echo h($keys['imap_fetch_limit']); ?>"></div>
    </div>
    <div class="form-actions">
      <button class="btn btn-primary" type="submit">Save settings</button>
      <a class="btn btn-secondary" href="/gopools/public/inbox/fetch.php">Fetch now</a>
    </div>
  </form>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
