<?php
/**
 * /sitemap.php — Dynamic XML sitemap
 * Lists: homepage, static pages, all CRO profile pages, and paginated directory pages.
 * Google discovers every CRO in one crawl instead of paginating through the directory.
 */
declare(strict_types=1);
header('Content-Type: application/xml; charset=UTF-8');

$baseUrl = 'https://www.invivolabs.com';

// ── Bootstrap DB ──
try {
    $bootstrap = $_SERVER['DOCUMENT_ROOT'] . '/public/_bootstrap.php';
    if (!is_file($bootstrap)) throw new RuntimeException("Bootstrap not found");
    require_once $bootstrap;
    $pdo = IVL_DB::pdo();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // ── Table/column discovery (same pattern as search) ──
    $firstExisting = function(PDO $pdo, array $candidates): ?string {
        foreach ($candidates as $t) {
            try { $pdo->query("SELECT 1 FROM `{$t}` LIMIT 0"); return $t; } catch(Throwable $e){}
        }
        return null;
    };
    $getCols = function(PDO $pdo, string $table): array {
        return array_fill_keys($pdo->query("SHOW COLUMNS FROM `{$table}`")->fetchAll(PDO::FETCH_COLUMN), true);
    };
    $pick = function(array $cols, array $candidates): ?string {
        foreach ($candidates as $c) if (isset($cols[$c])) return $c;
        return null;
    };

    $T_ORGS = $firstExisting($pdo, ['organizations','orgs','organization','cro_orgs','companies','vendors','ivl_orgs','org']);
    if (!$T_ORGS) throw new RuntimeException("No orgs table");

    $orgCols    = $getCols($pdo, $T_ORGS);
    $C_ID       = $pick($orgCols, ['id','org_id']) ?? 'id';
    $C_SLUG     = $pick($orgCols, ['slug','org_slug']) ?? 'slug';
    $C_NAME     = $pick($orgCols, ['name','org_name','title']) ?? 'name';
    $C_VERIFIED = $pick($orgCols, ['is_verified','verified']);
    $C_TEST     = $pick($orgCols, ['is_test','test']);
    $C_UPDATED  = $pick($orgCols, ['updated_at','modified_at','last_updated','updated']);

    // ── Visibility filters (same as search.php) ──
    $where = [];
    if ($C_VERIFIED) {
        $where[] = "(o.`$C_VERIFIED` = 1 OR NOT EXISTS (SELECT 1 FROM users u WHERE u.org_id = o.`$C_ID` AND u.role_id = 3))";
    }
    if ($C_TEST) {
        $where[] = "(o.`$C_TEST` = 0 OR o.`$C_TEST` IS NULL)";
    }
    $sqlWhere = $where ? ('WHERE ' . implode(' AND ', $where)) : '';

    // ── Fetch all visible CRO slugs ──
    $cols = "o.`$C_SLUG` AS slug";
    if ($C_VERIFIED) $cols .= ", o.`$C_VERIFIED` AS is_verified";
    if ($C_UPDATED)  $cols .= ", o.`$C_UPDATED` AS updated_at";

    $stmt = $pdo->query("SELECT $cols FROM `{$T_ORGS}` o $sqlWhere ORDER BY o.`$C_NAME` ASC");
    $orgs = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // ── Count for pagination URLs ──
    $perPage    = 10;
    $totalOrgs  = count($orgs);
    $totalPages = max(1, (int)ceil($totalOrgs / $perPage));

} catch (Throwable $e) {
    error_log('sitemap.php error: ' . $e->getMessage());
    $orgs       = [];
    $totalPages = 1;
}

// ── Output XML ──
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

  <!-- ── Core pages ── -->
  <url>
    <loc><?= $baseUrl ?>/</loc>
    <changefreq>weekly</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc><?= $baseUrl ?>/public/search</loc>
    <changefreq>daily</changefreq>
    <priority>0.9</priority>
  </url>
  <url>
    <loc><?= $baseUrl ?>/contract-research-organizations</loc>
    <changefreq>monthly</changefreq>
    <priority>0.7</priority>
  </url>
  <url>
    <loc><?= $baseUrl ?>/services.php</loc>
    <changefreq>monthly</changefreq>
    <priority>0.6</priority>
  </url>
  <url>
    <loc><?= $baseUrl ?>/resources</loc>
    <changefreq>weekly</changefreq>
    <priority>0.6</priority>
  </url>
  <url>
    <loc><?= $baseUrl ?>/blog</loc>
    <changefreq>weekly</changefreq>
    <priority>0.6</priority>
  </url>
  <url>
    <loc><?= $baseUrl ?>/glossary</loc>
    <changefreq>monthly</changefreq>
    <priority>0.5</priority>
  </url>
  <url>
    <loc><?= $baseUrl ?>/faq</loc>
    <changefreq>monthly</changefreq>
    <priority>0.5</priority>
  </url>
  <url>
    <loc><?= $baseUrl ?>/about</loc>
    <changefreq>monthly</changefreq>
    <priority>0.4</priority>
  </url>
  <url>
    <loc><?= $baseUrl ?>/contact</loc>
    <changefreq>monthly</changefreq>
    <priority>0.4</priority>
  </url>
  <url>
    <loc><?= $baseUrl ?>/intrathecal-catheterization</loc>
    <changefreq>monthly</changefreq>
    <priority>0.8</priority>
  </url>
  <url>
    <loc><?= $baseUrl ?>/advertise</loc>
    <changefreq>monthly</changefreq>
    <priority>0.3</priority>
  </url>
  <url>
    <loc><?= $baseUrl ?>/privacy</loc>
    <changefreq>yearly</changefreq>
    <priority>0.2</priority>
  </url>
  <url>
    <loc><?= $baseUrl ?>/terms</loc>
    <changefreq>yearly</changefreq>
    <priority>0.2</priority>
  </url>

  <!-- ── Directory pagination pages ── -->
<?php for ($p = 2; $p <= $totalPages; $p++): ?>
  <url>
    <loc><?= $baseUrl ?>/public/search?page=<?= $p ?></loc>
    <changefreq>daily</changefreq>
    <priority>0.7</priority>
  </url>
<?php endfor; ?>

  <!-- ── Individual CRO profile pages ── -->
<?php foreach ($orgs as $org):
    $slug     = htmlspecialchars($org['slug'], ENT_XML1);
    $verified = !empty($org['is_verified']);
    $lastmod  = '';
    if (!empty($org['updated_at'])) {
        $ts = strtotime($org['updated_at']);
        if ($ts) $lastmod = date('Y-m-d', $ts);
    }
?>
  <url>
    <loc><?= $baseUrl ?>/public/org.php?slug=<?= $slug ?></loc>
<?php if ($lastmod): ?>
    <lastmod><?= $lastmod ?></lastmod>
<?php endif; ?>
    <changefreq><?= $verified ? 'weekly' : 'monthly' ?></changefreq>
    <priority><?= $verified ? '0.8' : '0.6' ?></priority>
  </url>
<?php endforeach; ?>

</urlset>