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

function gp_setting(PDO $pdo, string $key, string $default = ''): string {
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = :k LIMIT 1");
    $stmt->execute([':k' => $key]);
    $v = $stmt->fetchColumn();
    return $v !== false ? (string)$v : $default;
}

$page_title = 'Email to project settings';
$ok = '';

$fields = [
    'email_to_project_enabled' => gp_setting($pdo, 'email_to_project_enabled', '1'),
    'email_auto_create_project' => gp_setting($pdo, 'email_auto_create_project', '0'),
    'email_default_project_status' => gp_setting($pdo, 'email_default_project_status', 'planned'),
    'email_default_category_name' => gp_setting($pdo, 'email_default_category_name', 'New pool'),
    'imap_host' => gp_setting($pdo, 'imap_host', ''),
    'imap_port' => gp_setting($pdo, 'imap_port', '993'),
    'imap_encryption' => gp_setting($pdo, 'imap_encryption', 'ssl'),
    'imap_username' => gp_setting($pdo, 'imap_username', 'projects@gopools.gr'),
    'imap_password' => gp_setting($pdo, 'imap_password', ''),
    'imap_mailbox' => gp_setting($pdo, 'imap_mailbox', 'INBOX'),
    'imap_fetch_limit' => gp_setting($pdo, 'imap_fetch_limit', '20'),
];

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

    foreach ($fields 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>Email to project settings</h1>
    <span>Mailbox + default automation behavior for projects@gopools.gr</span>
  </div>
  <div class="page-header-actions">
    <a class="btn btn-secondary btn-sm" href="/gopools/public/inbox/index.php">Open 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; ?>

  <form method="post">
      <?php echo csrf_field(); ?>
    <div class="form-grid">
      <div class="form-group">
        <label>Email to project enabled</label>
        <select name="email_to_project_enabled">
          <option value="1" <?php echo $fields['email_to_project_enabled'] === '1' ? 'selected' : ''; ?>>Yes</option>
          <option value="0" <?php echo $fields['email_to_project_enabled'] === '0' ? 'selected' : ''; ?>>No</option>
        </select>
      </div>

      <div class="form-group">
        <label>Auto create project directly</label>
        <select name="email_auto_create_project">
          <option value="0" <?php echo $fields['email_auto_create_project'] === '0' ? 'selected' : ''; ?>>No, create draft via inbox</option>
          <option value="1" <?php echo $fields['email_auto_create_project'] === '1' ? 'selected' : ''; ?>>Yes</option>
        </select>
      </div>

      <div class="form-group">
        <label>Default project status</label>
        <select name="email_default_project_status">
          <?php foreach (['planned','active','pending_close','completed','cancelled'] as $s): ?>
            <option value="<?php echo h($s); ?>" <?php echo $fields['email_default_project_status'] === $s ? 'selected' : ''; ?>>
              <?php echo h($s); ?>
            </option>
          <?php endforeach; ?>
        </select>
      </div>

      <div class="form-group">
        <label>Default category name</label>
        <input type="text" name="email_default_category_name" value="<?php echo h($fields['email_default_category_name']); ?>">
      </div>

      <div class="form-group">
        <label>IMAP host</label>
        <input type="text" name="imap_host" value="<?php echo h($fields['imap_host']); ?>" placeholder="mail.gopools.gr">
      </div>

      <div class="form-group">
        <label>IMAP port</label>
        <input type="text" name="imap_port" value="<?php echo h($fields['imap_port']); ?>">
      </div>

      <div class="form-group">
        <label>IMAP encryption</label>
        <input type="text" name="imap_encryption" value="<?php echo h($fields['imap_encryption']); ?>" placeholder="ssl">
      </div>

      <div class="form-group">
        <label>IMAP username</label>
        <input type="text" name="imap_username" value="<?php echo h($fields['imap_username']); ?>">
      </div>

      <div class="form-group">
        <label>IMAP password</label>
        <input type="password" name="imap_password" value="<?php echo h($fields['imap_password']); ?>">
      </div>

      <div class="form-group">
        <label>IMAP mailbox</label>
        <input type="text" name="imap_mailbox" value="<?php echo h($fields['imap_mailbox']); ?>">
      </div>

      <div class="form-group">
        <label>Fetch limit</label>
        <input type="text" name="imap_fetch_limit" value="<?php echo h($fields['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 emails now</a>
    </div>
  </form>
</div>

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