<?php
/**
 * Show IDs premium screens — packed only in the Pro add-on overlay.
 *
 * Secure design (rafter-secure-design):
 * - Identity: WordPress admins viewing list tables / the admin bar. No new auth.
 * - AuthZ: User, media, comment, action-row, and frontend admin-bar IDs live
 *   in this file. Lite cannot run it; Freemium::unlocked requires Pro + license.
 * - Output: IDs are integers escaped with esc_html().
 *
 * @package ShowIds
 */

namespace ShowIds;

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

/**
 * Users, media, comments, action-row IDs, and the frontend admin bar.
 */
final class Premium {

	public static function boot( Module $module ): void {
		if ( ! function_exists( 'wp_powersuite_is_license_valid' ) || ! wp_powersuite_is_license_valid() ) {
			return;
		}

		$settings = $module->getSettings();

		if ( ! empty( $settings['show_frontend_admin_bar'] ) ) {
			add_action( 'admin_bar_menu', [ self::class, 'addFrontendAdminBarId' ], 999 );
		}

		if ( ! is_admin() ) {
			return;
		}

		if ( ! empty( $settings['show_user_ids'] ) ) {
			add_action(
				'load-users.php',
				static function () use ( $module ): void {
					self::registerUsersListHooks( $module );
				}
			);
		}

		if ( ! empty( $settings['show_media_ids'] ) ) {
			add_action(
				'load-upload.php',
				static function () use ( $module ): void {
					self::registerMediaListHooks( $module );
				}
			);
		}

		if ( ! empty( $settings['show_comment_ids'] ) ) {
			add_action(
				'load-edit-comments.php',
				static function () use ( $module ): void {
					self::registerCommentsListHooks( $module );
				}
			);
		}
	}

	public static function registerActionRowForPosts( Module $module, string $post_type ): void {
		if ( empty( $module->getSettings()['show_id_in_action_row'] ) ) {
			return;
		}

		add_filter( "{$post_type}_row_actions", [ self::class, 'addIdInActionRow' ], 99, 2 );
	}

	public static function registerActionRowForTaxonomy( Module $module, string $taxonomy ): void {
		if ( empty( $module->getSettings()['show_id_in_action_row'] ) ) {
			return;
		}

		add_filter( "{$taxonomy}_row_actions", [ self::class, 'addIdInActionRow' ], 99, 2 );
	}

	public static function registerUsersListHooks( Module $module ): void {
		$settings = $module->getSettings();

		add_filter( 'manage_users_columns', [ $module, 'addIdColumn' ] );
		add_filter( 'manage_users_custom_column', [ self::class, 'displayUserId' ], 10, 3 );
		add_filter( 'manage_users_sortable_columns', [ $module, 'makeIdColumnSortable' ] );

		if ( ! empty( $settings['show_id_in_action_row'] ) ) {
			add_filter( 'user_row_actions', [ self::class, 'addIdInActionRow' ], 99, 2 );
		}

		$module->registerListColumnStyles();
	}

	public static function registerMediaListHooks( Module $module ): void {
		$settings = $module->getSettings();

		add_filter( 'manage_media_columns', [ $module, 'addIdColumn' ] );
		add_action( 'manage_media_custom_column', [ $module, 'displayPostId' ], 10, 2 );
		add_filter( 'manage_upload_sortable_columns', [ $module, 'makeIdColumnSortable' ] );

		if ( ! empty( $settings['show_id_in_action_row'] ) ) {
			add_filter( 'media_row_actions', [ self::class, 'addIdInActionRow' ], 99, 2 );
		}

		$module->registerListColumnStyles();
	}

	public static function registerCommentsListHooks( Module $module ): void {
		$settings = $module->getSettings();

		add_filter( 'manage_edit-comments_columns', [ $module, 'addIdColumn' ] );
		add_action( 'manage_comments_custom_column', [ self::class, 'displayCommentId' ], 10, 2 );

		if ( ! empty( $settings['show_id_in_action_row'] ) ) {
			add_filter( 'comment_row_actions', [ self::class, 'addIdInActionRow' ], 99, 2 );
		}

		$module->registerListColumnStyles();
	}

	public static function displayUserId( string $content, string $column_name, $user_id ): string {
		if ( $column_name === Module::COLUMN_NAME ) {
			return '<strong>' . esc_html( (string) (int) $user_id ) . '</strong>';
		}

		return $content;
	}

	public static function displayCommentId( string $column_name, $comment_id ): void {
		if ( $column_name === Module::COLUMN_NAME ) {
			echo '<strong>' . esc_html( (string) (int) $comment_id ) . '</strong>';
		}
	}

	/**
	 * @param array<string, string> $actions Row actions.
	 * @param object                $object  List table row object.
	 * @return array<string, string>
	 */
	public static function addIdInActionRow( array $actions, $object ): array {
		if ( ! current_user_can( 'edit_posts' ) ) {
			return $actions;
		}

		$id = self::resolveObjectId( $object );
		if ( $id === null ) {
			return $actions;
		}

		$actions['wpps-list-table-item-id'] = sprintf(
			'<span class="wpps-list-table-item-id">%s</span>',
			esc_html(
				sprintf(
					/* translators: %d: object ID number */
					__( 'ID: %d', 'wppowersuite-lite' ),
					$id
				)
			)
		);

		return $actions;
	}

	public static function addFrontendAdminBarId( \WP_Admin_Bar $wp_admin_bar ): void {
		if ( is_admin() ) {
			return;
		}

		$object_id   = null;
		$object_type = '';

		if ( is_singular() ) {
			global $post;
			if ( $post instanceof \WP_Post ) {
				$object_id   = $post->ID;
				$object_type = ucfirst( $post->post_type );
			}
		} elseif ( is_category() || is_tag() || is_tax() ) {
			$queried_object = get_queried_object();
			if ( $queried_object instanceof \WP_Term ) {
				$object_id   = $queried_object->term_id;
				$object_type = ucfirst( $queried_object->taxonomy );
			}
		} elseif ( is_author() ) {
			$author = get_queried_object();
			if ( $author instanceof \WP_User ) {
				$object_id   = $author->ID;
				$object_type = 'User';
			}
		}

		if ( $object_id === null ) {
			return;
		}

		$wp_admin_bar->add_node(
			[
				'id'     => 'wpps-show-id',
				'title'  => sprintf(
					'<span class="ab-label">%s ID: <strong>%d</strong></span>',
					esc_html( $object_type ),
					(int) $object_id
				),
				'parent' => 'top-secondary',
				'meta'   => [
					'title' => sprintf(
						/* translators: 1: object type label, 2: numeric ID */
						__( '%1$s ID: %2$d', 'wppowersuite-lite' ),
						$object_type,
						$object_id
					),
				],
			]
		);
	}

	private static function resolveObjectId( object $object ): ?int {
		if ( property_exists( $object, 'ID' ) ) {
			return (int) $object->ID;
		}

		if ( property_exists( $object, 'term_id' ) ) {
			return (int) $object->term_id;
		}

		if ( property_exists( $object, 'comment_ID' ) ) {
			return (int) $object->comment_ID;
		}

		return null;
	}
}
