<?php

namespace Brandy\Niches\Import;

use Brandy\Niches\NicheLoader;

/**
 * Imports sample nav menus (and the widgets marker) for a niche.
 *
 * Extracted verbatim from NicheService as part of the behavior-preserving
 * decomposition.
 */
class MenuImporter {

	/**
	 * Selected niche import data.
	 *
	 * @var array
	 */
	private $selected_niche;

	public function __construct( $selected_niche ) {
		$this->selected_niche = $selected_niche;
	}

	public function import() {
		if ( empty( $this->selected_niche ) ) {
			return;
		}

		$niche_id     = $this->selected_niche['id'];
		$sample_menus = $this->get_sample_menus();

		if ( empty( $sample_menus ) ) {
			return array();
		}

		do_action( 'brandy_before_import_menus', $niche_id, $sample_menus );

		$imported_menus = array();
		foreach ( $sample_menus as $menu_info ) {
			$menu_id = $this->create_or_get_menu( $menu_info );
			if ( ! $menu_id ) {
				continue;
			}

			$imported_menus[] = array_merge(
				array( 'id' => $menu_id ),
				$menu_info
			);

			$parent_menus = $this->add_top_level_menu_items( $menu_id, $menu_info['items'] );
			$this->add_submenu_items( $menu_id, $menu_info['items'], $parent_menus );
		}

		do_action( 'brandy_after_import_menus', $niche_id, $imported_menus );

		return $imported_menus;
	}

	private function get_sample_menus() {
		if ( ! isset( $this->selected_niche['sample_menus'] ) || ! is_array( $this->selected_niche['sample_menus'] ) ) {
			return array();
		}

		$niche_instance = NicheLoader::get_instance()->get_niche_instance( $this->selected_niche['id'] );

		if ( ! empty( $niche_instance ) && is_callable( array( $niche_instance, 'get_sample_menus' ) ) ) {
			return $niche_instance::get_sample_menus();
		}

		return $this->selected_niche['sample_menus'];
	}

	private function create_or_get_menu( $menu_info ) {
		$existing_menu = wp_get_nav_menu_object( $menu_info['name'] );
		if ( $existing_menu ) {
			return $existing_menu->term_id;
		}

		$menu_id = wp_create_nav_menu( $menu_info['name'] );
		return is_wp_error( $menu_id ) ? false : $menu_id;
	}

	private function add_top_level_menu_items( $menu_id, $items ) {
		$parent_menus = array();

		foreach ( $items as $menu_item ) {
			if ( isset( $menu_item['parent'] ) ) {
				continue;
			}

			$menu_args = $this->get_menu_item_args( $menu_item );
			$id        = wp_update_nav_menu_item( $menu_id, 0, $menu_args );

			if ( ! is_wp_error( $id ) && ! empty( $menu_item['key'] ) ) {
				$parent_menus[ $menu_item['key'] ] = $id;
			}
		}

		return $parent_menus;
	}

	private function add_submenu_items( $menu_id, $items, $parent_menus ) {
		foreach ( $items as $submenu ) {
			if ( ! isset( $submenu['parent'] ) || ! isset( $parent_menus[ $submenu['parent'] ] ) ) {
				continue;
			}

			$menu_args = $this->get_menu_item_args( $submenu, $parent_menus[ $submenu['parent'] ] );
			wp_update_nav_menu_item( $menu_id, 0, $menu_args );
		}
	}

	private function get_menu_item_args( $item, $parent_id = 0 ) {
		$base_args = array(
			'menu-item-title'  => $item['title'] ?? 'Sample item',
			'menu-item-status' => $item['status'] ?? 'publish',
		);

		if ( ! empty( $parent_id ) ) {
			$base_args['menu-item-parent-id'] = $parent_id;
		}

		if ( empty( $item['object_id'] ) ) {
			return array_merge(
				$base_args,
				array(
					'menu-item-url' => $item['url'] ?? '#',
				)
			);
		}

		return array_merge(
			$base_args,
			array(
				'menu-item-type'      => $item['item_type'] ?? '',
				'menu-item-object-id' => $item['object_id'],
				'menu-item-object'    => $item['item_object'],
			)
		);
	}

	public function import_widgets() {
		if ( empty( $this->selected_niche ) ) {
			return;
		}
		update_option( 'brandy_sample_widgets', $this->selected_niche['id'] );
	}
}
