<?php
/**
 * Activity Guard – Real Time Notifier to Slack Rest Route.
 *
 * This class is used to response and all rest route works.
 *
 * @package WPNTS\Inc
 */

namespace WPNTS\Inc;

use WPNTS\Inc\wpnts_nts_fs; // phpcs:ignore
use WPNTS\Inc\Activate; // phpcs:ignore
use WPNTS\Inc\Deactivate; // phpcs:ignore
use WPNTS\Inc\Database\DB; // phpcs:ignore
use WPNTS\Inc\SlackAttachment; // phpcs:ignore
use WPNTS\Inc\SlackChannelRouter; // phpcs:ignore

defined('ABSPATH') || die('Hey, what are you doing here? You silly human!');
/**
 * Menu used to rest route created.
 *
 * @since 1.0.0
 */
class Menu {

	/**
	 * Construct method.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		$wpnts_db_instance = new DB();
		$schedules_int = get_option( 'wpntswebhook_menu_settings');
		$schedules_interval = json_decode($schedules_int);

		$create_nav_menu = $schedules_interval->create_nav_menu ?? 'false';
		$delete_nav_menu = $schedules_interval->delete_nav_menu ?? 'false';
		$update_nav_menu = $schedules_interval->update_nav_menu ?? 'false';

		// Track navigation menu creation.
		if ( true === $create_nav_menu ) {
			add_action('wp_create_nav_menu', [ $this, 'wpnts_menu_created' ], 10, 1);
		}

		// Track navigation menu deletion.
		if ( true === $delete_nav_menu ) {
			add_action('wp_delete_nav_menu', [ $this, 'wpnts_menu_deleted' ], 10, 1);
		}

		// Track navigation menu update.
		if ( true === $update_nav_menu ) {
			add_action('wp_update_nav_menu', [ $this, 'wpnts_menu_updated' ], 10, 1);
		}
	}

	/**
	 * Notify Slack and log activity when a navigation menu is created.
	 *
	 * @param int $menu_id The ID of the created menu.
	 */
	public function wpnts_menu_created( $menu_id ) {
		// Get the menu name.
		$menu_name = wp_get_nav_menu_object($menu_id)->name;

		// Get the current user's details.
		$user_id = get_current_user_id();
		$author = get_the_author_meta('display_name', $user_id);

		// Get the site URL, including child site for multisite.
		$attachmentHandler = new SlackAttachment();
		$site_url = $attachmentHandler->getSiteURLAddOption();

		$this->wpnts_notify_slack("Menu '{$menu_name}' has been created by {$author}.  $site_url", ':wastebasket:');

		$log_message = "Menu <b>'$menu_name'</b> has been created by <b>{$author}</b>.  <br> $site_url";
		$severity = 'High';
		$event_type = 'Menu';
		$time = current_time( 'mysql' );
		$ip = $attachmentHandler->get_user_ip();
		$attachmentHandler->log_activity_to_db( $severity, $time, $ip, $author, $event_type, $log_message  );
	}

	/**
	 * Notify Slack and log activity when a navigation menu is updated.
	 *
	 * @param int $menu_id The ID of the updated menu.
	 */
	public function wpnts_menu_updated( $menu_id ) {
		// Get the menu name.
		$menu_name = wp_get_nav_menu_object($menu_id)->name;

		// Get the current user's details.
		$user_id = get_current_user_id();
		$author = get_the_author_meta('display_name', $user_id);

		// Get the site URL, including child site for multisite.
		$attachmentHandler = new SlackAttachment();
		$site_url = $attachmentHandler->getSiteURLAddOption();

		$this->wpnts_notify_slack("Menu '{$menu_name}' has been updated by {$author}.  $site_url", ':wastebasket:');

		$log_message = "Menu <b>'$menu_name'</b> has been updated by <b>{$author}</b>.  <br> $site_url";
		$severity = 'High';
		$event_type = 'Menu';
		$time = current_time( 'mysql' );
		$ip = $attachmentHandler->get_user_ip();
		$attachmentHandler->log_activity_to_db( $severity, $time, $ip, $author, $event_type, $log_message  );
	}



	/**
	 * Notify Slack and log activity when a navigation menu is deleted.
	 *
	 * @param int $menu_id The ID of the deleted menu.
	 */
	public function wpnts_menu_deleted( $menu_id ) {
		// Attempt to get the menu object.
		$menu_object = wp_get_nav_menu_object($menu_id);

		// Get the current user's details.
		$user_id = get_current_user_id();
		$author = get_the_author_meta('display_name', $user_id);

		// Get the site URL, including child site for multisite.
		$attachmentHandler = new SlackAttachment();
		$site_url = $attachmentHandler->getSiteURLAddOption();

		if ( $menu_object ) {
			// Menu object exists, get the menu name.
			$menu_name = $menu_object->name;
			$this->wpnts_notify_slack("Menu '{$menu_name}' has been deleted by {$author}.  $site_url", ':wastebasket:');
			$log_message = "Menu <b>'$menu_name'</b> has been deleted by <b>{$author}</b>.  <br> $site_url";
		} else {
			// Menu object does not exist, notify using the ID.
			$this->wpnts_notify_slack("Menu with ID '{$menu_id}' has been deleted by {$author}.  $site_url", ':wastebasket:');
			$log_message = "Menu with ID <b>'$menu_id'</b> has been deleted by <b>{$author}</b>.  <br> $site_url";
		}

		// Log the activity.
		$severity = 'High';
		$event_type = 'Menu';
		$time = current_time('mysql');
		$ip = $attachmentHandler->get_user_ip();
		$attachmentHandler->log_activity_to_db($severity, $time, $ip, $author, $event_type, $log_message);
	}



	/**
	 * Sends a notification message to a configured Slack webhook with channel routing.
	 *
	 * This function formats a message with an optional emoji and sends it to the Slack webhook URL
	 * specified in the plugin's menu settings, with support for custom channels and user tags.
	 *
	 * @param string $message The message to send to Slack.
	 * @param string $emoji   An optional emoji to prepend to the message.
	 * @return void
	 */
	private function wpnts_notify_slack( $message, $emoji = '' ) {
		$formatted_message = $emoji . ' ' . $message;
		
		// Use SlackChannelRouter for channel and user tag support
		SlackChannelRouter::send_notification_with_routing(
			'wpntswebhook_menu_settings',
			$formatted_message
		);
	}
}
