<div class="theme-offer">
	<?php
	$home_tutor_demo_import_completed = get_option( 'home_tutor_demo_import_completed', false );

	if ( $home_tutor_demo_import_completed ) {
		echo '<p class="notice-text">' . esc_html__( 'Your demo import has been completed successfully.', 'home-tutor' ) . '</p>';
		echo '<span><a href="' . esc_url( home_url() ) . '" class="site-btn" target="_blank">' . esc_html__( 'VIEW SITE', 'home-tutor' ) . '</a></span>';
	}

	if ( isset( $_POST['submit'] ) && current_user_can( 'edit_theme_options' ) ) {
		if ( ! function_exists( 'home_tutor_attach_theme_image' ) ) {
			function home_tutor_attach_theme_image( $home_tutor_relative_path, $home_tutor_post_id = 0 ) {
				$home_tutor_source_path = trailingslashit( get_template_directory() ) . ltrim( $home_tutor_relative_path, '/' );
				if ( ! file_exists( $home_tutor_source_path ) ) {
					return 0;
				}

				$home_tutor_filename = wp_basename( $home_tutor_source_path );
				$home_tutor_bits     = wp_upload_bits( $home_tutor_filename, null, file_get_contents( $home_tutor_source_path ) );
				if ( ! empty( $home_tutor_bits['error'] ) ) {
					return 0;
				}

				$home_tutor_filetype   = wp_check_filetype( $home_tutor_filename, null );
				$home_tutor_attachment = array(
					'post_mime_type' => $home_tutor_filetype['type'],
					'post_title'     => sanitize_file_name( pathinfo( $home_tutor_filename, PATHINFO_FILENAME ) ),
					'post_content'   => '',
					'post_status'    => 'inherit',
				);

				$home_tutor_attach_id = wp_insert_attachment( $home_tutor_attachment, $home_tutor_bits['file'], $home_tutor_post_id );
				if ( is_wp_error( $home_tutor_attach_id ) ) {
					return 0;
				}

				require_once ABSPATH . 'wp-admin/includes/image.php';
				$home_tutor_metadata = wp_generate_attachment_metadata( $home_tutor_attach_id, $home_tutor_bits['file'] );
				wp_update_attachment_metadata( $home_tutor_attach_id, $home_tutor_metadata );
				return (int) $home_tutor_attach_id;
			}
		}

		$home_tutor_menu_name     = 'Main Menus';
		$home_tutor_menu_location = 'primary';
		$home_tutor_menu_object   = wp_get_nav_menu_object( $home_tutor_menu_name );
		$home_tutor_menu_id       = $home_tutor_menu_object ? (int) $home_tutor_menu_object->term_id : (int) wp_create_nav_menu( $home_tutor_menu_name );

		$home_tutor_pages = array(
			array(
				'title'   => 'Home',
				'slug'    => 'home',
				'content' => '',
			),
			array(
				'title'   => 'About',
				'slug'    => 'about',
				'content' => 'Home Tutor helps students build confidence and academic excellence through personalized one-on-one guidance.',
			),
			array(
				'title'   => 'Subjects',
				'slug'    => 'subjects',
				'content' => 'Explore subject-focused tutoring plans for school-level learning, exam preparation, and skill improvement.',
			),
			array(
				'title'   => 'Tutors',
				'slug'    => 'tutors',
				'content' => 'Meet our expert tutors and choose the right mentor based on subject, level, and learning goals.',
			),
			array(
				'title'   => 'Testimonials',
				'slug'    => 'testimonials',
				'content' => 'Read success stories from students and parents who improved results through guided tutoring.',
			),
			array(
				'title'   => 'Blog',
				'slug'    => 'blog',
				'content' => 'Find practical study materials, productivity guides, and learning support resources for students.',
			),
			array(
				'title'   => 'Contact',
				'slug'    => 'contact',
				'content' => 'Get in touch to discuss your learning goals and book a tutoring consultation.',
			),
		);

		$home_tutor_page_ids = array();
		foreach ( $home_tutor_pages as $home_tutor_page_data ) {
			$home_tutor_existing_page = get_page_by_path( $home_tutor_page_data['slug'], OBJECT, 'page' );
			if ( $home_tutor_existing_page ) {
				$home_tutor_page_ids[ $home_tutor_page_data['slug'] ] = (int) $home_tutor_existing_page->ID;
				continue;
			}

			$home_tutor_page_ids[ $home_tutor_page_data['slug'] ] = (int) wp_insert_post(
				array(
					'post_type'    => 'page',
					'post_title'   => $home_tutor_page_data['title'],
					'post_content' => $home_tutor_page_data['content'],
					'post_status'  => 'publish',
					'post_author'  => 1,
					'post_name'    => $home_tutor_page_data['slug'],
				)
			);
		}

			if ( ! empty( $home_tutor_page_ids['home'] ) ) {
			update_option( 'page_on_front', $home_tutor_page_ids['home'] );
			update_option( 'show_on_front', 'page' );
			update_post_meta( $home_tutor_page_ids['home'], '_wp_page_template', 'page-template/home-page.php' );
		}
		if ( ! empty( $home_tutor_page_ids['blog'] ) ) {
			update_option( 'page_for_posts', $home_tutor_page_ids['blog'] );
		}

		$home_tutor_existing_menu_items = wp_get_nav_menu_items( $home_tutor_menu_id );
		$home_tutor_menu_object_ids     = array();
		if ( ! empty( $home_tutor_existing_menu_items ) ) {
			foreach ( $home_tutor_existing_menu_items as $home_tutor_menu_item ) {
				$home_tutor_menu_object_ids[] = (int) $home_tutor_menu_item->object_id;
			}
		}

		foreach ( $home_tutor_pages as $home_tutor_page_data ) {
			$home_tutor_page_id = isset( $home_tutor_page_ids[ $home_tutor_page_data['slug'] ] ) ? (int) $home_tutor_page_ids[ $home_tutor_page_data['slug'] ] : 0;
			if ( ! $home_tutor_page_id || in_array( $home_tutor_page_id, $home_tutor_menu_object_ids, true ) ) {
				continue;
			}

			wp_update_nav_menu_item(
				$home_tutor_menu_id,
				0,
				array(
					'menu-item-title'     => $home_tutor_page_data['title'],
					'menu-item-status'    => 'publish',
					'menu-item-object-id' => $home_tutor_page_id,
					'menu-item-object'    => 'page',
					'menu-item-type'      => 'post_type',
				)
			);
		}

		$home_tutor_locations                           = get_theme_mod( 'nav_menu_locations', array() );
		$home_tutor_locations[ $home_tutor_menu_location ] = $home_tutor_menu_id;
		set_theme_mod( 'nav_menu_locations', $home_tutor_locations );

		$home_tutor_resources_category_slug = 'learning-resources';
		$home_tutor_resources_term          = term_exists( $home_tutor_resources_category_slug, 'category' );
		if ( ! $home_tutor_resources_term ) {
			$home_tutor_resources_term = wp_insert_term( 'Learning Resources', 'category', array( 'slug' => $home_tutor_resources_category_slug ) );
		}
		$home_tutor_resources_term_id = is_wp_error( $home_tutor_resources_term ) ? 0 : (int) $home_tutor_resources_term['term_id'];

		$home_tutor_resources_posts = array(
			array(
				'title'   => 'Mastering Mathematics Basics',
				'slug'    => 'mastering-mathematics-basics',
				'content' => 'Create a practical study routine with realistic goals, short sessions, and regular revision time.',
				'image'   => 'images/demo/resource-1.png',
			),
			array(
				'title'   => 'Effective Study Techniques',
				'slug'    => 'effective-study-techniques',
				'content' => 'Use topic planning, timed practice, and active recall methods to improve exam performance.',
				'image'   => 'images/demo/resource-2.png',
			),
			array(
				'title'   => 'Grammar Made Easy',
				'slug'    => 'grammar-made-easy',
				'content' => 'Break complex concepts into smaller parts and combine notes with problem-solving practice.',
				'image'   => 'images/demo/resource-3.png',
			),
			array(
				'title'   => 'Science Simplified for Students',
				'slug'    => 'science-simplified-for-students',
				'content' => 'Prioritize assignments and reduce distractions using simple planning tools and daily checklists.',
				'image'   => 'images/demo/resource-4.png',
			),
		);

		$home_tutor_banner_image_url = get_template_directory_uri() . '/images/demo/banner-right.png';

		foreach ( $home_tutor_resources_posts as $home_tutor_resource_post ) {
			$home_tutor_existing_post = get_page_by_path( $home_tutor_resource_post['slug'], OBJECT, 'post' );
			$home_tutor_post_id       = $home_tutor_existing_post ? (int) $home_tutor_existing_post->ID : (int) wp_insert_post(
				array(
					'post_type'    => 'post',
					'post_title'   => $home_tutor_resource_post['title'],
					'post_content' => $home_tutor_resource_post['content'],
					'post_status'  => 'publish',
					'post_author'  => 1,
					'post_name'    => $home_tutor_resource_post['slug'],
				)
			);

			if ( $home_tutor_post_id && $home_tutor_resources_term_id ) {
				wp_set_post_categories( $home_tutor_post_id, array( $home_tutor_resources_term_id ), false );
			}

			if ( $home_tutor_post_id && ! empty( $home_tutor_resource_post['image'] ) ) {
				$home_tutor_image_id = home_tutor_attach_theme_image( $home_tutor_resource_post['image'], $home_tutor_post_id );
				if ( $home_tutor_image_id ) {
					set_post_thumbnail( $home_tutor_post_id, $home_tutor_image_id );
				}
			}
		}

		// Banner and section demo settings.
		update_option( 'blogname', 'TutorEase' );
		update_option( 'blogdescription', 'Personalized Learning at Your Doorstep' );
		set_theme_mod( 'home_tutor_site_title_enable', true );
		set_theme_mod( 'home_tutor_site_tagline_enable', true );

		set_theme_mod( 'home_tutor_topbar_hide', true );
		set_theme_mod( 'home_tutor_topbar_text', 'Now offering free trial sessions for all new students this month! Book yours today and start learning smarter.' );
		set_theme_mod( 'home_tutor_banner_hide', true );
		set_theme_mod( 'home_tutor_banner_title', 'Empowering Students Through One-on-One Learning' );
		set_theme_mod( 'home_tutor_banner_desc_text', 'Build confidence with personalized tutoring, structured lessons, and expert guidance for better academic outcomes.' );
		set_theme_mod( 'home_tutor_banner_btn_text1', 'Find a Tutor' );
		set_theme_mod( 'home_tutor_banner_btn_url1', home_url( '/subjects/' ) );
		set_theme_mod( 'home_tutor_banner_btn_text2', 'Book Free Trial' );
		set_theme_mod( 'home_tutor_banner_btn_url2', home_url( '/contact/' ) );
		set_theme_mod( 'home_tutor_banner_phone_box_hide', true );
		set_theme_mod( 'home_tutor_banner_phone_icon', 'fas fa-phone-volume' );
		set_theme_mod( 'home_tutor_banner_phone_title', 'Call Us For Enquiry' );
		set_theme_mod( 'home_tutor_banner_phone_number', '+1234567890' );
		set_theme_mod( 'home_tutor_banner_mid_box_icon0', 'fas fa-user-graduate' );
		set_theme_mod( 'home_tutor_banner_mid_text0', 'Expert Tutors' );
		set_theme_mod( 'home_tutor_banner_mid_box_icon1', 'fas fa-clock' );
		set_theme_mod( 'home_tutor_banner_mid_text1', 'Flexible Timings' );
		set_theme_mod( 'home_tutor_banner_mid_box_icon2', 'far fa-dot-circle' );
		set_theme_mod( 'home_tutor_banner_mid_text2', 'Free Trial Session' );
		set_theme_mod( 'home_tutor_banner_mid_count', '75+' );
		set_theme_mod( 'home_tutor_banner_mid_count_text', 'Expert Tutors' );
		set_theme_mod( 'home_tutor_banner_right_image', $home_tutor_banner_image_url );

		set_theme_mod( 'home_tutor_section_sub_title', 'Learning Resources' );
		set_theme_mod( 'home_tutor_section_title', 'Recommended Study Materials' );
		set_theme_mod( 'home_tutor_resources_section_btn_text', 'View All' );
		set_theme_mod( 'home_tutor_resources_section_btn_url', "#" );
		set_theme_mod( 'home_tutor_our_resources', $home_tutor_resources_category_slug );
		set_theme_mod( 'home_tutor_practice_button_text', 'Read More' );
		set_theme_mod( 'home_tutor_footer_copy', 'By Buywptemplate' );

		update_option( 'home_tutor_demo_import_completed', true );
		echo '<p class="notice-text">' . esc_html__( 'Your demo import has been completed successfully.', 'home-tutor' ) . '</p>';
		echo '<span><a href="' . esc_url( home_url() ) . '" class="button button-primary site-btn" target="_blank">' . esc_html__( 'VIEW SITE', 'home-tutor' ) . '</a></span>';
	}
	?>

	<form action="<?php echo esc_url( home_url() ); ?>/wp-admin/themes.php?page=home-tutor-guide-page" method="POST" onsubmit="return validate(this);">
		<?php if ( ! get_option( 'home_tutor_demo_import_completed' ) ) : ?>
			<p><?php esc_html_e( 'Click the below run importer button to import demo content', 'home-tutor' ); ?></p>
			<input class="run-import" type="submit" name="submit" value="<?php esc_attr_e( 'Run Importer', 'home-tutor' ); ?>" class="button button-primary button-large">
		<?php endif; ?>
	</form>
	<script type="text/javascript">
	function validate(valid) {
		if (confirm("Do you really want to import the theme demo content?")) {
			document.getElementById('demo-import-loader').style.display = 'block';
			document.forms[0].submit();
		} else {
			return false;
		}
	}
	</script>
</div>
