<?php
/**
 * Programmatic menu creation.
 *
 * Creates Primary and Footer nav menus on theme activation
 * with all the correct menu items and hierarchy.
 *
 * @package Northfield_Theatre
 * @since 1.0.0
 */

/**
 * Create default navigation menus on theme activation.
 */
function northfield_create_default_menus() {
	// Only run if menus don't already exist.
	$primary_menu = wp_get_nav_menu_object( 'Primary Navigation' );
	if ( $primary_menu ) {
		return;
	}

	// Create Primary Navigation menu.
	$primary_id = wp_create_nav_menu( 'Primary Navigation' );
	if ( is_wp_error( $primary_id ) ) {
		return;
	}

	// Helper to find or create a page and return its ID.
	$get_or_create_page = function ( $title, $slug, $template = '' ) {
		$page = get_page_by_path( $slug );
		if ( $page ) {
			return $page->ID;
		}

		$page_id = wp_insert_post( array(
			'post_title'   => $title,
			'post_name'    => $slug,
			'post_type'    => 'page',
			'post_status'  => 'publish',
			'post_content' => '',
		) );

		if ( ! is_wp_error( $page_id ) && $template ) {
			update_post_meta( $page_id, '_wp_page_template', $template );
		}

		return $page_id;
	};

	// Create necessary pages.
	$home_id      = $get_or_create_page( 'Home', 'home' );
	$whats_on_id  = $get_or_create_page( "What's On", 'whats-on', 'page-whats-on.php' );
	$visit_id     = $get_or_create_page( 'Your Visit', 'visit', 'page-visit.php' );
	$access_id    = $get_or_create_page( 'Access', 'access', 'page-access.php' );
	$basket_id    = $get_or_create_page( 'Basket', 'basket', 'page-basket.php' );
	$checkout_id  = $get_or_create_page( 'Checkout', 'checkout', 'page-checkout.php' );
	$seating_id   = $get_or_create_page( 'Seating Plan', 'seating-plan', 'page-seating.php' );
	$our_story_id = $get_or_create_page( 'Our Story', 'our-story', 'page-our-story.php' );
	$contact_id   = $get_or_create_page( 'Contact', 'contact', 'page-contact.php' );
	$moorside_id = $get_or_create_page( 'Moorside Town Hall', 'moorside', 'page-moorside.php' );
	$news_id      = $get_or_create_page( 'News', 'news' );

	$take_part_id  = $get_or_create_page( 'Take Part', 'take-part', 'page-take-part.php' );
	$hire_id       = $get_or_create_page( 'Hire', 'hire', 'page-hire.php' );
	$read_watch_id = $get_or_create_page( 'Read & Watch', 'read-watch' );
	$shop_id       = $get_or_create_page( 'Shop', 'shop' );

	// Build primary menu items.
	$menu_items = array(
		array(
			'title'  => "What's On",
			'object' => 'page',
			'id'     => $whats_on_id,
		),
		array(
			'title'    => 'Your Visit',
			'object'   => 'page',
			'id'       => $visit_id,
			'children' => array(
				array( 'title' => 'Getting Here', 'url' => home_url( '/visit/#getting-here' ) ),
				array( 'title' => 'Seating Plan', 'object' => 'page', 'id' => $seating_id ),
				array( 'title' => 'Access', 'object' => 'page', 'id' => $access_id ),
				array( 'title' => 'Eat & Drink', 'url' => home_url( '/visit/#eat-drink' ) ),
				array( 'title' => 'FAQs', 'url' => home_url( '/visit/#faqs' ) ),
			),
		),
		array( 'title' => 'Take Part', 'object' => 'page', 'id' => $take_part_id ),
		array(
			'title'    => 'Our Story',
			'object'   => 'page',
			'id'       => $our_story_id,
			'children' => array(
				array( 'title' => 'About Northfield', 'object' => 'page', 'id' => $our_story_id ),
				array( 'title' => 'Moorside Town Hall', 'object' => 'page', 'id' => $moorside_id ),
				array( 'title' => 'Contact', 'object' => 'page', 'id' => $contact_id ),
			),
		),
		array( 'title' => 'News', 'object' => 'page', 'id' => $news_id ),
		array( 'title' => 'Hire', 'object' => 'page', 'id' => $hire_id ),
	);

	$position = 0;
	foreach ( $menu_items as $item ) {
		$position += 10;
		$parent_db_id = 0;

		if ( isset( $item['id'] ) ) {
			$parent_db_id = wp_update_nav_menu_item( $primary_id, 0, array(
				'menu-item-title'     => $item['title'],
				'menu-item-object-id' => $item['id'],
				'menu-item-object'    => 'page',
				'menu-item-type'      => 'post_type',
				'menu-item-status'    => 'publish',
				'menu-item-position'  => $position,
			) );
		} elseif ( isset( $item['url'] ) ) {
			$parent_db_id = wp_update_nav_menu_item( $primary_id, 0, array(
				'menu-item-title'    => $item['title'],
				'menu-item-url'      => $item['url'],
				'menu-item-type'     => 'custom',
				'menu-item-status'   => 'publish',
				'menu-item-position' => $position,
			) );
		}

		// Add children.
		if ( ! empty( $item['children'] ) && ! is_wp_error( $parent_db_id ) ) {
			$child_pos = 0;
			foreach ( $item['children'] as $child ) {
				$position += 10;
				$child_pos++;

				if ( isset( $child['id'] ) ) {
					wp_update_nav_menu_item( $primary_id, 0, array(
						'menu-item-title'      => $child['title'],
						'menu-item-object-id'  => $child['id'],
						'menu-item-object'     => 'page',
						'menu-item-type'       => 'post_type',
						'menu-item-parent-id'  => $parent_db_id,
						'menu-item-status'     => 'publish',
						'menu-item-position'   => $position,
					) );
				} elseif ( isset( $child['url'] ) ) {
					wp_update_nav_menu_item( $primary_id, 0, array(
						'menu-item-title'     => $child['title'],
						'menu-item-url'       => $child['url'],
						'menu-item-type'      => 'custom',
						'menu-item-parent-id' => $parent_db_id,
						'menu-item-status'    => 'publish',
						'menu-item-position'  => $position,
					) );
				}
			}
		}
	}

	// Assign menu to the primary location.
	$locations = get_theme_mod( 'nav_menu_locations', array() );
	$locations['primary'] = $primary_id;

	// Create Footer Navigation menu.
	$footer_id = wp_create_nav_menu( 'Footer Navigation' );
	if ( ! is_wp_error( $footer_id ) ) {
		$footer_items = array(
			array( 'title' => "What's On", 'object' => 'page', 'id' => $whats_on_id ),
			array( 'title' => 'Your Visit', 'object' => 'page', 'id' => $visit_id ),
			array( 'title' => 'Access', 'object' => 'page', 'id' => $access_id ),
			array( 'title' => 'Our Story', 'object' => 'page', 'id' => $our_story_id ),
			array( 'title' => 'News', 'object' => 'page', 'id' => $news_id ),
			array( 'title' => 'Contact', 'object' => 'page', 'id' => $contact_id ),
		);

		$pos = 0;
		foreach ( $footer_items as $fi ) {
			$pos += 10;
			wp_update_nav_menu_item( $footer_id, 0, array(
				'menu-item-title'     => $fi['title'],
				'menu-item-object-id' => $fi['id'],
				'menu-item-object'    => 'page',
				'menu-item-type'      => 'post_type',
				'menu-item-status'    => 'publish',
				'menu-item-position'  => $pos,
			) );
		}

		$locations['footer'] = $footer_id;
	}

	set_theme_mod( 'nav_menu_locations', $locations );

	// Set the static front page and the posts page.
	update_option( 'show_on_front', 'page' );
	update_option( 'page_on_front', $home_id );
	update_option( 'page_for_posts', $news_id );

	// Set permalink structure to post name (needed for CPT URLs).
	if ( get_option( 'permalink_structure' ) !== '/%postname%/' ) {
		update_option( 'permalink_structure', '/%postname%/' );
		flush_rewrite_rules();
	}
}
add_action( 'after_switch_theme', 'northfield_create_default_menus', 30 );

/**
 * Rebuilding existing installs is handled by the repair tool at
 * Tools → Northfield Repair (inc/repair.php), not by an admin_init hook.
 * Running heavy setup on every admin request was starving PHP-FPM
 * workers and deadlocking the site.
 */
