<?php
/**
 * Template Helper Functions
 *
 * @package HomeProz
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Get the page-specific body class
 *
 * Returns the appropriate class name for the current page/template
 * Used for page-scoped CSS and JavaScript
 *
 * @return string The page class name
 */
function homeproz_get_page_class() {
    if (is_front_page()) {
        return 'Home_Page';
    }

    if (is_home()) {
        return 'Blog_Archive';
    }

    if (is_post_type_archive('property')) {
        return 'Properties_Archive';
    }

    if (is_singular('property')) {
        return 'Single_Property';
    }

    if (is_singular('post')) {
        return 'Single_Post';
    }

    if (is_page_template('page-about.php') || is_page('about')) {
        return 'About_Page';
    }

    if (is_page_template('page-contact.php') || is_page('contact')) {
        return 'Contact_Page';
    }

    if (is_search()) {
        return 'Search_Page';
    }

    if (is_404()) {
        return 'Error_404';
    }

    if (is_archive()) {
        return 'Archive_Page';
    }

    if (is_page()) {
        return 'Default_Page';
    }

    return 'Default_Page';
}

/**
 * Add page class to body classes
 */
function homeproz_body_classes($classes) {
    $classes[] = homeproz_get_page_class();

    // Add class if no sidebar
    if (!is_active_sidebar('sidebar-blog') || is_front_page() || is_page()) {
        $classes[] = 'no-sidebar';
    }

    return $classes;
}
add_filter('body_class', 'homeproz_body_classes');

/**
 * Custom excerpt length
 */
function homeproz_excerpt_length($length) {
    return 25;
}
add_filter('excerpt_length', 'homeproz_excerpt_length');

/**
 * Custom excerpt more
 */
function homeproz_excerpt_more($more) {
    return '&hellip;';
}
add_filter('excerpt_more', 'homeproz_excerpt_more');

/**
 * Get property status badge class
 *
 * @param string $status The property status
 * @return string CSS class for the badge
 */
function homeproz_get_status_class($status) {
    $status = strtolower($status);

    switch ($status) {
        case 'active':
            return 'badge-success';
        case 'pending':
            return 'badge-warning';
        case 'sold':
            return 'badge-muted';
        default:
            return 'badge-default';
    }
}

/**
 * Format price for display
 *
 * @param int|float $price The price value
 * @return string Formatted price
 */
function homeproz_format_price($price) {
    if (empty($price) || $price <= 0) {
        return 'Contact for Price';
    }

    return '$' . number_format($price);
}

/**
 * Fallback menu when no menu is assigned
 */
function homeproz_fallback_menu() {
    echo '<ul class="nav-menu">';
    echo '<li class="menu-item"><a href="' . esc_url(home_url('/')) . '">Home</a></li>';
    echo '<li class="menu-item"><a href="' . esc_url(home_url('/properties/')) . '">Properties</a></li>';
    echo '<li class="menu-item"><a href="' . esc_url(home_url('/about/')) . '">About</a></li>';
    echo '<li class="menu-item"><a href="' . esc_url(home_url('/contact/')) . '">Contact</a></li>';
    echo '</ul>';
}

/**
 * Fallback footer menu when no menu is assigned
 */
function homeproz_footer_fallback_menu() {
    echo '<ul class="footer-menu">';
    echo '<li class="menu-item"><a href="' . esc_url(home_url('/')) . '">Home</a></li>';
    echo '<li class="menu-item"><a href="' . esc_url(home_url('/properties/')) . '">Properties</a></li>';
    echo '<li class="menu-item"><a href="' . esc_url(home_url('/about/')) . '">About</a></li>';
    echo '<li class="menu-item"><a href="' . esc_url(home_url('/blog/')) . '">Blog</a></li>';
    echo '<li class="menu-item"><a href="' . esc_url(home_url('/contact/')) . '">Contact</a></li>';
    echo '</ul>';
}

/**
 * Get theme option from ACF or defaults
 *
 * @param string $key Option key
 * @param mixed $default Default value
 * @return mixed Option value
 */
function homeproz_get_option($key, $default = '') {
    // Use ACF helper if available
    if (function_exists('homeproz_get_acf_option')) {
        return homeproz_get_acf_option($key, $default);
    }

    // Fallback defaults
    $options = array(
        'phone' => '507-516-4870',
        'email' => 'info@homeprozrealestate.com',
        'address' => '111 E Clark St, Albert Lea, MN 56007',
        'facebook' => 'https://www.facebook.com/profile.php?id=61578834743321',
        'tiktok' => 'https://www.tiktok.com/@homeproz.real.est',
    );

    return isset($options[$key]) ? $options[$key] : $default;
}
