<?php
class Whizzie {

    public function __construct() {
        $this->init();
    }

    public function init() {
        $this->photographer_magazine_setup_widgets();
        $this->photographer_magazine_setup_theme_mods();
    }

    public function photographer_magazine_setup_widgets() {
        // Required includes for media functions
        require_once ABSPATH . 'wp-admin/includes/media.php';
        require_once ABSPATH . 'wp-admin/includes/file.php';
        require_once ABSPATH . 'wp-admin/includes/image.php';

        // Demo post titles
        $post_titles = array(
            "FASHION PHOTOGRAPHY",
            "WEDDING PHOTOGRAPHY",
            "NATURE PHOTOGRAPHY"
        );

        for ($i = 1; $i <= 3; $i++) {
            $title = $post_titles[$i - 1];
            $content = "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.";

            $existing_post = new WP_Query(array(
                'post_type'      => 'post',
                'title'          => $title,
                'post_status'    => 'any',
                'posts_per_page' => 1,
                'fields'         => 'ids',
            ));

            if ($existing_post->have_posts()) {
                wp_reset_postdata();
                continue; // Skip if post already exists
            }

            // Create the post
            $post_id = wp_insert_post(array(
                'post_title'   => wp_strip_all_tags($title),
                'post_content' => $content,
                'post_status'  => 'publish',
                'post_type'    => 'post',
            ));

            if (is_wp_error($post_id) || !$post_id) {
                continue;
            }

            // Set featured image using media_sideload_image
            $image_url = get_template_directory_uri() . '/images/post/image' . $i . '.png';
            $attach_id = media_sideload_image($image_url, $post_id, null, 'id');

            if (!is_wp_error($attach_id)) {
                set_post_thumbnail($post_id, $attach_id);
            }
        }
    }

