




						
<?php
declare(strict_types=1);

require_once __DIR__ . '/db.php';
require_once __DIR__ . '/content.php';

function h(?string $value): string
{
    return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
}

function url(string $path = ''): string
{
    $base = rtrim(BASE_URL, '/');
    return $base . '/' . ltrim($path, '/');
}

function asset(string $path): string
{
    return url($path);
}

function page_file(string $slug): string
{
    return $slug === 'home' ? 'index.php' : $slug . '.php';
}

function nav_items(): array
{
    return [
        'home' => ['label' => 'Home', 'href' => 'index.php'],
        'about' => ['label' => 'About', 'href' => 'about.php'],
        'solutions' => ['label' => 'Solutions', 'href' => 'solutions.php'],
        'technology' => ['label' => 'Technology', 'href' => 'technology.php'],
        'sustainability' => ['label' => 'Sustainability', 'href' => 'sustainability.php'],
        'blog' => ['label' => 'Blog', 'href' => 'blog.php'],
        'contact' => ['label' => 'Contact', 'href' => 'contact.php'],
    ];
}

function fetch_active_hero_slides(): array
{
    $db = db();
    if (!$db) {
        return default_hero_slides();
    }

    $sql = "SELECT title, subtitle, description, cta_text, cta_link, media_path, alt_text
            FROM hero_slides WHERE is_active = 1 ORDER BY sort_order ASC, id ASC";
    $result = $db->query($sql);
    if (!$result) {
        return default_hero_slides();
    }

    $rows = $result->fetch_all(MYSQLI_ASSOC);
    return $rows ?: default_hero_slides();
}

function fetch_active_services(): array
{
    $db = db();
    if (!$db) {
        return default_services();
    }

    $sql = "SELECT title, slug, description, icon, image_path, alt_text
            FROM services WHERE is_active = 1 ORDER BY sort_order ASC, id ASC";
    $result = $db->query($sql);
    if (!$result) {
        return default_services();
    }

    $rows = $result->fetch_all(MYSQLI_ASSOC);
    return $rows ?: default_services();
}

