<?php
/**
 * Builder home page + redirect Appearance → Editor to Aura Builder.
 *
 * @package AuraBuilder
 */

declare(strict_types=1);

namespace AuraBuilder\Admin;

final class HomePageBridge {

	public const OPTION        = 'aptb_home_page';
	public const OPTION_LEGACY = 'aura_builder_home_page';

	public function hooks(): void {
		add_action( 'admin_init', array( $this, 'redirect_theme_editor' ), 1 );
		// Soft-replace Site Editor menu link when a builder home is set.
		add_action( 'admin_menu', array( $this, 'repoint_site_editor_menu' ), 999 );
	}

	public static function get_home_id(): int {
		$id = (int) get_option( self::OPTION, 0 );
		if ( $id <= 0 ) {
			$id = (int) get_option( self::OPTION_LEGACY, 0 );
		}
		return max( 0, $id );
	}

	/**
	 * Mark post as site front page + builder home (or clear if disabling).
	 */
	public static function set_as_home( int $post_id, bool $on ): void {
		if ( $on ) {
			if ( $post_id <= 0 || get_post_status( $post_id ) === false ) {
				return;
			}
			update_option( self::OPTION, $post_id, false );
			// WordPress reading settings: static front page.
			update_option( 'show_on_front', 'page' );
			update_option( 'page_on_front', $post_id );
			// If no posts page, leave page_for_posts alone.
			return;
		}
		$current = self::get_home_id();
		if ( $current === $post_id ) {
			delete_option( self::OPTION );
			// Only clear WP front if it pointed at this page.
			if ( (int) get_option( 'page_on_front' ) === $post_id ) {
				update_option( 'show_on_front', 'posts' );
				update_option( 'page_on_front', 0 );
			}
		}
	}

	public static function is_home( int $post_id ): bool {
		if ( $post_id <= 0 ) {
			return false;
		}
		if ( self::get_home_id() === $post_id ) {
			return true;
		}
		return get_option( 'show_on_front' ) === 'page' && (int) get_option( 'page_on_front' ) === $post_id;
	}

	/**
	 * Aparência → Editor (FSE / theme editor) → Aura Builder home.
	 */
	public function redirect_theme_editor(): void {
		if ( ! is_admin() || ! current_user_can( 'edit_pages' ) ) {
			return;
		}
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		if ( ! empty( $_GET['ab_skip_home'] ) ) {
			return;
		}
		$home = self::get_home_id();
		if ( $home <= 0 || ! current_user_can( 'edit_post', $home ) ) {
			return;
		}

		global $pagenow;
		$pagenow = (string) $pagenow;

		$targets = array( 'site-editor.php', 'theme-editor.php' );
		// Gutenberg site editor sometimes loads as admin.php?page=gutenberg-edit-site
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$page = isset( $_GET['page'] ) ? sanitize_key( (string) $_GET['page'] ) : '';
		$is_fse_page = in_array( $page, array( 'gutenberg-edit-site', 'site-editor' ), true );

		if ( ! in_array( $pagenow, $targets, true ) && ! $is_fse_page ) {
			return;
		}

		$url = admin_url( 'admin.php?page=aura-builder&post=' . $home );
		wp_safe_redirect( $url );
		exit;
	}

	/**
	 * Point “Editor” under Appearance to the builder home when set.
	 */
	public function repoint_site_editor_menu(): void {
		$home = self::get_home_id();
		if ( $home <= 0 || ! current_user_can( 'edit_post', $home ) ) {
			return;
		}
		global $submenu;
		if ( empty( $submenu['themes.php'] ) || ! is_array( $submenu['themes.php'] ) ) {
			return;
		}
		$url = admin_url( 'admin.php?page=aura-builder&post=' . $home );
		foreach ( $submenu['themes.php'] as $i => $item ) {
			if ( ! is_array( $item ) || empty( $item[2] ) ) {
				continue;
			}
			$slug = (string) $item[2];
			// Site Editor / Theme File Editor.
			if (
				$slug === 'site-editor.php'
				|| $slug === 'theme-editor.php'
				|| str_contains( $slug, 'site-editor' )
				|| str_contains( $slug, 'gutenberg-edit-site' )
			) {
				$submenu['themes.php'][ $i ][0] = 'Editor (Aura)';
				$submenu['themes.php'][ $i ][2] = 'admin.php?page=aura-builder&post=' . $home;
			}
		}
	}
}
