<?php
require_once __DIR__ . '/../../app/core/bootstrap.php';
require_once __DIR__ . '/../../app/lib/mailer.php';
auth_require_login();
$pdo = crm_pdo();
$page_title = 'SMTP settings';

$fields = [
    'smtp_enabled' => gp_setting($pdo, 'smtp_enabled', '0'),
    'smtp_host' => gp_setting($pdo, 'smtp_host', ''),
    'smtp_port' => gp_setting($pdo, 'smtp_port', '587'),
    'smtp_secure' => gp_setting($pdo, 'smtp_secure', 'tls'),
    'smtp_username' => gp_setting($pdo, 'smtp_username', ''),
    'smtp_password' => gp_setting($pdo, 'smtp_password', ''),
    'smtp_from_email' => gp_setting($pdo, 'smtp_from_email', 'projects@gopools.gr'),
    'smtp_from_name' => gp_setting($pdo, 'smtp_from_name', 'GoPools CRM'),
    'smtp_reply_to' => gp_setting($pdo, 'smtp_reply_to', ''),
    'smtp_test_email' => gp_setting($pdo, 'smtp_test_email', ''),
];

$ok = '';
$errors = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    gp_require_csrf();
    foreach (array_keys($fields) as $k) {
        $fields[$k] = ($k === 'smtp_enabled') ? (isset($_POST[$k]) ? '1' : '0') : 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]);
    }

    if (isset($_POST['send_test'])) {
        $to = $fields['smtp_test_email'] !== '' ? $fields['smtp_test_email'] : $fields['smtp_from_email'];
        $error = '';
        $sent = gp_smtp_send($pdo, $to, 'GoPools SMTP Test', "This is a test email from GoPools CRM.\n\nSMTP settings appear to be working.", [], $error);
        if ($sent) $ok = 'Settings saved and test email sent.';
        else $errors[] = 'Settings saved, but test failed: ' . $error;
    } else {
        $ok = 'SMTP settings saved.';
    }
}

include __DIR__ . '/../includes/header.php';
?>
<div class="page-header">
  <div class="page-header-title">
    <h1>SMTP settings</h1>
    <span>Outgoing email engine for project dispatch</span>
  </div>
  <div class="page-header-actions">
    <a class="btn btn-secondary btn-sm" href="/gopools/public/settings/index.php">Back to settings</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>Enable SMTP</label><div><input type="checkbox" name="smtp_enabled" <?php echo $fields['smtp_enabled'] === '1' ? 'checked' : ''; ?>></div></div>
      <div class="form-group"><label>SMTP host</label><input type="text" name="smtp_host" value="<?php echo h($fields['smtp_host']); ?>" placeholder="mail.gopools.gr"></div>
      <div class="form-group"><label>SMTP port</label><input type="text" name="smtp_port" value="<?php echo h($fields['smtp_port']); ?>"></div>
      <div class="form-group"><label>Encryption</label><select name="smtp_secure"><?php foreach (['tls','ssl','none'] as $s): ?><option value="<?php echo h($s); ?>" <?php echo $fields['smtp_secure'] === $s ? 'selected' : ''; ?>><?php echo h($s); ?></option><?php endforeach; ?></select></div>
      <div class="form-group"><label>SMTP username</label><input type="text" name="smtp_username" value="<?php echo h($fields['smtp_username']); ?>"></div>
      <div class="form-group"><label>SMTP password</label><input type="password" name="smtp_password" value="<?php echo h($fields['smtp_password']); ?>"></div>
      <div class="form-group"><label>From email</label><input type="email" name="smtp_from_email" value="<?php echo h($fields['smtp_from_email']); ?>"></div>
      <div class="form-group"><label>From name</label><input type="text" name="smtp_from_name" value="<?php echo h($fields['smtp_from_name']); ?>"></div>
      <div class="form-group"><label>Reply-to</label><input type="email" name="smtp_reply_to" value="<?php echo h($fields['smtp_reply_to']); ?>"></div>
      <div class="form-group"><label>Test email</label><input type="email" name="smtp_test_email" value="<?php echo h($fields['smtp_test_email']); ?>" placeholder="you@example.com"></div>
    </div>
    <div class="form-actions">
      <button class="btn btn-primary" type="submit" name="save_only" value="1">Save settings</button>
      <button class="btn btn-secondary" type="submit" name="send_test" value="1">Save + send test</button>
    </div>
  </form>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
