<?php
require_once __DIR__ . '/../../app/core/bootstrap.php';
require_once __DIR__ . '/../../app/core/project_permissions.php';
require_once __DIR__ . '/../../app/core/theme.php';
auth_require_login();
if (!gp_can_manage_project_access($pdo = crm_pdo())) {
    http_response_code(403);
    exit('Access denied.');
}
$page_title = 'Branding & Theme';

$fields = [
    'brand_app_name' => gp_theme_setting($pdo, 'brand_app_name', 'GoPools CRM'),
    'brand_logo' => gp_theme_setting($pdo, 'brand_logo', ''),
    'brand_logo_height' => gp_theme_setting($pdo, 'brand_logo_height', '34'),
    'brand_favicon' => gp_theme_setting($pdo, 'brand_favicon', ''),
    'theme_mode' => gp_theme_setting($pdo, 'theme_mode', 'light'),
    'theme_primary' => gp_theme_setting($pdo, 'theme_primary', '#2563eb'),
    'theme_secondary' => gp_theme_setting($pdo, 'theme_secondary', '#0f172a'),
    'theme_glass' => gp_theme_setting($pdo, 'theme_glass', '1'),
    'ui_border_radius' => gp_theme_setting($pdo, 'ui_border_radius', '20'),
    'sidebar_width' => gp_theme_setting($pdo, 'sidebar_width', '260'),
    'font_family' => gp_theme_setting($pdo, 'font_family', 'Outfit'),
    'background_type' => gp_theme_setting($pdo, 'background_type', 'gradient'),
    'background_color' => gp_theme_setting($pdo, 'background_color', '#f8fafc'),
    'background_image' => gp_theme_setting($pdo, 'background_image', ''),
    'background_gradient' => gp_theme_setting($pdo, 'background_gradient', 'linear-gradient(135deg,#f8fafc 0%,#e2e8f0 100%)'),
    'background_overlay_opacity' => gp_theme_setting($pdo, 'background_overlay_opacity', '0.10'),
];

$ok = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    gp_require_csrf();
    foreach (array_keys($fields) as $k) {
        $fields[$k] = ($k === 'theme_glass') ? (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]);
    }
    $ok = 'Branding settings saved successfully.';
}

