
<?php

require_once __DIR__ . '/admin/inc/config.php';

function seoNotFound(): void
{
    http_response_code(404);
    require __DIR__ . '/404.php';
    exit;
}

function routeExists(PDO $pdo, string $sql, array $params = []): bool
{
    $stmt = $pdo->prepare($sql);
    $stmt->execute($params);
    return $stmt->fetchColumn() !== false;
}

$path = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?: '/';

$segments = array_values(array_filter(
    explode('/', trim($path, '/')),
    static fn($segment) => $segment !== ''
));

$segments = array_map('rawurldecode', $segments);

foreach ($segments as $segment) {
    if (str_contains($segment, '/') || str_contains($segment, "\0")) {
        seoNotFound();
    }
}

$count = count($segments);

/*
|--------------------------------------------------------------------------
| ONE SEGMENT: CATEGORY
| Example: /liquid-painting-booth
|--------------------------------------------------------------------------
*/

if ($count === 1) {
    $categorySlug = $segments[0];

    if (routeExists($pdo, "
        SELECT 1
        FROM tbl_top_category
        WHERE top_category_slug = ?
        AND status = 1
        LIMIT 1
    ", [$categorySlug])) {
        $_GET['slug'] = $categorySlug;
        require __DIR__ . '/product-category.php';
        exit;
    }

    seoNotFound();
}

/*
|--------------------------------------------------------------------------
| TWO SEGMENTS
| A) /category/product
| B) /location/category
|--------------------------------------------------------------------------
*/

if ($count === 2) {
    [$first, $second] = $segments;

    // CATEGORY + PRODUCT

    if (routeExists($pdo, "
        SELECT 1
        FROM tbl_service AS s
        INNER JOIN tbl_top_category AS c
            ON c.id = s.top_category_id
        WHERE c.top_category_slug = ?
        AND s.product_slug = ?
        AND c.status = 1
        AND s.status = 1
        LIMIT 1
    ", [$first, $second])) {
        $_GET['category'] = $first;
        $_GET['slug'] = $second;

        require __DIR__ . '/product.php';
        exit;
    }

    // LOCATION + CATEGORY


 
    if (routeExists($pdo, "
        SELECT 1
        FROM tbl_location AS l
        INNER JOIN tbl_top_category AS c
            ON c.top_category_slug = ?
        WHERE l.slug = ?
        AND l.status = 1
        AND c.status = 1
        AND (
            EXISTS (
                SELECT 1
                FROM tbl_service_location AS sl
                WHERE sl.location_id = l.id
                AND sl.category_id = c.id
            )
            OR EXISTS (
                SELECT 1
                FROM tbl_service_location AS sl
                INNER JOIN tbl_service AS s
                    ON s.id = sl.service_id
                WHERE sl.location_id = l.id
                AND s.top_category_id = c.id
                AND s.status = 1
            )
        )
        LIMIT 1
    ", [$second, $first])) {
        $_GET['location'] = $first;
        $_GET['slug'] = $second;
    
        require __DIR__ . '/product-category.php';
        exit;
    }


    seoNotFound();
}

/*
|--------------------------------------------------------------------------
| THREE SEGMENTS: LOCATION + CATEGORY + PRODUCT
| Example: /dubai/powder-coating-plants/product-slug
|--------------------------------------------------------------------------
*/

if ($count === 3) {
    [$locationSlug, $categorySlug, $productSlug] = $segments;

    if (routeExists($pdo, "
        SELECT 1
        FROM tbl_service_location AS sl
        INNER JOIN tbl_location AS l
            ON l.id = sl.location_id
        INNER JOIN tbl_service AS s
            ON s.id = sl.service_id
        INNER JOIN tbl_top_category AS c
            ON c.id = s.top_category_id
        WHERE l.slug = ?
        AND c.top_category_slug = ?
        AND s.product_slug = ?
        AND l.status = 1
        AND c.status = 1
        AND s.status = 1
        LIMIT 1
    ", [
        $locationSlug,
        $categorySlug,
        $productSlug
    ])) {
        $_GET['location'] = $locationSlug;
        $_GET['category'] = $categorySlug;
        $_GET['product'] = $productSlug;

        require __DIR__ . '/location_detail.php';
        exit;
    }

    seoNotFound();
}

seoNotFound();
