<?php
declare(strict_types=1);

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

ini_set('log_errors', '1');
ini_set('error_log', __DIR__ . '/php-error.log');

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

function normalizeHost(string $host): string {
    $host = strtolower(trim($host));
    $host = (string)preg_replace('/:\d+$/', '', $host);
    return $host;
}

try {
    // Config: try relative first (good when moved), then absolute fallback
    $configCandidates = [
        __DIR__ . '/../../config/config5.php',
        '/home/fvme60ccdm8r/config/config5.php'
    ];

    $configPath = null;
    foreach ($configCandidates as $p) {
        if (is_readable($p)) {
            $configPath = $p;
            break;
        }
    }
    if (!$configPath) {
        throw new Exception('Config file missing or not readable. Tried: ' . implode(' | ', $configCandidates));
    }
    require_once $configPath;

    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

    $domains = [];
    $res = $conn->query("SELECT name FROM domains WHERE name IS NOT NULL AND name <> '' ORDER BY name ASC");
    while ($row = $res->fetch_assoc()) {
        $domains[] = $row['name'];
    }
    $res->free();
    $conn->close();

    $host = normalizeHost($_SERVER['HTTP_HOST'] ?? 'vsatdomains.com');
    $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';

    // Canonical should match the actual public URL for this page.
    // If this page is always at /all-domains.php in the site root, keep it simple:
    $canonical = $scheme . '://' . $host . '/all-domains.php';

    // landing.php is now in /portfolio/
    $landingPath = '/portfolio/landing.php';

} catch (Throwable $e) {
    http_response_code(500);
    echo "<pre style='white-space:pre-wrap;color:red;'>";
    echo "FATAL ERROR: " . htmlspecialchars($e->getMessage()) . "\n";
    echo "File: " . htmlspecialchars($e->getFile()) . "\n";
    echo "Line: " . (int)$e->getLine() . "\n";
    echo "</pre>";
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>All Domains For Sale - VSATDomains</title>
    <meta name="description" content="Browse the complete VSATDomains portfolio. View all available domains.">
    <meta name="robots" content="index, follow">
    <link rel="canonical" href="<?php echo htmlspecialchars($canonical); ?>">

    <!-- If styles.css is in the SITE ROOT, use /styles.css -->
    <link rel="stylesheet" href="/styles.css">

    <style>
        body{
            font-family: Arial, sans-serif;
            margin:0;
            padding:0;
            background:#fff;
            color:#111;
        }
        header{
            background:#000;
            color:#fff;
            text-align:center;
            padding:18px 0;
        }
        header h1{
            margin:0;
            font-size:2.2rem;
        }
        header p{
            margin:6px 0 0;
            font-size:1rem;
            color:#ddd;
        }
        nav{
            margin-top:10px;
        }
        nav a{
            color:#fff;
            text-decoration:none;
            margin:0 10px;
            font-size:15px;
        }
        nav a:hover{
            text-decoration:underline;
        }
        .wrap{
            max-width:1100px;
            margin:0 auto;
            padding:24px 16px 40px;
        }
        .title-row{
            display:flex;
            flex-wrap:wrap;
            align-items:center;
            justify-content:space-between;
            gap:10px;
            margin-bottom:14px;
        }
        .title-row h2{
            margin:0;
            font-size:1.6rem;
        }
        .count{
            color:#555;
            font-size:14px;
        }
        .domain-grid{
            display:grid;
            grid-template-columns:repeat(auto-fill,minmax(220px,1fr));
            gap:12px;
            margin-top:10px;
        }
        .domain-card{
            border:1px solid #eee;
            border-radius:8px;
            padding:12px 12px;
            background:#fafafa;
            transition:0.15s ease;
        }
        .domain-card:hover{
            background:#fff;
            border-color:#ddd;
            transform:translateY(-1px);
        }
        .domain-card a{
            color:#111;
            text-decoration:none;
            font-weight:700;
            word-break:break-word;
        }
        .domain-card a:hover{
            text-decoration:underline;
        }
        footer{
            background:#000;
            color:#fff;
            text-align:center;
            padding:16px 0;
            font-size:14px;
        }
    </style>
</head>
<body>
    <header>
        <h1>VSATDomains.com</h1>
        <p>Complete Portfolio</p>
        <nav>
  <a href="/portfolio/index.php?page=1">Home</a>
  <a href="/portfolio/all-domains.php">All Domains</a>
  <a href="/portfolio/about.php">About</a>
  <a href="/portfolio/contact.php">Contact</a>
</nav>
    </header>

    <div class="wrap">
        <div class="title-row">
            <h2>All Domains For Sale</h2>
            <div class="count"><?php echo count($domains); ?> domains listed</div>
        </div>

        <?php if (empty($domains)): ?>
            <p>No domains found.</p>
        <?php else: ?>
            <div class="domain-grid">
                <?php foreach ($domains as $d): ?>
                    <div class="domain-card">
                        <a href="<?php echo htmlspecialchars($landingPath); ?>?domain=<?php echo urlencode($d); ?>">
                            <?php echo htmlspecialchars($d); ?>
                        </a>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>

    <footer>
        © 2025 VSATDomains.com All Rights Reserved
    </footer>
</body>
</html>
