<?php

class Whizzie {

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

	public function init()
	{
		
	}

	public static function auto_car_repair_setup_widgets(){

	wp_delete_post(1);

		// Create post category
		$auto_car_repair_parent_category = wp_insert_term(
			'Service Category', // the term
			'category', // the taxonomy
			array(
				'description'=> 'This is sample post category',
				'slug' => 'post_cat',
			)
		);

		$auto_car_repair_post_title = array(	'Diagonstics',
			'Engine Repair',
			'Oil/Filter',
			'Tires',
			'Transmition',
			'Batteries');

		for($auto_car_repair_i=1; $auto_car_repair_i<=count($auto_car_repair_post_title); $auto_car_repair_i++){

			$auto_car_repair_content = 'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using Content here, content here, making it look like readable English.';

			// Create post object
			$auto_car_repair_my_post = array(
					'post_title'    => wp_strip_all_tags( $auto_car_repair_post_title[$auto_car_repair_i - 1] ),
					'post_content'  => $auto_car_repair_content,
					'post_status'   => 'publish',
					'post_type'     => 'post',
					'post_category' => [$auto_car_repair_parent_category['term_id']]
			);
				// Insert the post into the database
			$auto_car_repair_etspost_id = wp_insert_post( $auto_car_repair_my_post );

			// Now replace meta w/ new updated value array
			$auto_car_repair_image_url = get_template_directory_uri().'/assets/images/blog/service-img.png';

			$auto_car_repair_image_name= 'service-img.png';
			$auto_car_repair_upload_dir       = wp_upload_dir();
			// Set upload folder
			$auto_car_repair_image_data       = file_get_contents($auto_car_repair_image_url);
			// Get image data
			$auto_car_repair_unique_file_name = wp_unique_filename( $auto_car_repair_upload_dir['path'], $auto_car_repair_image_name );
			// Generate unique name
			$auto_car_repair_filename= basename( $auto_car_repair_unique_file_name );
			// Create image file name
			// Check folder permission and define file location
			if( wp_mkdir_p( $auto_car_repair_upload_dir['path'] ) ) {
					$auto_car_repair_file = $auto_car_repair_upload_dir['path'] . '/' . $auto_car_repair_filename;
			} else {
					$auto_car_repair_file = $auto_car_repair_upload_dir['basedir'] . '/' . $auto_car_repair_filename;
			}
			// Create the image  file on the server
			file_put_contents( $auto_car_repair_file, $auto_car_repair_image_data );
			// Check image file type
			$auto_car_repair_wp_filetype = wp_check_filetype( $auto_car_repair_filename, null );
			// Set attachment data
			$auto_car_repair_attachment = array(
					'post_mime_type' => $auto_car_repair_wp_filetype['type'],
					'post_title'     => sanitize_file_name( $auto_car_repair_filename ),
					'post_content'   => '',
					'post_type'     => 'post',
					'post_status'    => 'inherit'
			);
			// Create the attachment
			$auto_car_repair_attach_id = wp_insert_attachment( $auto_car_repair_attachment, $auto_car_repair_file, $auto_car_repair_etspost_id );
			// Include image.php
			require_once(ABSPATH . 'wp-admin/includes/image.php');
			// Define attachment metadata
			$auto_car_repair_attach_data = wp_generate_attachment_metadata( $auto_car_repair_attach_id, $auto_car_repair_file );
			// Assign metadata to attachment
				wp_update_attachment_metadata( $auto_car_repair_attach_id, $auto_car_repair_attach_data );
			// And finally assign featured image to post
			set_post_thumbnail( $auto_car_repair_etspost_id, $auto_car_repair_attach_id );
		}

	/* Create Menu */
            $auto_car_repair_menuname  = 'Main Menus';
            $auto_car_repair_location  = 'main-menu';

            $auto_car_repair_menu = wp_get_nav_menu_object( $auto_car_repair_menuname );

            if ( ! $auto_car_repair_menu ) {
            $auto_car_repair_menu_id = wp_create_nav_menu( $auto_car_repair_menuname );

           // Home Page 
			wp_update_nav_menu_item( $auto_car_repair_menu_id, 0, array(
				'menu-item-title'     => __( 'Home', 'auto-car-repair' ),
				'menu-item-url'       => home_url( '/' ),
				'menu-item-type'      => 'custom',
				'menu-item-status'    => 'publish',
				'menu-item-position'  => 1,
			) );

			// About Page 
			$auto_car_repair_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 ( $auto_car_repair_about_id ) {
			wp_update_nav_menu_item( $auto_car_repair_menu_id, 0, array(
			'menu-item-title'     => 'About Us',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $auto_car_repair_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
            'menu-item-position'  => 2,
			) );
			}

			// Services Page 
			$auto_car_repair_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 ( $auto_car_repair_about_id ) {
			wp_update_nav_menu_item( $auto_car_repair_menu_id, 0, array(
			'menu-item-title'     => 'Services',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $auto_car_repair_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
            'menu-item-position'  => 3,
			) );
			}

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

                $auto_car_repair_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($auto_car_repair_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$auto_car_repair_blog_page_query->hae_posts()) {
                    $auto_car_repair_blog_page = array(
                        'post_type'   => 'page',
                        'post_title'  => $auto_car_repair_blog_page_title,
                        'post_status' => 'publish',
                        'post_author' => 1,
                    );
                    $auto_car_repair_blog_page_id = wp_insert_post($auto_car_repair_blog_page);
                    update_option('page_for_posts', $auto_car_repair_blog_page_id);

                    wp_update_na_menu_item($auto_car_repair_menu_id, 0, array(
                        'menu-item-title'      => __('Blog', 'auto-car-repair'),
                        'menu-item-url'        => get_permalink($auto_car_repair_blog_page_id),
                        'menu-item-status'     => 'publish',
                        'menu-item-object-id'  => $auto_car_repair_blog_page_id,
                        'menu-item-object'     => 'page',
                        'menu-item-type'       => 'post_type',
                    ));
                }
            
			/* ---------- Assign Menu Location ---------- */
			$auto_car_repair_locations = get_theme_mod( 'nav_menu_locations', array() );
			$auto_car_repair_locations[ $auto_car_repair_location ] = $auto_car_repair_menu_id;
			set_theme_mod( 'nav_menu_locations', $auto_car_repair_locations );
		}

	}
}