<?php
declare(strict_types=1);

$type = preg_replace('/[^a-z_]/', '', strtolower($_GET['type'] ?? 'page'));
$slug = preg_replace('/[^a-z0-9-]/', '', strtolower($_GET['slug'] ?? 'home'));

$allowed = ['page', 'service', 'location', 'blog', 'blog_post'];
if (!in_array($type, $allowed, true)) {
    $type = 'page';
    $slug = 'home';
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && $type === 'page' && $slug === 'contact') {
    require __DIR__ . '/contact.php';
    exit;
}

$template = match($type) {
    'service'   => __DIR__ . '/templates/service.php',
    'location'  => __DIR__ . '/templates/location.php',
    'blog', 'blog_post' => __DIR__ . '/templates/blog.php',
    default     => __DIR__ . '/templates/page.php',
};

if (!file_exists($template)) {
    http_response_code(404);
    require __DIR__ . '/templates/page.php';
    exit;
}

require $template;
