<?php
declare(strict_types=1);

require_once __DIR__ . '/api/app.php';

sv_ensure_schema();

$community = trim((string) ($_GET['community'] ?? ''));
$city = trim((string) ($_GET['city'] ?? ''));
$caste = trim((string) ($_GET['caste'] ?? ''));
$manglik = trim((string) ($_GET['manglik'] ?? ''));

$where = ["u.profile_status = 'active'"];
$params = [];
if ($community !== '') {
    $where[] = 'u.community LIKE ?';
    $params[] = '%' . $community . '%';
}
if ($city !== '') {
    $where[] = 'u.city LIKE ?';
    $params[] = '%' . $city . '%';
}
if ($caste !== '') {
    $where[] = 'u.caste LIKE ?';
    $params[] = '%' . $caste . '%';
}
if ($manglik !== '') {
    $where[] = 'd.manglik = ?';
    $params[] = $manglik;
}

$labelParts = array_filter([$community, $caste, $city, $manglik ? $manglik . ' Manglik' : '']);
$label = $labelParts ? implode(' ', $labelParts) . ' Matrimony' : 'Verified Matrimony Profiles India';
$title = $label . ' | Bride Groom Profiles - Sapt Vachan';
$desc = 'Browse active Hindu bride and groom profiles on Sapt Vachan. Filter matrimony by community, caste, city, gotra and manglik status.';

$sql = "SELECT u.*, f.father_occupation, f.mother_occupation, f.siblings, f.family_type, f.family_status,
               d.dob, d.marital_status, d.gotra, d.manglik, d.birth_time, d.birth_place, d.rashi, d.nakshatra,
               d.diet, d.blood_group, d.disability, d.legal_case, d.college, d.education_type,
               d.company_name, d.working_status, d.hobbies, d.partner_preference, d.life_goal, d.life_ambitions
        FROM users u
        LEFT JOIN user_family f ON f.user_id = u.id
        LEFT JOIN user_profile_details d ON d.user_id = u.id
        WHERE " . implode(' AND ', $where) . "
        ORDER BY u.is_premium DESC, u.is_verified DESC, u.created_at DESC
        LIMIT 48";
$stmt = db()->prepare($sql);
$stmt->execute($params);
$profiles = array_map('sv_public_user', $stmt->fetchAll());
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><?= h($title) ?></title>
  <meta name="description" content="<?= h($desc) ?>">
  <meta name="robots" content="index, follow, max-image-preview:large">
  <link rel="canonical" href="https://www.saptvachan.com/matrimony-profiles.php">
  <link rel="stylesheet" href="styles.css">
  <link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>💕</text></svg>">
</head>
<body>
  <div class="page-loader"><div class="loader-heart"></div></div>
  <nav class="navbar"><div class="container"><a href="index.html" class="nav-brand"><div class="logo-icon">💕</div><h2>Sapt<span>Vachan</span></h2></a><div class="nav-links"><a href="index.html">Home</a><a href="search.html">Search</a><a href="matches.html">AI Matches</a><a href="blog.html">Blog</a><a href="help.html">Help</a></div><div class="nav-actions"><div class="auth-links"><a href="login.html" class="btn btn-sm btn-outline">Login</a><a href="register.html" class="btn btn-sm btn-primary">Register</a></div><button class="hamburger" aria-label="Menu"><span></span><span></span><span></span></button></div></div></nav>
  <div class="mobile-overlay"></div>

  <section class="seo-landing-hero">
    <div class="container">
      <div class="hero-badge">Real Profile Listings</div>
      <h1><?= h($label) ?></h1>
      <p><?= h($desc) ?></p>
      <div class="seo-landing-actions">
        <a href="register.html" class="btn btn-primary btn-lg">Register Free</a>
        <a href="search.html" class="btn btn-outline btn-lg">Advanced Search</a>
      </div>
    </div>
  </section>

  <section class="section">
    <div class="container">
      <div class="results-grid" style="grid-template-columns:repeat(auto-fill,minmax(280px,1fr));">
        <?php foreach ($profiles as $profile): ?>
          <article class="card profile-card">
            <img src="<?= h(profilePlaceholderPhoto($profile['name'])) ?>" alt="<?= h($profile['name']) ?> matrimony profile" class="card-img" loading="lazy">
            <div class="card-body">
              <h2 class="card-name"><?= h($profile['name']) ?></h2>
              <p class="card-meta"><?= (int) $profile['age'] ?> yrs | <?= h($profile['city']) ?> | <?= h($profile['profession']) ?></p>
              <div class="card-tags">
                <span class="tag"><?= h($profile['caste'] ?: $profile['religion']) ?></span>
                <span class="tag"><?= h($profile['community']) ?></span>
                <?php if ($profile['manglik']): ?><span class="tag"><?= h($profile['manglik']) ?></span><?php endif; ?>
              </div>
              <a href="profile.html?id=<?= (int) $profile['id'] ?>" class="btn btn-primary btn-sm">View Profile</a>
            </div>
          </article>
        <?php endforeach; ?>
      </div>
      <?php if (!$profiles): ?>
        <div class="no-results"><h2>No profiles found</h2><p>Try another community, caste or city filter.</p></div>
      <?php endif; ?>
    </div>
  </section>

  <button class="scroll-top" title="Scroll to top">↑</button>
  <script src="script.js"></script>
</body>
</html>
