<?php

namespace VendorTasksDokanClickUp\Settings;

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

/**
 * Admin Menu Class
 *
 * Handles admin menu registration and integration
 *
 * @since 1.0.0
 * @package VendorTasksDokanClickUp
 * @subpackage Settings
 */
class Menu {

	/**
	 * Class constructor
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		add_action( 'admin_menu', array( $this, 'register_menus' ), 999 );
		add_filter( 'custom_menu_order', '__return_true' );
		add_filter( 'menu_order', array( $this, 'set_menu_order' ), 99 );
	}

	/**
	 * Register root admin page and Dokan submenu symlink
	 *
	 * @since 1.1.0
	 *
	 * @return void
	 */
	public function register_menus(): void {
		$icon = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyMCIgaGVpZ2h0PSIyMCIgdmlld0JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9IiNhN2FhYWQiIHN0cm9rZS13aWR0aD0iMS44IiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHJvbGU9ImltZyIgYXJpYS1sYWJlbD0iQ2xpY2tVcCBJbnRlZ3JhdGlvbiBtZW51IGljb24iPjxwYXRoIGQ9Ik0yMC41IDEyIEE4LjUgOC41IDAgMSAxIDE3LjIgNS4zIj48L3BhdGg+PHBhdGggZD0iTTguMyAxMi4zIEwxMSAxNSBMMTUuNyA5LjQiPjwvcGF0aD48cG9seWdvbiBwb2ludHM9IjIxLjQsMy40IDIxLjEsOC43IDE2LjIsNi40IiBmaWxsPSIjYTdhYWFkIiBzdHJva2U9Im5vbmUiPjwvcG9seWdvbj48L3N2Zz4=';

		// Find Dokan's actual registered position and slot in right after it.
		global $menu;
		$dokan_pos = null;
		foreach ( $menu as $pos => $item ) {
			if ( isset( $item[2] ) && 'dokan' === $item[2] ) {
				$dokan_pos = (float) $pos;
				break;
			}
		}
		$our_position = null !== $dokan_pos ? $dokan_pos + 0.01 : 55.41;

		add_menu_page(
			__( 'Vendor Tasks for Dokan & ClickUp', 'alamin-vendor-tasks-dokan-clickup' ),
			__( 'Vendor Tasks', 'alamin-vendor-tasks-dokan-clickup' ),
			'manage_options',
			'vendor-tasks-dokan-clickup',
			array( $this, 'settings_page' ),
			$icon,
			$our_position
		);

		// Dashboard + Plans + Account submenus.
		global $submenu; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
		$submenu['vendor-tasks-dokan-clickup'][] = array( // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
			__( 'Dashboard', 'alamin-vendor-tasks-dokan-clickup' ),
			'manage_options',
			'admin.php?page=vendor-tasks-dokan-clickup#/overview',
			__( 'Dashboard', 'alamin-vendor-tasks-dokan-clickup' ),
		);
		$submenu['vendor-tasks-dokan-clickup'][] = array( // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
			__( 'Plans', 'alamin-vendor-tasks-dokan-clickup' ),
			'manage_options',
			'admin.php?page=vendor-tasks-dokan-clickup#/plans',
			__( 'Plans', 'alamin-vendor-tasks-dokan-clickup' ),
		);

		// Dokan submenu entry — same slug routes to root page.
		if ( class_exists( 'WeDevs_Dokan' ) || class_exists( 'Dokan_Lite' ) ) {
			add_submenu_page(
				'dokan',
				__( 'Vendor Tasks for Dokan & ClickUp', 'alamin-vendor-tasks-dokan-clickup' ),
				__( 'Vendor Tasks', 'alamin-vendor-tasks-dokan-clickup' ),
				'manage_options',
				'vendor-tasks-dokan-clickup'
			);
		}
	}

	/**
	 * Place our menu slug immediately after 'dokan' in the admin menu order.
	 *
	 * WooCommerce's menu_order filter overrides numeric position ordering, so we
	 * must hook the same filter (at later priority) and splice our slug in explicitly.
	 *
	 * @since 1.1.0
	 *
	 * @param string[] $menu_order Current menu order slugs.
	 * @return string[] Modified menu order.
	 */
	public function set_menu_order( array $menu_order ): array {
		$dokan_pos = array_search( 'dokan', $menu_order, true );
		if ( false === $dokan_pos ) {
			return $menu_order;
		}

		// Remove our slug from its current position (prevents duplicates).
		$our_slug = 'vendor-tasks-dokan-clickup';
		$our_pos  = array_search( $our_slug, $menu_order, true );
		if ( false !== $our_pos ) {
			array_splice( $menu_order, (int) $our_pos, 1 );
			// Recalculate dokan position after removal.
			$dokan_pos = array_search( 'dokan', $menu_order, true );
		}

		if ( false === $dokan_pos ) {
			return $menu_order;
		}

		array_splice( $menu_order, (int) $dokan_pos + 1, 0, array( $our_slug ) );
		return $menu_order;
	}

	/**
	 * Render settings page
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function settings_page(): void {
		wc_get_template(
			'settings/settings-page.php',
			array(),
			'vendor-tasks-dokan-clickup/',
			VENDOR_TASKS_DOKAN_CLICKUP_PATH . 'templates/'
		);
	}
}