    public function photographer_magazine_setup_theme_mods() {
        $options = get_theme_mod('theme_options', []);
        $options['header_top_button'] = 'Appointment';
        $options['header_top_buttonlink'] = '#';
        set_theme_mod('theme_options', $options);

        /* Create Menu */
            $photographer_magazine_menuname  = 'Primary Menu';
            $photographer_magazine_location  = 'primary-menu';

            $photographer_magazine_menu = wp_get_nav_menu_object( $photographer_magazine_menuname );

            if ( ! $photographer_magazine_menu ) {
            $photographer_magazine_menu_id = wp_create_nav_menu( $photographer_magazine_menuname );

           // Home Page 
			wp_update_nav_menu_item( $photographer_magazine_menu_id, 0, array(
				'menu-item-title'     => __( 'Home', 'photographer-magazine' ),
				'menu-item-url'       => home_url( '/' ),
				'menu-item-type'      => 'custom',
				'menu-item-status'    => 'publish',
			) );

			// About Us Page 
			$photographer_magazine_about_id = wp_insert_post( array(
			'post_type'   => 'page',
			'post_content' => 'We are committed to providing reliable, professional, and result-oriented solutions tailored to meet individual needs. Our goal is to empower people with the right guidance, knowledge, and support to help them make informed decisions for a better future. <br><br> Our mission is to deliver high-quality services with honesty, transparency, and dedication. We focus on understanding client requirements and offering practical solutions that create long-term value. <br><br> With a client-centric approach, experienced professionals, and a commitment to excellence, we ensure every individual receives the attention and guidance they deserve. We believe in building trust through quality service and consistent results.',
			'post_title'  => 'About Us',
			'post_status' => 'publish',
			) );
 
			if ( $photographer_magazine_about_id ) {
			wp_update_nav_menu_item( $photographer_magazine_menu_id, 0, array(
			'menu-item-title'     => 'About Us',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $photographer_magazine_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

			// Pages Page 
			$photographer_magazine_about_id = wp_insert_post( array(
			'post_type'   => 'page',
			'post_content' => 'We are committed to providing reliable, professional, and result-oriented solutions tailored to meet individual needs. Our goal is to empower people with the right guidance, knowledge, and support to help them make informed decisions for a better future. <br><br> Our mission is to deliver high-quality services with honesty, transparency, and dedication. We focus on understanding client requirements and offering practical solutions that create long-term value. <br><br> With a client-centric approach, experienced professionals, and a commitment to excellence, we ensure every individual receives the attention and guidance they deserve. We believe in building trust through quality service and consistent results.',
			'post_title'  => 'Pages',
			'post_status' => 'publish',
			) );

			if ( $photographer_magazine_about_id ) {
			wp_update_nav_menu_item( $photographer_magazine_menu_id, 0, array(
			'menu-item-title'     => 'Pages',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $photographer_magazine_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

			// Services Page 
			$photographer_magazine_about_id = wp_insert_post( array(
			'post_type'   => 'page',
			'post_content' => 'We are committed to providing reliable, professional, and result-oriented solutions tailored to meet individual needs. Our goal is to empower people with the right guidance, knowledge, and support to help them make informed decisions for a better future. <br><br> Our mission is to deliver high-quality services with honesty, transparency, and dedication. We focus on understanding client requirements and offering practical solutions that create long-term value. <br><br> With a client-centric approach, experienced professionals, and a commitment to excellence, we ensure every individual receives the attention and guidance they deserve. We believe in building trust through quality service and consistent results.',
			'post_title'  => 'Services',
			'post_status' => 'publish',
			) );

			if ( $photographer_magazine_about_id ) {
			wp_update_nav_menu_item( $photographer_magazine_menu_id, 0, array(
			'menu-item-title'     => 'Services',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $photographer_magazine_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

			// Contact Us Page 
			$photographer_magazine_about_id = wp_insert_post( array(
			'post_type'   => 'page',
			'post_content' => 'We are committed to providing reliable, professional, and result-oriented solutions tailored to meet individual needs. Our goal is to empower people with the right guidance, knowledge, and support to help them make informed decisions for a better future. <br><br> Our mission is to deliver high-quality services with honesty, transparency, and dedication. We focus on understanding client requirements and offering practical solutions that create long-term value. <br><br> With a client-centric approach, experienced professionals, and a commitment to excellence, we ensure every individual receives the attention and guidance they deserve. We believe in building trust through quality service and consistent results.',
			'post_title'  => 'Contact Us',
			'post_status' => 'publish',
			) );

			if ( $photographer_magazine_about_id ) {
			wp_update_nav_menu_item( $photographer_magazine_menu_id, 0, array(
			'menu-item-title'     => 'Contact Us',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $photographer_magazine_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

            // Create Blog Page
                $photographer_magazine_blog_page_title = 'Blog';

                $photographer_magazine_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($photographer_magazine_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$photographer_magazine_blog_page_query->hae_posts()) {
                    $photographer_magazine_blog_page = array(
                        'post_type'   => 'page',
                        'post_title'  => $photographer_magazine_blog_page_title,
                        'post_status' => 'publish',
                        'post_author' => 1,
                    );
                    $photographer_magazine_blog_page_id = wp_insert_post($photographer_magazine_blog_page);
                    update_option('page_for_posts', $photographer_magazine_blog_page_id);

                    wp_update_nav_menu_item($photographer_magazine_menu_id, 0, array(
                        'menu-item-title'      => __('Blog', 'photographer-magazine'),
                        'menu-item-url'        => get_permalink($photographer_magazine_blog_page_id),
                        'menu-item-status'     => 'publish',
                        'menu-item-object-id'  => $photographer_magazine_blog_page_id,
                        'menu-item-object'     => 'page',
                        'menu-item-type'       => 'post_type',
                    ));
                }
                
                // Create Blog Page
                $photographer_magazine_blog_page_title = 'Blog';

                $photographer_magazine_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($photographer_magazine_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$photographer_magazine_blog_page_query->hae_posts()) {
                    $photographer_magazine_blog_page = array(
                        'post_type'   => 'page',
                        'post_title'  => $photographer_magazine_blog_page_title,
                        'post_status' => 'publish',
                        'post_author' => 1,
                    );
                    $photographer_magazine_blog_page_id = wp_insert_post($photographer_magazine_blog_page);
                    update_option('page_for_posts', $photographer_magazine_blog_page_id);

                    wp_update_nav_menu_item($photographer_magazine_menu_id, 0, array(
                        'menu-item-title'      => __('Blog', 'photographer-magazine'),
                        'menu-item-url'        => get_permalink($photographer_magazine_blog_page_id),
                        'menu-item-status'     => 'publish',
                        'menu-item-object-id'  => $photographer_magazine_blog_page_id,
                        'menu-item-object'     => 'page',
                        'menu-item-type'       => 'post_type',
                    ));
                }
                
			/* Assign Menu Location */
			$photographer_magazine_locations = get_theme_mod( 'nav_menu_locations', array() );
			$photographer_magazine_locations[ $photographer_magazine_location ] = $photographer_magazine_menu_id;
			set_theme_mod( 'nav_menu_locations', $photographer_magazine_locations );
		}
    }
}
