<?php
require_once __DIR__ . '/../../app/core/bootstrap.php';
require_once __DIR__ . '/../../app/core/project_permissions.php';
auth_require_login();

$pdo = crm_pdo();
if (!gp_can_manage_project_access($pdo)) {
    http_response_code(403); exit('Access denied.');
}

$page_title = 'General Settings';

function get_setting($pdo, $key, $default = '') {
    $st = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = ?");
    $st->execute([$key]);
    return $st->fetchColumn() ?: $default;
}

$ok = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    gp_require_csrf();
    $weatherKey = trim((string)($_POST['weather_api_key'] ?? ''));
    
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES ('weather_api_key', :v) ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)");
    $stmt->execute([':v' => $weatherKey]);
    
    $ok = 'Settings saved successfully.';
}

$weatherApiKey = get_setting($pdo, 'weather_api_key');

include __DIR__ . '/../includes/header.php';
?>
<div class="page-header">
  <div class="page-header-title">
    <h1>General Settings</h1>
    <span>Global system parameters and API integrations</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>

<?php if ($ok): ?><div class="alert-success" style="margin-bottom:24px;"><?php echo h($ok); ?></div><?php endif; ?>

<form method="post">
  <?php echo csrf_field(); ?>
  
  <div class="grid-2">
    <div class="card card-static">
      <div class="page-header-title" style="margin-bottom:20px;">
        <h2 style="font-size:1.1rem; margin:0;">🌦️ Weather Integration</h2>
        <span>Configure OpenWeatherMap API</span>
      </div>
      
      <div class="form-grid">
        <div class="form-group" style="grid-column:1/-1">
          <label>OpenWeatherMap API Key</label>
          <input type="text" name="weather_api_key" value="<?php echo h($weatherApiKey); ?>" placeholder="Enter your 32-character API key">
          <small style="color:var(--gp-muted); margin-top:8px; display:block;">
            Get a free key from <a href="https://openweathermap.org/api" target="_blank" rel="noopener" style="color:var(--gp-primary); font-weight:600;">openweathermap.org</a>.
            <br>Real weather data will appear on the dashboard calendar for projects with a valid "City".
          </small>
        </div>
      </div>
    </div>

    <!-- More settings can go here -->
    <div class="card card-static" style="opacity:0.6; pointer-events:none;">
      <div class="page-header-title" style="margin-bottom:20px;">
        <h2 style="font-size:1.1rem; margin:0;">🛠️ Maintenance</h2>
        <span>Advanced system controls</span>
      </div>
      <p class="small">Additional configuration options will be available in future updates.</p>
    </div>
  </div>

  <div class="card card-static" style="margin-top:24px; text-align:center; padding:32px;">
    <button class="btn btn-primary btn-lg" type="submit" style="min-width:240px;">Save General Settings</button>
  </div>
</form>

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