<?php
/**
 * Theme functions and definitions.
 * This child theme was generated by TemplateMela.
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 */

/* ================================================== */
/* 1. Enqueue Styles and Scripts                    */
/* ================================================== */
/**
 * Enqueue parent and child theme styles and other assets.
 */
function avanam_child_enqueue_theme_assets() {
    // Enqueue the parent theme's stylesheet.
    // We are now definitively using 'avanam-style' as the handle,
    // based on the parent theme's style.css 'Text Domain'.
    // Use AVANAM_VERSION if it's defined in the parent's functions.php, otherwise use parent's theme version.
    wp_enqueue_style( 'avanam-style', get_template_directory_uri() . '/style.css', array(), defined( 'AVANAM_VERSION' ) ? AVANAM_VERSION : wp_get_theme()->parent()->get('Version') );

    // Enqueue the child theme's stylesheet.
    // It *depends* on the parent theme's style ('avanam-style'), ensuring proper loading order.
    wp_enqueue_style( 'avanam-child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array( 'avanam-style' ), // <-- ***THIS IS THE KEY CORRECTION***
        wp_get_theme()->get('Version')
    );
    
    // Enqueue Font Awesome for icons used in the custom homepage template.
    wp_enqueue_style( 'font-awesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css', array(), '5.15.4' );

    // If you plan to add custom JavaScript for carousels (e.g., main.js)
    // wp_enqueue_script( 'avanam-child-custom-js', get_stylesheet_directory_uri() . '/js/main.js', array('jquery'), wp_get_theme()->get('Version'), true );
}
add_action( 'wp_enqueue_scripts', 'avanam_child_enqueue_theme_assets' );


/* ================================================== */
/* 2. WooCommerce Catalog Mode for Guest Users      */
/* ================================================== */
// ... (rest of your WooCommerce catalog mode functions, as provided previously) ...
function custom_catalog_mode_for_guests() {
    if ( ! is_user_logged_in() ) {
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        add_action( 'template_redirect', 'custom_redirect_guests_from_cart_checkout' );
    }
}
add_action( 'init', 'custom_catalog_mode_for_guests' );

function custom_redirect_guests_from_cart_checkout() {
    if ( is_cart() || is_checkout() ) {
        wp_redirect( home_url() );
        exit();
    }
}

// add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_guest_add_to_cart_text' );
// add_filter( 'woocommerce_loop_add_to_cart_link', 'custom_guest_add_to_cart_link' );
function custom_guest_add_to_cart_text( $text ) {
    return __( 'Login to Purchase', 'your-text-domain' );
}
function custom_guest_add_to_cart_link( $link ) {
    $link = '<a href="' . get_permalink() . '" class="button">' . __( 'Login to Purchase', 'your-text-domain' ) . '</a>';
    return $link;
}


/* ================================================== */
/* 3. Change Theme Options Defaults (Based on your original code) */
/* ================================================== */
// ... (rest of your theme options defaults function, as provided previously) ...
function craftme_child_change_option_defaults( $defaults ) {
    $new_defaults = '{
        "post_content_style": "unboxed"
    }';
    $new_defaults = json_decode( $new_defaults, true );
    return wp_parse_args( $new_defaults, $defaults );
}
// add_filter( 'base_theme_options_defaults', 'craftme_child_change_option_defaults', 20 );