include __DIR__ . '/../includes/header.php';
?>
<div class="page-header">
  <div class="page-header-title">
    <h1>Branding & Theme</h1>
    <span>Complete control over your CRM's aesthetic identity</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">
    <!-- Identity Section -->
    <div class="card card-static">
      <div class="page-header-title" style="margin-bottom:20px;">
        <h2 style="font-size:1.1rem; margin:0;">Brand Identity</h2>
        <span>App name, logos, and icons</span>
      </div>
      <div class="form-grid">
        <div class="form-group" style="grid-column:1/-1">
          <label>App Display Name</label>
          <input type="text" name="brand_app_name" value="<?php echo h($fields['brand_app_name']); ?>">
        </div>
        <div class="form-group">
          <label>Logo URL</label>
          <input type="text" name="brand_logo" value="<?php echo h($fields['brand_logo']); ?>" placeholder="/uploads/logo.png">
        </div>
        <div class="form-group">
          <label>Logo Height (px)</label>
          <input type="number" name="brand_logo_height" value="<?php echo h($fields['brand_logo_height']); ?>" min="20" max="100">
        </div>
        <div class="form-group" style="grid-column:1/-1">
          <label>Favicon URL</label>
          <input type="text" name="brand_favicon" value="<?php echo h($fields['brand_favicon']); ?>" placeholder="/favicon.ico">
        </div>
      </div>
    </div>

    <!-- UI Preferences -->
    <div class="card card-static">
      <div class="page-header-title" style="margin-bottom:20px;">
        <h2 style="font-size:1.1rem; margin:0;">UI Architecture</h2>
        <span>Layout, spacing, and typography</span>
      </div>
      <div class="form-grid">
        <div class="form-group">
          <label>Font Family</label>
          <select name="font_family">
            <?php 
            $fonts = ['Outfit', 'Inter', 'Roboto', 'Montserrat', 'Poppins', 'Outfit', 'Open Sans'];
            foreach (array_unique($fonts) as $f): ?>
              <option value="<?php echo h($f); ?>" <?php echo $fields['font_family']===$f?'selected':''; ?>><?php echo h($f); ?></option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="form-group">
          <label>Border Radius (px)</label>
          <input type="number" name="ui_border_radius" value="<?php echo h($fields['ui_border_radius']); ?>" min="0" max="40">
        </div>
        <div class="form-group">
          <label>Sidebar Width (px)</label>
          <input type="number" name="sidebar_width" value="<?php echo h($fields['sidebar_width']); ?>" min="200" max="400">
        </div>
        <div class="form-group">
          <label>Glassmorphism Effects</label>
          <div style="display:flex; align-items:center; gap:10px; margin-top:8px;">
            <input type="checkbox" name="theme_glass" id="theme_glass" <?php echo $fields['theme_glass']==='1'?'checked':''; ?> style="width:20px;height:20px;">
            <label for="theme_glass" style="margin:0; font-weight:500;">Enable backdrop blur</label>
          </div>
        </div>
      </div>
    </div>

    <!-- Color Palette -->
    <div class="card card-static">
      <div class="page-header-title" style="margin-bottom:20px;">
        <h2 style="font-size:1.1rem; margin:0;">Color System</h2>
        <span>Theme mode and primary accents</span>
      </div>
      <div class="form-grid">
        <div class="form-group">
          <label>Default Mode</label>
          <select name="theme_mode">
            <option value="light" <?php echo $fields['theme_mode']==='light'?'selected':''; ?>>Light Premium</option>
            <option value="dark" <?php echo $fields['theme_mode']==='dark'?'selected':''; ?>>Dark Premium</option>
            <option value="auto" <?php echo $fields['theme_mode']==='auto'?'selected':''; ?>>Auto (System)</option>
          </select>
        </div>
        <div class="form-group"></div>
        <div class="form-group">
          <label>Primary Accent</label>
          <div style="display:flex; gap:10px;">
            <input type="color" name="theme_primary" value="<?php echo h($fields['theme_primary']); ?>" style="width:50px; height:42px; padding:2px;">
            <input type="text" value="<?php echo h($fields['theme_primary']); ?>" readonly style="flex:1; background:rgba(0,0,0,0.02);">
          </div>
        </div>
        <div class="form-group">
          <label>Secondary Accent</label>
          <div style="display:flex; gap:10px;">
            <input type="color" name="theme_secondary" value="<?php echo h($fields['theme_secondary']); ?>" style="width:50px; height:42px; padding:2px;">
            <input type="text" value="<?php echo h($fields['theme_secondary']); ?>" readonly style="flex:1; background:rgba(0,0,0,0.02);">
          </div>
        </div>
      </div>
    </div>

    <!-- Background Engine -->
    <div class="card card-static">
      <div class="page-header-title" style="margin-bottom:20px;">
        <h2 style="font-size:1.1rem; margin:0;">Background Engine</h2>
        <span>Global app background configuration</span>
      </div>
      <div class="form-grid">
        <div class="form-group">
          <label>Background Type</label>
          <select name="background_type">
            <option value="color" <?php echo $fields['background_type']==='color'?'selected':''; ?>>Solid Color</option>
            <option value="image" <?php echo $fields['background_type']==='image'?'selected':''; ?>>Image</option>
            <option value="gradient" <?php echo $fields['background_type']==='gradient'?'selected':''; ?>>Dynamic Gradient</option>
          </select>
        </div>
        <div class="form-group">
          <label>Overlay Opacity</label>
          <input type="number" step="0.01" min="0" max="0.90" name="background_overlay_opacity" value="<?php echo h($fields['background_overlay_opacity']); ?>">
        </div>
        <div class="form-group" style="grid-column:1/-1">
          <label>Gradient CSS / Image URL</label>
          <input type="text" name="background_gradient" value="<?php echo h($fields['background_gradient']); ?>" placeholder="linear-gradient(...) or /path/to/img.jpg">
          <?php if($fields['background_type'] === 'image'): ?>
            <input type="hidden" name="background_image" value="<?php echo h($fields['background_gradient']); ?>">
          <?php endif; ?>
        </div>
      </div>
    </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; font-size:1rem; padding:14px 32px;">Apply Architecture Changes</button>
  </div>
</form>

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