<?php
/**
 * PageInstaller - Creates default pages on first plugin activation.
 *
 * @package RadiusTheme\RadiusBooking\Setup
 */

namespace RadiusTheme\RadiusBooking\Setup;

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

/**
 * Handles creation of default booking pages during first install.
 *
 * @since 1.0.0
 */
class PageInstaller {

	/**
	 * Pages to create: settings_key => [title, shortcode_content].
	 *
	 * @return array
	 */
	private static function get_pages(): array {
		return array(
			'bookingFormPage' => array(
				'title'   => __( 'Booking Form', 'radius-booking' ),
				'content' => '<!-- wp:shortcode -->[radius_booking_form]<!-- /wp:shortcode -->',
			),
			'myBookingPage'        => array(
				'title'   => __( 'My Booking', 'radius-booking' ),
				'content' => '<!-- wp:shortcode -->[rtrb_my_booking]<!-- /wp:shortcode -->',
			),
			'meetingBookingPage'   => array(
				'title'   => __( 'Book a Meeting', 'radius-booking' ),
				'content' => '<!-- wp:shortcode -->[rtrb_meeting_booking]<!-- /wp:shortcode -->',
			),
			'eventListingPage'     => array(
				'title'   => __( 'Events', 'radius-booking' ),
				'content' => '<!-- wp:shortcode -->[rtrb_event_list]<!-- /wp:shortcode -->',
			),
			'eventRegistrationPage' => array(
				'title'   => __( 'Event Registration', 'radius-booking' ),
				'content' => '<!-- wp:shortcode -->[rtrb_event_registration]<!-- /wp:shortcode -->',
			),
		);
	}

	/**
	 * Create default pages on first install and store their IDs in settings.
	 *
	 * @return void
	 */
	public static function create_pages(): void {
		$pages    = self::get_pages();
		$page_ids = array();

		foreach ( $pages as $key => $page_data ) {
			$page_id = wp_insert_post(
				array(
					'post_title'     => $page_data['title'],
					'post_content'   => $page_data['content'],
					'post_status'    => 'publish',
					'post_type'      => 'page',
					'post_author'    => get_current_user_id() ?: 1,
					'comment_status' => 'closed',
				),
				true
			);

			if ( ! is_wp_error( $page_id ) ) {
				$page_ids[ $key ] = $page_id;
				update_post_meta( $page_id, '_radius_booking_page', $key );
			}
		}

		if ( ! empty( $page_ids ) ) {
			$existing = get_option( 'rtrb_pages_settings', array() );
			$merged   = array_merge( is_array( $existing ) ? $existing : array(), $page_ids );
			update_option( 'rtrb_pages_settings', $merged );
		}
	}

	/**
	 * Create any missing pages that were added in newer versions.
	 *
	 * Checks the saved page settings and only creates pages whose keys
	 * are missing or whose page IDs point to trashed/deleted posts.
	 *
	 * @since 1.2.0
	 *
	 * @return void
	 */
	public static function create_missing_pages(): void {
		$all_pages = self::get_pages();
		$existing  = get_option( 'rtrb_pages_settings', array() );

		if ( ! is_array( $existing ) ) {
			$existing = array();
		}

		$new_ids = array();

		foreach ( $all_pages as $key => $page_data ) {
			// Skip if page already exists and is published.
			if ( ! empty( $existing[ $key ] ) && 'publish' === get_post_status( $existing[ $key ] ) ) {
				continue;
			}

			$page_id = wp_insert_post(
				array(
					'post_title'     => $page_data['title'],
					'post_content'   => $page_data['content'],
					'post_status'    => 'publish',
					'post_type'      => 'page',
					'post_author'    => get_current_user_id() ?: 1,
					'comment_status' => 'closed',
				),
				true
			);

			if ( ! is_wp_error( $page_id ) ) {
				$new_ids[ $key ] = $page_id;
				update_post_meta( $page_id, '_radius_booking_page', $key );
			}
		}

		if ( ! empty( $new_ids ) ) {
			$merged = array_merge( $existing, $new_ids );
			update_option( 'rtrb_pages_settings', $merged );
		}
	}
}