<?php 
include 'includes/db.php';
include 'includes/url-helper.php';

// Get current city and service from URL parameters
$citySlug    = $_GET['city'] ?? '';
$serviceSlug = $_GET['service'] ?? '';

// Validate parameters
if (!$citySlug || !$serviceSlug) {
    header("Location: services.php");
    exit;
}

// If accessed via old URL format (direct city-service.php), redirect to SEO-friendly URL
if (isset($_GET['city']) && isset($_GET['service']) && 
    strpos($_SERVER['REQUEST_URI'], 'city-service.php') !== false && 
    strpos($_SERVER['REQUEST_URI'], '?') !== false) {
    redirectToSeoUrl(https://www.finggu.com/$citySlug, $serviceSlug);
}

// Fetch the current page data
$stmt = $pdo->prepare("
    SELECT cs.*, c.name as city_name, c.slug as city_slug, s.name as service_name, s.slug as service_slug
    FROM city_service cs
    JOIN cities c ON cs.city_id = c.id
    JOIN services s ON cs.service_id = s.id
    WHERE c.slug = ? AND s.slug = ?
");
$stmt->execute([$citySlug, $serviceSlug]);
$page = $stmt->fetch();

if (!$page) {
    http_response_code(404);
    header("Location: services.php");
    exit;
}

// Enhanced SEO variables
$serviceName = $page['service_name'];
$cityName = $page['city_name'];
$currentYear = date('Y');

// Advanced SEO title and description
$pageTitle = $page['seo_title'] ?: "Best $serviceName Services in $cityName $currentYear | Finggu Infotech";
$pageDescription = $page['seo_description'] ?: "★★★★★ Top-rated $serviceName services in $cityName. 500+ projects completed. Get free consultation. Call +91-8655442429. Expert team, affordable prices.";

echo "<h1>Minimal Test - $pageTitle</h1>";
echo "<p>City: $cityName</p>";
echo "<p>Service: $serviceName</p>";
echo "<p>This is a minimal version without header.php to test for redirects.</p>";
?>