<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/config.php';

// Get service slug from URL
$slug = isset($_GET['slug']) ? trim($_GET['slug']) : '';

// Function to get service image
function get_service_image($service)
{
    if (!empty($service['image']))
        return $service['image'];
    return '/wp-content/uploads/2022/08/chimney-sweep.jpg';
}

// If no slug provided, redirect to services list
if (empty($slug)) {
    header('Location: /our-services/');
    exit;
}

// Get service from database
$conn = get_db_connection();
$stmt = $conn->prepare("SELECT s.*, c.name as category_name 
    FROM services s 
    LEFT JOIN service_categories c ON s.category_id = c.id 
    WHERE s.slug = ? OR s.url = ? 
    LIMIT 1");
$stmt->bind_param("ss", $slug, $slug);
$stmt->execute();
$result = $stmt->get_result();
$service = $result->fetch_assoc();
$stmt->close();

if (!$service) {
    http_response_code(404);
    include BASE_PATH . '/include/header.php';
    ?>
    <main id="content">
        <section class="subpage-hero">
            <div class="container">
                <h1>Service <span>Not Found</span></h1>
                <p>The service you're looking for doesn't exist or has been removed.</p>
                <div class="mt-4">
                    <a href="/our-services/" class="btn-pro">View All Services</a>
                </div>
            </div>
        </section>
    </main>
    <?php
    include BASE_PATH . '/include/footer.php';
    exit;
}

// Get related services
$related_services = [];
$category_id = $service['category_id'];
if ($category_id) {
    $related_stmt = $conn->prepare("SELECT s.* FROM services s WHERE s.category_id = ? AND s.id != ? LIMIT 5");
    $related_stmt->bind_param("ii", $category_id, $service['id']);
    $related_stmt->execute();
    $related_result = $related_stmt->get_result();
    while ($row = $related_result->fetch_assoc()) {
        $related_services[] = $row;
    }
    $related_stmt->close();
}

// Set page title
$page_title = htmlspecialchars($service['name']) . ' - ' . get_website_name();
include BASE_PATH . '/include/header.php';
?>

<main id="content">
    <!-- Hero Section -->
    <section class="subpage-hero">
        <div class="container">
            <?php if (!empty($service['category_name'])): ?>
                <span
                    class="badge bg-primary px-3 py-2 rounded-pill mb-3 text-uppercase fw-bold letter-spacing-1"><?php echo htmlspecialchars($service['category_name']); ?></span>
            <?php endif; ?>
            <h1><?php echo htmlspecialchars($service['name']); ?></h1>
            <?php if (!empty($service['description'])): ?>
                <p><?php echo htmlspecialchars($service['description']); ?></p>
            <?php endif; ?>
        </div>
    </section>

    <!-- Breadcrumbs -->
    <div class="breadcrumb-container">
        <div class="container">
            <a href="/"><i class="fas fa-home"></i> Home</a> / <a href="/our-services/">Our Services</a> /
            <span><?php echo htmlspecialchars($service['name']); ?></span>
        </div>
    </div>

    <!-- Service Content Section -->
    <section class="section-padding bg-light">
        <div class="container">
            <div class="row g-5">
                <!-- Main Content -->
                <div class="col-lg-8">
                    <div class="pro-card p-0 overflow-hidden mb-5">
                        <img src="<?php echo get_service_image($service); ?>"
                            alt="<?php echo htmlspecialchars($service['name']); ?>" class="w-100 h-auto shadow-sm">
                        <div class="p-5">
                            <div class="service-content">
                                <?php if (!empty($service['content'])): ?>
                                    <?php echo $service['content']; ?>
                                <?php else: ?>
                                    <p class="lead">Professional chimney service tailored to your needs.</p>
                                    <p>Our team of experts is dedicated to providing the highest quality
                                        <?php echo htmlspecialchars($service['name']); ?> in the region. We use
                                        state-of-the-art equipment and proven techniques to ensure your chimney and
                                        fireplace are safe, efficient, and clean.</p>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Sidebar -->
                <div class="col-lg-4">
                    <div class="sticky-top" style="top: 120px;">
                        <!-- Booking Card -->
                        <div class="pro-card bg-dark text-white mb-4">
                            <h3 class="text-white mb-4">Book This Service</h3>
                            <p class="opacity-75 mb-4">Ready to schedule your
                                <?php echo htmlspecialchars($service['name']); ?>? Our team is standing by.</p>
                            <div class="d-grid gap-3">
                                <a href="tel:<?php echo get_phone_number_tel(); ?>" class="btn-pro text-center">
                                    <i class="fas fa-phone-alt me-2"></i> <?php echo get_phone_number(); ?>
                                </a>
                                <a href="/contact-us/" class="btn btn-outline-light rounded-3 py-3 fw-bold">
                                    <i class="fas fa-calendar-check me-2"></i> Online Booking
                                </a>
                            </div>
                        </div>

                        <!-- Related Services -->
                        <?php if (!empty($related_services)): ?>
                            <div class="pro-card">
                                <h4 class="mb-4">Related Services</h4>
                                <div class="related-list">
                                    <?php foreach ($related_services as $related): ?>
                                        <a href="/service.php?slug=<?php echo urlencode($related['slug']); ?>"
                                            class="d-flex align-items-center gap-3 text-decoration-none text-dark mb-3 group">
                                            <div class="flex-shrink-0" style="width: 60px; height: 60px;">
                                                <img src="<?php echo get_service_image($related); ?>"
                                                    alt="<?php echo htmlspecialchars($related['name']); ?>"
                                                    class="w-100 h-100 object-fit-cover rounded-3">
                                            </div>
                                            <div>
                                                <h6 class="mb-0 fw-bold group-hover-primary transition-all">
                                                    <?php echo htmlspecialchars($related['name']); ?></h6>
                                            </div>
                                        </a>
                                    <?php endforeach; ?>
                                </div>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </section>
</main>

<style>
    .service-content h2 {
        font-size: 2rem;
        margin-top: 2.5rem;
        margin-bottom: 1.5rem;
        color: var(--secondary-color);
    }

    .service-content p {
        font-size: 1.1rem;
        line-height: 1.8;
        color: var(--text-main);
        margin-bottom: 1.5rem;
    }

    .service-content ul {
        margin-bottom: 1.5rem;
    }

    .service-content li {
        margin-bottom: 0.75rem;
        font-size: 1.05rem;
    }

    .group:hover .group-hover-primary {
        color: var(--primary-color) !important;
    }

    .letter-spacing-1 {
        letter-spacing: 1px;
    }
</style>

<?php include BASE_PATH . '/include/footer.php'; ?>