<?php
/**
 * City Page Template - 510 Notary Services
 * This file handles all individual city pages dynamically from database
 */

// Define ROOT_PATH for city pages (one level up from /areas/)
define('ROOT_PATH', dirname(__DIR__) . '/');

require_once ROOT_PATH . 'config/config.php';

// Get city slug from URL
$citySlug = basename($_SERVER['PHP_SELF'], '.php');

// Get city data from database
$stmt = $conn->prepare("SELECT * FROM service_areas WHERE city_slug = ? AND is_active = 1");
$stmt->bind_param("s", $citySlug);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    header('Location: ' . SITE_URL . 'areas.php');
    exit;
}

$cityRow = $result->fetch_assoc();

// TEMPORARY DEBUG
echo "<!-- DEBUG: City Slug = " . $citySlug . " -->";
echo "<!-- DEBUG: Rows found = " . $result->num_rows . " -->";
echo "<!-- DEBUG: Main Content Length = " . strlen($cityRow['main_content']) . " -->";
echo "<!-- DEBUG: First 100 chars = " . substr($cityRow['main_content'], 0, 100) . " -->";

// Parse neighborhoods and landmarks from comma-separated strings
$neighborhoods = !empty($cityRow['neighborhoods']) ? array_map('trim', explode(',', $cityRow['neighborhoods'])) : [];
$landmarks = !empty($cityRow['landmarks']) ? array_map('trim', explode(',', $cityRow['landmarks'])) : [];

// Build city array for template compatibility
$city = [
    'name' => $cityRow['city_name'],
    'county' => $cityRow['county'],
    'title' => $cityRow['meta_title'],
    'meta_desc' => $cityRow['meta_description'],
    'intro' => $cityRow['intro_text'],
    'content' => $cityRow['main_content'],
    'local_businesses' => $cityRow['local_businesses'],
    'meeting_spots' => $cityRow['meeting_spots'],
    'unique_features' => $cityRow['unique_features'],
    'neighborhoods' => $neighborhoods,
    'landmarks' => $landmarks
];

// Set page title and description from database
$pageTitle = $city['title'];
$pageDescription = $city['meta_desc'];

