<?php
/**
 * Template Name: Services Page
 *
 * Services page template that displays service cards, pricing, and CTA sections.
 *
 * @package Paws_And_Care
 * @since 0.1.2
 */

get_header();

// Get page-specific customizer settings
$show_page_content = get_theme_mod('paws_and_care_services_page_show_content', false);
$services_title = get_theme_mod('paws_and_care_services_page_title', __('What We Offer', 'paws-and-care'));
$services_subtitle = get_theme_mod('paws_and_care_services_page_subtitle', __('We offer a comprehensive range of services to keep your pets happy and healthy.', 'paws-and-care'));
$show_features = get_theme_mod('paws_and_care_services_page_show_features', true);
$show_pricing = get_theme_mod('paws_and_care_services_page_show_pricing', true);
$show_cta = get_theme_mod('paws_and_care_services_page_show_cta', true);

// Get services content from front-page settings
$services_content = get_theme_mod('paws_and_care_services_content', paws_and_care_get_services_default());
if (is_string($services_content)) {
    $services_content = json_decode($services_content, true);
}
?>

<?php
// Page Header (auto-generates title, subtitle, breadcrumbs)
get_template_part('template-parts/page-header');
?>

<?php
// Page Content from Editor (if enabled)
if ($show_page_content && have_posts()):
    while (have_posts()):
        the_post();
        if (get_the_content()):
            ?>
            <section class="py-5">
                <div class="container">
                    <div class="row justify-content-center">
                        <div class="col-lg-10">
                            <div class="page-content">
                                <?php the_content(); ?>
                            </div>
                        </div>
                    </div>
                </div>
            </section>
            <?php
        endif;
    endwhile;
endif;
?>

<!-- Services Section -->
<section id="primary-content" class="py-5 services-section">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-lg-8 text-center">
                <h2 class="section-title"><?php echo esc_html($services_title); ?></h2>
                <p class="lead mb-5"><?php echo esc_html($services_subtitle); ?></p>
            </div>
        </div>

        <?php if (!empty($services_content) && is_array($services_content)): ?>
            <div class="row g-4">
                <?php
                foreach ($services_content as $service):
                    $service_title = !empty($service['title']) ? $service['title'] : '';
                    $service_text = !empty($service['text']) ? $service['text'] : '';
                    $service_text2 = !empty($service['text2']) ? $service['text2'] : __('Learn More', 'paws-and-care');
                    $service_features = !empty($service['designation']) ? $service['designation'] : '';
                    $service_link = !empty($service['link']) ? $service['link'] : '#';
                    $service_icon = !empty($service['icon_value']) ? $service['icon_value'] : 'fa-star';
                    $open_new_tab = !empty($service['open_new_tab']) && $service['open_new_tab'] === 'yes';

                    // Always add fa-solid prefix
                    $icon_class = str_replace(array('fa-solid ', 'fa-brands ', 'fa-regular ', 'fa '), '', $service_icon);
                    $icon_class = 'fa-solid ' . $icon_class;

                    // Parse features into array (split by newlines)
                    $features_array = array();
                    if (!empty($service_features)) {
                        $features_array = preg_split('/\r\n|\r|\n/', $service_features);
                        $features_array = array_filter(array_map('trim', $features_array));
                    }
                    ?>
                    <div class="col-md-6 col-lg-4">
                        <div class="service-card card h-100">
                            <div class="card-body text-center p-4">
                                <div class="service-icon">
                                    <i class="<?php echo esc_attr($icon_class); ?>"></i>
                                </div>
                                <h4><?php echo esc_html($service_title); ?></h4>
                                <p><?php echo esc_html($service_text); ?></p>

                                <?php if ($show_features && !empty($features_array)): ?>
                                    <ul class="list-unstyled text-start mb-4">
                                        <?php foreach ($features_array as $feature): ?>
                                            <li><i class="fa-solid fa-check text-primary me-2"></i><?php echo esc_html($feature); ?>
                                            </li>
                                        <?php endforeach; ?>
                                    </ul>
                                <?php endif; ?>

                                <a href="<?php echo esc_url($service_link); ?>" class="btn btn-outline-primary" <?php echo $open_new_tab ? ' target="_blank" rel="noopener noreferrer"' : ''; ?>>
                                    <?php echo esc_html($service_text2); ?>
                                </a>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</section>

<?php
// Pricing Section (from front-page settings)
if ($show_pricing):
    get_template_part('template-parts/home/section', 'pricing');
endif;
?>

<?php
// CTA Section (from front-page settings)
if ($show_cta):
    get_template_part('template-parts/home/section', 'cta');
endif;
?>

<?php get_footer(); ?>