<?php
// City-Service Links Component
// Include this file to show city-specific service links

include_once 'db.php';
include_once __DIR__ . '/url-helper.php';

function getCityServiceLinks($serviceSlug, $limit = 5) {
    global $pdo;
    
    $stmt = $pdo->prepare("
        SELECT c.slug, c.name 
        FROM city_service cs
        JOIN cities c ON cs.city_id = c.id
        JOIN services s ON cs.service_id = s.id
        WHERE s.slug = ?
        ORDER BY c.name
        LIMIT ?
    ");
    $stmt->execute([$serviceSlug, $limit]);
    return $stmt->fetchAll();
}

function renderCityLinks($serviceSlug, $serviceName) {
    $cities = getCityServiceLinks($serviceSlug, 5);
    
    if (empty($cities)) {
        return '';
    }
    
    $html = '<div class="mt-4">';
    $html .= '<div class="text-sm text-gray-600 mb-2">Available in:</div>';
    $html .= '<div class="flex flex-wrap gap-2">';
    
    foreach ($cities as $city) {
        $url = getCityServiceUrl($city['slug'], $serviceSlug);
        $html .= '<a href="' . $url . '" ';
        $html .= 'class="inline-block bg-blue-50 text-blue-700 hover:bg-blue-100 px-3 py-1 rounded-full text-sm font-medium transition-colors">';
        $html .= htmlspecialchars($city['name']);
        $html .= '</a>';
    }
    
    // Add "View All Cities" link
    $url = getServiceCitiesUrl($serviceSlug);
    $html .= '<a href="' . $url . '" ';
    $html .= 'class="inline-block bg-gray-100 text-gray-700 hover:bg-gray-200 px-3 py-1 rounded-full text-sm font-medium transition-colors">';
    $html .= '+ More Cities';
    $html .= '</a>';
    
    $html .= '</div>';
    $html .= '</div>';
    
    return $html;
}
?>