<?php
/**
 * Template part for displaying the services section on the homepage
 *
 * @package Paws_And_Care
 * @since 1.0.0
 */

// Check if section is enabled
$services_enable = get_theme_mod('paws_and_care_services_enable', true);
if (!$services_enable) {
    return;
}

// Get customizer values (defaults are in inc/customizer/defaults.php)
$services_title = get_theme_mod('paws_and_care_services_title', __('Our Services', 'paws-and-care'));
$services_subtitle = get_theme_mod('paws_and_care_services_subtitle', __('We offer a comprehensive range of services to keep your pets happy and healthy.', 'paws-and-care'));
$services_content = get_theme_mod('paws_and_care_services_content', paws_and_care_get_services_default());

// Get display settings
$show_features = get_theme_mod('paws_and_care_services_show_features', true);
$show_button = get_theme_mod('paws_and_care_services_show_button', true);

// Decode services if it's a string
if (is_string($services_content)) {
    $services_content = json_decode($services_content, true);
}
?>

<section class="py-5 bg-light services-section">
    <?php paws_and_care_section_shortcut('paws_and_care_services', __('Edit Services', 'paws-and-care')); ?>
    <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; ?>

                                <?php if (!empty($show_button)): ?>
                                    <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>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</section>