<?php
/**
 * Page template registration and starter site import.
 *
 * @package EasySite_Pro
 */

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

/**
 * Register page templates.
 *
 * @param array $templates Existing templates.
 * @return array
 */
function easysite_page_templates( $templates ) {
	$custom = array(
		'templates/template-home.php'     => __( 'EasySite Home', 'easysite-pro' ),
		'templates/template-about.php'    => __( 'EasySite About', 'easysite-pro' ),
		'templates/template-services.php' => __( 'EasySite Services', 'easysite-pro' ),
		'templates/template-contact.php'  => __( 'EasySite Contact', 'easysite-pro' ),
		'templates/template-shop.php'     => __( 'EasySite Shop', 'easysite-pro' ),
		'templates/template-blog.php'     => __( 'EasySite Blog', 'easysite-pro' ),
	);

	return array_merge( $templates, $custom );
}
add_filter( 'theme_page_templates', 'easysite_page_templates' );

/**
 * Import starter site demo content.
 *
 * @param string $starter_key Starter site key.
 */
function easysite_import_starter_site( $starter_key ) {
	$starters = easysite_get_starter_sites();
	if ( ! isset( $starters[ $starter_key ] ) ) {
		return;
	}

	$starter = $starters[ $starter_key ];
	$content = easysite_get_starter_content( $starter_key );

	$page_map   = array();
	$templates  = array(
		'home'     => 'templates/template-home.php',
		'about'    => 'templates/template-about.php',
		'services' => 'templates/template-services.php',
		'contact'  => 'templates/template-contact.php',
		'shop'     => 'templates/template-shop.php',
		'blog'     => 'templates/template-blog.php',
	);

	foreach ( $starter['pages'] as $page_key ) {
		if ( ! isset( $content['pages'][ $page_key ] ) ) {
			continue;
		}

		$page_data = $content['pages'][ $page_key ];
		$existing  = get_page_by_path( sanitize_title( $page_data['title'] ) );

		if ( $existing ) {
			$page_id = $existing->ID;
		} else {
			$page_id = wp_insert_post( array(
				'post_title'   => $page_data['title'],
				'post_content' => $page_data['content'],
				'post_status'  => 'publish',
				'post_type'    => 'page',
			) );
		}

		if ( $page_id && ! is_wp_error( $page_id ) ) {
			$page_map[ $page_key ] = $page_id;
			if ( isset( $templates[ $page_key ] ) ) {
				update_post_meta( $page_id, '_wp_page_template', $templates[ $page_key ] );
			}
		}
	}

	if ( ! empty( $page_map['home'] ) ) {
		update_option( 'show_on_front', 'page' );
		update_option( 'page_on_front', $page_map['home'] );
		easysite_setup_home_builder( $page_map['home'] );
	}
	if ( ! empty( $page_map['blog'] ) ) {
		update_option( 'page_for_posts', $page_map['blog'] );
	}

	if ( ! empty( $content['theme_mods'] ) ) {
		foreach ( $content['theme_mods'] as $mod => $value ) {
			set_theme_mod( $mod, $value );
		}
	}

	// Create primary menu.
	$menu_name = 'EasySite Primary';
	$menu_id   = wp_create_nav_menu( $menu_name );
	if ( ! is_wp_error( $menu_id ) ) {
		$order = 0;
		foreach ( array( 'home', 'about', 'services', 'shop', 'blog', 'contact' ) as $key ) {
			if ( empty( $page_map[ $key ] ) ) {
				continue;
			}
			wp_update_nav_menu_item( $menu_id, 0, array(
				'menu-item-title'  => get_the_title( $page_map[ $key ] ),
				'menu-item-object' => 'page',
				'menu-item-object-id' => $page_map[ $key ],
				'menu-item-type'   => 'post_type',
				'menu-item-status' => 'publish',
				'menu-item-position' => ++$order,
			) );
		}
		$locations = get_theme_mod( 'nav_menu_locations', array() );
		$locations['primary'] = $menu_id;
		set_theme_mod( 'nav_menu_locations', $locations );
	}

	// Sample blog post.
	if ( ! get_posts( array( 'post_type' => 'post', 'posts_per_page' => 1 ) ) ) {
		wp_insert_post( array(
			'post_title'   => $content['sample_post']['title'],
			'post_content' => $content['sample_post']['content'],
			'post_status'  => 'publish',
			'post_type'    => 'post',
		) );
	}

	do_action( 'easysite_starter_imported', $starter_key, $page_map );
}

/**
 * Enable visual page builder on homepage with starter sections.
 *
 * @param int $home_id Home page ID.
 */
function easysite_setup_home_builder( $home_id ) {
	update_post_meta( $home_id, EASYSITE_BUILDER_ENABLED, '1' );
	$layout = apply_filters( 'easysite_get_builder_layout_filter', array(), $home_id );
	if ( ! empty( $layout ) ) {
		update_post_meta( $home_id, EASYSITE_BUILDER_META, wp_json_encode( $layout ) );
	}
}

/**
 * Get demo content for a starter site.
 *
 * @param string $key Starter key.
 * @return array
 */