function fetch_published_blogs(int $limit = 12): array
{
    $db = db();
    if (!$db) {
        return array_slice(default_blogs(), 0, $limit);
    }

    $stmt = $db->prepare("SELECT title, slug, excerpt, content, image_path, alt_text, published_at
                          FROM blogs WHERE status = 'published'
                          ORDER BY published_at DESC, id DESC LIMIT ?");
    if (!$stmt) {
        return array_slice(default_blogs(), 0, $limit);
    }
    $stmt->bind_param('i', $limit);
    $stmt->execute();
    $rows = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
    return $rows ?: array_slice(default_blogs(), 0, $limit);
}

function fetch_blog_by_slug(string $slug): ?array
{
    $db = db();
    if ($db) {
        $stmt = $db->prepare("SELECT title, slug, excerpt, content, image_path, alt_text, published_at
                              FROM blogs WHERE slug = ? AND status = 'published' LIMIT 1");
        if ($stmt) {
            $stmt->bind_param('s', $slug);
            $stmt->execute();
            $row = $stmt->get_result()->fetch_assoc();
            if ($row) {
                return $row;
            }
        }
    }

    foreach (default_blogs() as $blog) {
        if ($blog['slug'] === $slug) {
            return $blog;
        }
    }
    return null;
}

function default_seo(string $slug): array
{
    $map = [
        'home' => ['Swizet Limited | AI Data Centre Optimization Platform', 'AI-powered data centre optimization for energy efficiency, cooling intelligence, carbon reporting and predictive infrastructure management.'],
        'about' => ['About Swizet Limited | Sustainable Data Centre Intelligence', 'Learn about Swizet Limited, a UK technology startup building an AI-powered intelligence layer for efficient and sustainable data centre operations.'],
        'solutions' => ['Solutions | AI Energy, Cooling and Carbon Optimization', 'Explore Swizet solutions for AI optimization, cooling analytics, predictive alerts, carbon tracking and energy insights for data centres.'],
        'technology' => ['Technology | Google Cloud AI Data Centre Platform', 'See how Swizet uses Google Cloud, Pub/Sub, Dataflow, BigQuery, Vertex AI, Cloud Run and Kubernetes to turn infrastructure data into intelligence.'],
        'sustainability' => ['Sustainability | ESG and Carbon Reporting for Data Centres', 'Swizet supports ESG, climate reporting, carbon insights and environmental metrics for sustainable digital infrastructure.'],
        'blog' => ['Blog | Swizet Data Centre Intelligence Insights', 'Read Swizet insights on AI optimization, cooling intelligence, carbon visibility and sustainable data centre operations.'],
        'contact' => ['Contact Swizet Limited | AI Data Centre Optimization', 'Contact Swizet Limited about AI-powered data centre optimization, cooling intelligence, predictive management and sustainability tracking.'],
    ];

    [$title, $description] = $map[$slug] ?? $map['home'];
    return [
        'meta_title' => $title,
        'meta_description' => $description,
        'meta_keywords' => 'Swizet, data centre optimization, AI cooling analytics, carbon reporting, energy optimization, sustainability tracking',
        'canonical_url' => APP_URL . '/' . page_file($slug),
        'schema_json' => '',
    ];
}

function get_seo(string $slug): array
{
    $seo = default_seo($slug);
    $db = db();
    if (!$db) {
        return $seo;
    }

    $stmt = $db->prepare("SELECT meta_title, meta_description, meta_keywords, canonical_url, schema_json
                          FROM seo_pages WHERE page_slug = ? LIMIT 1");
    if (!$stmt) {
        return $seo;
    }
    $stmt->bind_param('s', $slug);
    $stmt->execute();
    $row = $stmt->get_result()->fetch_assoc();
    if (!$row) {
        return $seo;
    }

    foreach ($seo as $key => $value) {
        if (isset($row[$key]) && trim((string) $row[$key]) !== '') {
            $seo[$key] = $row[$key];
        }
    }
    return $seo;
}

function organization_schema(): string
{
    return json_encode([
        '@context' => 'https://schema.org',
        '@type' => 'Organization',
        'name' => 'Swizet Limited',
        'url' => APP_URL,
        'description' => 'AI-powered intelligence platform for optimizing data centre operations, energy consumption, cooling efficiency and sustainability reporting.',
        'sameAs' => [],
    ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}

function breadcrumb_schema(array $items): string
{
    $list = [];
    foreach ($items as $position => $item) {
        $list[] = [
            '@type' => 'ListItem',
            'position' => $position + 1,
            'name' => $item['label'],
            'item' => APP_URL . '/' . ltrim($item['href'], '/'),
        ];
    }

    return json_encode([
        '@context' => 'https://schema.org',
        '@type' => 'BreadcrumbList',
        'itemListElement' => $list,
    ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}

function breadcrumbs(string $slug, ?string $label = null): array
{
    $items = [['label' => 'Home', 'href' => 'index.php']];
    if ($slug !== 'home') {
        $nav = nav_items();
        $items[] = ['label' => $label ?: ($nav[$slug]['label'] ?? ucfirst($slug)), 'href' => page_file($slug)];
    }
    return $items;
}

function active_class(string $slug, string $current): string
{
    return $slug === $current ? ' class="is-active"' : '';
}

function format_date(?string $date): string
{
    if (!$date) {
        return '';
    }
    $time = strtotime($date);
    return $time ? date('M j, Y', $time) : h($date);
}

function svg_icon(string $name): string
{
    $icons = [
        'ai' => '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3v18M4 12h16M7 7l10 10M17 7 7 17"/><circle cx="12" cy="12" r="3"/></svg>',
        'cooling' => '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3v18M5 7c5 4 9 4 14 0M5 17c5-4 9-4 14 0"/><circle cx="12" cy="12" r="2"/></svg>',
        'alerts' => '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M12 4 3 20h18L12 4Z"/><path d="M12 9v5M12 17h.01"/></svg>',
        'carbon' => '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M12 21c5-3 8-7 8-12V5l-8-3-8 3v4c0 5 3 9 8 12Z"/><path d="M9 13c2-4 5-5 8-6-1 5-3 8-8 8"/></svg>',
        'energy' => '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="m13 2-8 12h6l-1 8 9-13h-6l1-7Z"/></svg>',
    ];
    return $icons[$name] ?? $icons[