include ROOT_PATH . 'includes/header.php';
?>

    <!-- Page Header -->
    <section class="page-header">
        <div class="container">
            <h1>Mobile Notary in <?php echo e($city['name']); ?>, CA</h1>
            <p><?php echo e($city['county']); ?> | 510 & 341 Area Code</p>
            <div class="breadcrumb">
                <a href="<?php echo SITE_URL; ?>index.php">Home</a>
                <span>/</span>
                <a href="<?php echo SITE_URL; ?>areas.php">Service Areas</a>
                <span>/</span>
                <span><?php echo e($city['name']); ?></span>
            </div>
        </div>
    </section>

    <!-- City Content -->
    <section class="section">
        <div class="container">
            <div class="city-content">
                <div class="city-main">
                    <h2>Professional Notary Services in <?php echo e($city['name']); ?></h2>
                    
                    <p class="lead"><?php echo e($city['intro']); ?></p>
                    
                    <div class="city-main-content">
                        <?php 
                        // Output main content with proper paragraph formatting
                        $paragraphs = explode("\n\n", $city['content']);
                        foreach ($paragraphs as $paragraph) {
                            if (trim($paragraph)) {
                                echo '<p>' . e($paragraph) . '</p>';
                            }
                        }
                        ?>
                    </div>
                    
                    <?php if (!empty($city['local_businesses'])): ?>
                    <div class="local-info">
                        <h3>Meeting Locations & Local Spots</h3>
                        <p><?php echo e($city['local_businesses']); ?></p>
                    </div>
                    <?php endif; ?>
                    
                    <?php if (!empty($city['neighborhoods'])): ?>
                    <h3>Areas We Serve in <?php echo e($city['name']); ?></h3>
                    <div class="neighborhoods-list">
                        <?php foreach ($city['neighborhoods'] as $neighborhood): ?>
                        <span class="neighborhood-tag"><?php echo e($neighborhood); ?></span>
                        <?php endforeach; ?>
                    </div>
                    <?php endif; ?>
                    
                    <h3>Documents We Notarize in <?php echo e($city['name']); ?></h3>
                    <div class="documents-grid">
                        <div class="doc-item"><i class="fas fa-file-contract"></i> Power of Attorney</div>
                        <div class="doc-item"><i class="fas fa-home"></i> Real Estate Documents</div>
                        <div class="doc-item"><i class="fas fa-file-signature"></i> Loan Documents</div>
                        <div class="doc-item"><i class="fas fa-gavel"></i> Affidavits</div>
                        <div class="doc-item"><i class="fas fa-file-alt"></i> Trusts & Wills</div>
                        <div class="doc-item"><i class="fas fa-building"></i> Deeds</div>
                        <div class="doc-item"><i class="fas fa-briefcase"></i> Business Contracts</div>
                        <div class="doc-item"><i class="fas fa-medkit"></i> Medical Directives</div>
                    </div>
                    
                    <h3>Why Choose Our <?php echo e($city['name']); ?> Mobile Notary Service?</h3>
                    <ul class="benefits-list">
                        <li><i class="fas fa-check-circle"></i> <strong>We Come to You</strong> - No need to travel, we meet you at your <?php echo e($city['name']); ?> location</li>
                        <li><i class="fas fa-check-circle"></i> <strong>Same-Day Service</strong> - Often available for urgent <?php echo e($city['name']); ?> appointments</li>
                        <li><i class="fas fa-check-circle"></i> <strong>Flexible Hours</strong> - Available evenings and weekends</li>
                        <li><i class="fas fa-check-circle"></i> <strong>Professional Service</strong> - California commissioned and insured notary</li>
                        <li><i class="fas fa-check-circle"></i> <strong>Competitive Rates</strong> - Transparent pricing with no hidden fees</li>
                    </ul>
                </div>
                
                <div class="city-sidebar">
                    <div class="sidebar-cta">
                        <h3>Book Your <?php echo e($city['name']); ?> Appointment</h3>
                        <p>Ready for mobile notary service in <?php echo e($city['name']); ?>?</p>
                        <a href="tel:<?php echo phoneLink(setting('phone')); ?>" class="btn btn-accent btn-lg">
                            <i class="fas fa-phone"></i> <?php echo e(setting('phone')); ?>
                        </a>
                        <p class="cta-note">Call or Text Anytime</p>
                        <a href="<?php echo SITE_URL; ?>contact.php" class="btn btn-secondary">Request Online</a>
                    </div>
                    
                    <div class="sidebar-info">
                        <h4>Service Information</h4>
                        <ul>
                            <li><strong>Location:</strong> <?php echo e($city['name']); ?>, <?php echo e($city['county']); ?></li>
                            <li><strong>Area Code:</strong> 510 / 341</li>
                            <li><strong>Service:</strong> Mobile Notary</li>
                            <li><strong>Availability:</strong> 7 Days a Week</li>
                        </ul>
                    </div>
                    
                    <?php if (!empty($city['landmarks'])): ?>
                    <div class="sidebar-info">
                        <h4>Nearby Landmarks</h4>
                        <ul>
                            <?php foreach ($city['landmarks'] as $landmark): ?>
                            <li><?php echo e($landmark); ?></li>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </section>

    <!-- CTA Section -->
    <section class="cta-section">
        <div class="container">
            <h2>Need a Notary in <?php echo e($city['name']); ?>?</h2>
            <p>Contact us today for fast, professional mobile notary service</p>
            <div class="cta-buttons">
                <a href="<?php echo SITE_URL; ?>contact.php" class="btn btn-primary btn-lg">Book Appointment</a>
                <a href="tel:<?php echo phoneLink(setting('phone')); ?>" class="btn btn-secondary btn-lg">
                    <i class="fas fa-phone"></i> <?php echo e(setting('phone')); ?>
                </a>
            </div>
        </div>
    </section>

<?php include ROOT_PATH . 'includes/footer.php'; ?>