function easysite_get_starter_content( $key ) {
	$site_name = get_bloginfo( 'name' );
	$hero_img  = easysite_placeholder_image( 'hero' );

	$defaults = array(
		'pages' => array(
			'home' => array(
				'title'   => __( 'Home', 'easysite-pro' ),
				'content' => '',
			),
			'about' => array(
				'title'   => __( 'About', 'easysite-pro' ),
				'content' => '<p>' . esc_html__( 'We are passionate about helping our customers succeed. Our team brings years of experience and a friendly approach to every project.', 'easysite-pro' ) . '</p>',
			),
			'services' => array(
				'title'   => __( 'Services', 'easysite-pro' ),
				'content' => '<p>' . esc_html__( 'Explore the services we offer. Each one is designed to save you time and deliver great results.', 'easysite-pro' ) . '</p>',
			),
			'contact' => array(
				'title'   => __( 'Contact', 'easysite-pro' ),
				'content' => '<p>' . esc_html__( 'We would love to hear from you. Send us a message and we will get back to you soon.', 'easysite-pro' ) . '</p>',
			),
			'blog' => array(
				'title'   => __( 'Blog', 'easysite-pro' ),
				'content' => '',
			),
			'shop' => array(
				'title'   => __( 'Shop', 'easysite-pro' ),
				'content' => '<p>' . esc_html__( 'Browse our products below.', 'easysite-pro' ) . '</p>',
			),
		),
		'theme_mods' => array(
			'easysite_hero_title'    => sprintf( __( 'Welcome to %s', 'easysite-pro' ), $site_name ),
			'easysite_hero_subtitle' => __( 'Quality service you can trust—book a consultation today.', 'easysite-pro' ),
			'easysite_hero_button_text' => __( 'Contact Us', 'easysite-pro' ),
			'easysite_hero_button_link' => '/contact',
			'easysite_about_title'   => __( 'About Us', 'easysite-pro' ),
			'easysite_about_text'    => __( 'We help businesses grow with simple, effective solutions. Replace this text with your own story.', 'easysite-pro' ),
			'easysite_services_title'=> __( 'What We Offer', 'easysite-pro' ),
			'easysite_service_1_title' => __( 'Consultation', 'easysite-pro' ),
			'easysite_service_1_desc'  => __( 'A friendly chat to understand your needs and goals.', 'easysite-pro' ),
			'easysite_service_2_title' => __( 'Implementation', 'easysite-pro' ),
			'easysite_service_2_desc'  => __( 'We handle the details so you can focus on your business.', 'easysite-pro' ),
			'easysite_service_3_title' => __( 'Support', 'easysite-pro' ),
			'easysite_service_3_desc'  => __( 'Ongoing help whenever you need it.', 'easysite-pro' ),
		),
		'sample_post' => array(
			'title'   => __( 'Welcome to Our Blog', 'easysite-pro' ),
			'content' => '<p>' . esc_html__( 'This is your first blog post. Edit or delete it and start sharing your news and tips with visitors.', 'easysite-pro' ) . '</p>',
		),
	);

	$industry_overrides = array(
		'shop' => array(
			'easysite_hero_title'    => __( 'Shop Our Collection', 'easysite-pro' ),
			'easysite_hero_subtitle' => __( 'Quality products delivered to your door.', 'easysite-pro' ),
			'easysite_shop_show'     => true,
		),
		'restaurant' => array(
			'easysite_hero_title'    => __( 'Fresh Food, Great Atmosphere', 'easysite-pro' ),
			'easysite_hero_subtitle' => __( 'Reserve your table or order online today.', 'easysite-pro' ),
			'easysite_service_1_title' => __( 'Dine In', 'easysite-pro' ),
			'easysite_service_2_title' => __( 'Takeaway', 'easysite-pro' ),
			'easysite_service_3_title' => __( 'Catering', 'easysite-pro' ),
		),
		'fitness' => array(
			'easysite_hero_title'    => __( 'Train Smarter, Feel Stronger', 'easysite-pro' ),
			'easysite_hero_subtitle' => __( 'Personal training programs tailored to you.', 'easysite-pro' ),
		),
		'beauty' => array(
			'easysite_hero_title'    => __( 'Look and Feel Your Best', 'easysite-pro' ),
			'easysite_hero_subtitle' => __( 'Salon services and spa treatments you will love.', 'easysite-pro' ),
			'easysite_service_1_title' => __( 'Hair Styling', 'easysite-pro' ),
			'easysite_service_2_title' => __( 'Skin Care', 'easysite-pro' ),
			'easysite_service_3_title' => __( 'Nail Care', 'easysite-pro' ),
		),
		'realestate' => array(
			'easysite_hero_title'    => __( 'Find Your Dream Home', 'easysite-pro' ),
			'easysite_hero_subtitle' => __( 'Trusted local agents ready to help you buy or sell.', 'easysite-pro' ),
			'easysite_service_1_title' => __( 'Buy a Home', 'easysite-pro' ),
			'easysite_service_2_title' => __( 'Sell Your Property', 'easysite-pro' ),
			'easysite_service_3_title' => __( 'Property Valuation', 'easysite-pro' ),
		),
		'coach' => array(
			'easysite_hero_title'    => __( 'Unlock Your Potential', 'easysite-pro' ),
			'easysite_hero_subtitle' => __( 'Life and business coaching to help you reach your goals.', 'easysite-pro' ),
		),
		'charity' => array(
			'easysite_hero_title'    => __( 'Together We Make a Difference', 'easysite-pro' ),
			'easysite_hero_subtitle' => __( 'Support our mission and help your community.', 'easysite-pro' ),
		),
	);

	if ( isset( $industry_overrides[ $key ] ) ) {
		$defaults['theme_mods'] = array_merge( $defaults['theme_mods'], $industry_overrides[ $key ] );
	}

	return apply_filters( 'easysite_starter_content', $defaults, $key );
}
