<?php
/**
 * Theme Setup Class
 *
 * @package Zentorone
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Zentorone_Setup {

	/**
	 * Instantiate the class
	 */
	public function __construct() {
		add_action( 'after_setup_theme', array( $this, 'setup' ) );
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
        add_action( 'widgets_init', array( $this, 'widgets_init' ) );
        add_filter( 'nav_menu_css_class', array( $this, 'nav_menu_classes' ), 10, 2 );
        add_filter( 'nav_menu_link_attributes', array( $this, 'nav_menu_link_attributes' ), 10, 4 );
	}

	/**
	 * Sets up theme defaults and registers support for various WordPress features.
	 */
	public function setup() {
		// Make theme available for translation.
		load_theme_textdomain( 'zentorone', get_template_directory() . '/languages' );

		// Add default posts and comments RSS feed links to head.
		add_theme_support( 'automatic-feed-links' );

		// Let WordPress manage the document title.
		add_theme_support( 'title-tag' );

		// Enable support for Post Thumbnails on posts and pages.
		add_theme_support( 'post-thumbnails' );

		// This theme uses wp_nav_menu() in one location.
		register_nav_menus(
			array(
				'menu-1' => esc_html__( 'Primary', 'zentorone' ),
			)
		);

		// Switch default core markup for search form, comment form, and comments to output valid HTML5.
		add_theme_support(
			'html5',
			array(
				'search-form',
				'comment-form',
				'comment-list',
				'gallery',
				'caption',
				'style',
				'script',
			)
		);

		// Set up the WordPress core custom background feature.
		add_theme_support(
			'custom-background',
			apply_filters(
				'zentorone_custom_background_args',
				array(
					'default-color' => 'ffffff',
					'default-image' => '',
				)
			)
		);

		// Add theme support for selective refresh for widgets.
		add_theme_support( 'customize-selective-refresh-widgets' );

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

    /**
	 * Register widget area.
	 */
	public function widgets_init() {
		register_sidebar(
			array(
				'name'          => esc_html__( 'Sidebar', 'zentorone' ),
				'id'            => 'sidebar-1',
				'description'   => esc_html__( 'Add widgets here.', 'zentorone' ),
				'before_widget' => '<section id="%1$s" class="widget %2$s">',
				'after_widget'  => '</section>',
				'before_title'  => '<h2 class="widget-title fs-5">',
				'after_title'   => '</h2>',
			)
		);

        register_sidebar(
			array(
				'name'          => esc_html__( 'Shop Sidebar', 'zentorone' ),
				'id'            => 'sidebar-shop',
				'description'   => esc_html__( 'Add widgets here for shop pages.', 'zentorone' ),
				'before_widget' => '<section id="%1$s" class="widget %2$s">',
				'after_widget'  => '</section>',
				'before_title'  => '<h2 class="widget-title fs-5">',
				'after_title'   => '</h2>',
			)
		);
        
        for ( $i = 1; $i <= 4; $i++ ) {
            register_sidebar(
                array(
                    'name'          => sprintf( esc_html__( 'Footer %d', 'zentorone' ), $i ),
                    'id'            => 'footer-' . $i,
                    'description'   => esc_html__( 'Add widgets here.', 'zentorone' ),
                    'before_widget' => '<div id="%1$s" class="widget %2$s mb-4">',
                    'after_widget'  => '</div>',
                    'before_title'  => '<h4 class="widget-title fs-5">',
                    'after_title'   => '</h4>',
                )
            );
        }
	}

	/**
	 * Enqueue scripts and styles.
     * Note: Core performance logic should be handled in class-zentorone-performance.php
     * This method handles the basic structure.
	 */
	public function enqueue_scripts() {
        // Enqueue main stylesheet (style.css is minimal, main styles are in assets/css/main.css)
        // We will enqueue main.css in Performance class to handle versioning/loading strategy, 
        // OR we can do it here. Let's do the basics here and let Performance handle the optimization.
        
		wp_enqueue_style( 'zentorone-style', get_stylesheet_uri(), array(), ZENTORONE_VERSION );
        
        // Threaded comments
		if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
			wp_enqueue_script( 'comment-reply' );
		}
	}

    /**
     * Filters the CSS class for a menu item 'li' element.
     */
    public function nav_menu_classes( $classes, $item ) {
        $classes[] = 'nav-item';
        return $classes;
    }

    /**
     * Filters the attributes of a menu item 'a' element.
     */
    public function nav_menu_link_attributes( $atts, $item, $args, $depth ) {
        $atts['class'] = 'nav-link';
        return $atts;
    }
}
