<?php
/**
 * Nutrawell functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package Nutrawell
 * @since 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Enqueue theme styles and scripts.
 */
function nutrawell_scripts() {
	// Required theme stylesheet (header only)
	wp_enqueue_style(
		'nutrawell-style',
		get_stylesheet_uri(),
		array(),
		wp_get_theme()->get( 'Version')
	);

	// Custom CSS from assets folder (using style.css from original)
	wp_enqueue_style(
		'nutrawell-custom-style',
		get_template_directory_uri() . '/assets/css/custom-style.css',
		array( 'nutrawell-style' ),
		wp_get_theme()->get( 'Version')
	);

		// Swiper CSS
		wp_enqueue_style(
			'swiper',
			get_theme_file_uri( 'assets/css/vendor/swiper-bundle.min.css' ),
			array(),
			wp_get_theme()->get( 'Version')
		);
	// Swiper JS
	wp_enqueue_script(
		'swiper',
		get_theme_file_uri( 'assets/js/vendor/swiper-bundle.min.js' ),
		array(),
		wp_get_theme()->get( 'Version'),
		true
	);

	// Theme JS
	wp_enqueue_script(
		'nutrawell-script',
		get_template_directory_uri() . '/assets/js/script.js',
		array( 'swiper' ),
		wp_get_theme()->get( 'Version'),
		true
	);

	// Enqueue comment reply script
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply');
	}
}
add_action( 'wp_enqueue_scripts', 'nutrawell_scripts');

/**
 * Sets up theme defaults and registers support for various WordPress features.
 */
function nutrawell_setup() {
	// Add support for automatic feed links.
	add_theme_support( 'automatic-feed-links' );

	// Add support for block styles.
	add_theme_support( 'wp-block-styles' );

	// Add support for responsive embedded content.
	add_theme_support( 'responsive-embeds' );

	// Add support for custom logo.
	add_theme_support(
		'custom-logo',
		array(
			'height'      => 60,
			'width'       => 200,
			'flex-height' => true,
			'flex-width'  => true,
		)
	);

	// Add support for post thumbnails.
	add_theme_support( 'post-thumbnails' );

	// Add support for title tag.
	add_theme_support( 'title-tag' );

	// Add support for HTML5 semantic markup.
	add_theme_support(
		'html5',
		array(
			'search-form',
			'comment-form',
			'comment-list',
			'gallery',
			'caption',
			'style',
			'script',
		)
	);

	// Add support for editor styles. Fonts come from theme.json fontFace, which the
	// editor loads automatically, so only the component stylesheet is added here.
	add_theme_support( 'editor-styles' );
	add_editor_style( 'assets/css/custom-style.css' );

	// Register navigation menu locations. Block themes use wp_navigation posts, but
	// these are kept for fallback and for translation when distributed outside .org.
	register_nav_menus(
		array(
			'primary' => esc_html__( 'Primary Navigation', 'nutrawell' ),
			'footer'  => esc_html__( 'Footer Navigation', 'nutrawell' ),
		)
	);

	// Load theme textdomain. Redundant for .org-hosted themes (auto since WP 4.6);
	// kept so translations bundled with non-.org distributions still load.
	load_theme_textdomain( 'nutrawell', get_template_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'nutrawell_setup');

/**
 * Register the Nutrawell block pattern category.
 *
 * Individual patterns in /patterns/*.php are auto-registered by WordPress core
 * (since WP 6.0) from their file headers, so they are not registered manually here.
 */
function nutrawell_register_pattern_category() {
	register_block_pattern_category(
		'nutrawell',
		array( 'label' => esc_html__( 'Nutrawell', 'nutrawell' ) )
	);
}
add_action( 'init', 'nutrawell_register_pattern_category' );

/**
 * Register custom block styles.
 *
 * Styling lives in assets/css/custom-style.css (.is-style-nutrawell-*), which is
 * loaded on both the front end and the editor, so the previews match. Names are
 * prefixed to avoid clashing with core's built-in block styles.
 */
function nutrawell_register_block_styles() {
	register_block_style(
		'core/group',
		array(
			'name'  => 'nutrawell-card',
			'label' => esc_html__( 'Card', 'nutrawell' ),
		)
	);

	register_block_style(
		'core/quote',
		array(
			'name'  => 'nutrawell-accent',
			'label' => esc_html__( 'Accent', 'nutrawell' ),
		)
	);
}
add_action( 'init', 'nutrawell_register_block_styles' );
