<?php
/**
 * Show IDs Module
 *
 * @package ShowIds
 * @since 1.0.0
 */

namespace ShowIds;

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

use WP_PowerSuite\Core\AbstractModule;

require_once __DIR__ . '/FeatureGate.php';
require_once __DIR__ . '/includes/premium.php';

class Module extends AbstractModule {

	private const MODULE_ID      = 'show-ids';
	private const MODULE_VERSION = '1.3.1';
	public const COLUMN_NAME     = 'wpps_id';

	private ?array $settings_cache = null;

	public function getId(): string {
		return self::MODULE_ID;
	}

	public function getName(): string {
		return __( 'Show IDs', 'wppowersuite-lite' );
	}

	public function getVersion(): string {
		return self::MODULE_VERSION;
	}

	public function getDescription(): string {
		return __( 'Shows content and user IDs in list tables when you need quick reference numbers.', 'wppowersuite-lite' );
	}

	public function getContext(): string {
		return 'both';
	}

	protected function registerHooks(): void {
		add_filter( 'wp_powersuite_sanitize_module_settings', [ $this, 'filterSanitizeModuleSettings' ], 10, 3 );
		add_action( 'wp_powersuite_module_settings_saved', [ $this, 'onSettingsSaved' ], 10, 2 );

		$settings = $this->getSettings();

		if ( ! is_admin() ) {
			if ( class_exists( Premium::class, false ) ) {
				Premium::boot( $this );
			}
			return;
		}

		$enabled_post_types = $settings['enabled_post_types'] ?? [];
		if ( is_array( $enabled_post_types ) && $enabled_post_types !== [] ) {
			add_action( 'load-edit.php', [ $this, 'registerPostListHooks' ] );
		}

		if ( ! empty( $settings['show_taxonomy_ids'] ) ) {
			add_action( 'load-edit-tags.php', [ $this, 'registerTaxonomyListHooks' ] );
		}

		if ( class_exists( Premium::class, false ) ) {
			Premium::boot( $this );
		}
	}

	public function registerPostListHooks(): void {
		$settings           = $this->getSettings();
		$enabled_post_types = $settings['enabled_post_types'] ?? [];
		if ( ! is_array( $enabled_post_types ) || $enabled_post_types === [] ) {
			return;
		}

		$post_type = isset( $_GET['post_type'] ) ? sanitize_key( wp_unslash( (string) $_GET['post_type'] ) ) : 'post';
		if ( $post_type === '' || ! in_array( $post_type, $enabled_post_types, true ) || ! post_type_exists( $post_type ) ) {
			return;
		}

		add_filter( $this->postsColumnsHook( $post_type ), [ $this, 'addIdColumn' ] );
		add_action( $this->postsCustomColumnHook( $post_type ), [ $this, 'displayPostId' ], 10, 2 );
		add_filter( $this->sortableColumnsHook( $post_type ), [ $this, 'makeIdColumnSortable' ] );

		if ( class_exists( Premium::class, false ) ) {
			Premium::registerActionRowForPosts( $this, $post_type );
		}

		$this->registerListColumnStyles();
	}

	public function registerTaxonomyListHooks(): void {
		$taxonomy = isset( $_GET['taxonomy'] ) ? sanitize_key( wp_unslash( (string) $_GET['taxonomy'] ) ) : '';
		if ( $taxonomy === '' || ! taxonomy_exists( $taxonomy ) ) {
			return;
		}

		$tax_object = get_taxonomy( $taxonomy );
		if ( ! $tax_object instanceof \WP_Taxonomy || ! $tax_object->public ) {
			return;
		}

		add_filter( "manage_edit-{$taxonomy}_columns", [ $this, 'addIdColumn' ] );
		add_filter( "manage_{$taxonomy}_custom_column", [ $this, 'displayTermId' ], 10, 3 );

		if ( class_exists( Premium::class, false ) ) {
			Premium::registerActionRowForTaxonomy( $this, $taxonomy );
		}

		$this->registerListColumnStyles();
	}

	public function registerListColumnStyles(): void {
		static $registered = false;

		if ( $registered ) {
			return;
		}

		$registered = true;
		add_action( 'admin_head', [ $this, 'addColumnStyles' ] );
	}

	private function postsColumnsHook( string $post_type ): string {
		if ( $post_type === 'post' ) {
			return 'manage_posts_columns';
		}
		if ( $post_type === 'page' ) {
			return 'manage_pages_columns';
		}

		return "manage_{$post_type}_posts_columns";
	}

	private function postsCustomColumnHook( string $post_type ): string {
		if ( $post_type === 'post' ) {
			return 'manage_posts_custom_column';
		}
		if ( $post_type === 'page' ) {
			return 'manage_pages_custom_column';
		}

		return "manage_{$post_type}_posts_custom_column";
	}

	private function sortableColumnsHook( string $post_type ): string {
		if ( $post_type === 'post' ) {
			return 'manage_edit-post_sortable_columns';
		}
		if ( $post_type === 'page' ) {
			return 'manage_edit-page_sortable_columns';
		}

		return "manage_edit-{$post_type}_sortable_columns";
	}

	/**
	 * @param array<string, string> $columns Existing columns.
	 * @return array<string, string>
	 */
	public function addIdColumn( array $columns ): array {
		if ( isset( $columns[ self::COLUMN_NAME ] ) ) {
			return $columns;
		}

		$new      = [];
		$inserted = false;
		$anchors  = [ 'title', 'name', 'username', 'comment' ];

		foreach ( $columns as $key => $label ) {
			if ( ! $inserted && in_array( $key, $anchors, true ) ) {
				$new[ self::COLUMN_NAME ] = __( 'ID', 'wppowersuite-lite' );
				$inserted                 = true;
			}

			$new[ $key ] = $label;
		}

		if ( ! $inserted ) {
			$new[ self::COLUMN_NAME ] = __( 'ID', 'wppowersuite-lite' );
		}

		return $new;
	}

	public function displayPostId( string $column_name, $post_id ): void {
		if ( $column_name === self::COLUMN_NAME ) {
			echo '<strong>' . esc_html( (string) (int) $post_id ) . '</strong>';
		}
	}

	public function displayTermId( string $content, string $column_name, $term_id ): string {
		if ( $column_name === self::COLUMN_NAME ) {
			return '<strong>' . esc_html( (string) (int) $term_id ) . '</strong>';
		}

		return $content;
	}

	/**
	 * @param array<string, string> $columns Sortable columns.
	 * @return array<string, string>
	 */
	public function makeIdColumnSortable( array $columns ): array {
		$columns[ self::COLUMN_NAME ] = 'ID';

		return $columns;
	}

	public function addColumnStyles(): void {
		?>
		<?php ob_start(); ?>
			.wp-list-table .column-<?php echo esc_attr( self::COLUMN_NAME ); ?>,
			.wp-list-table th.column-<?php echo esc_attr( self::COLUMN_NAME ); ?>,
			.wp-list-table td.column-<?php echo esc_attr( self::COLUMN_NAME ); ?> {
				width: 60px;
				max-width: 60px;
				text-align: center;
			}
			.wp-list-table .column-<?php echo esc_attr( self::COLUMN_NAME ); ?> strong {
				font-weight: 600;
			}
			.row-actions .wpps-list-table-item-id {
				color: #50575e;
			}
<?php \WP_PowerSuite\Core\InlineAssets::style( 'wpps-ia-modules-show-ids-module-php-style-1', (string) ob_get_clean() ); ?>
		<?php
	}

	/**
	 * @param array<string, mixed> $settings
	 * @param string               $module_id
	 * @param string               $context
	 * @return array<string, mixed>
	 */
	public function filterSanitizeModuleSettings( array $settings, string $module_id, string $context ): array {
		unset( $context );

		if ( $module_id !== self::MODULE_ID ) {
			return $settings;
		}

		$existing = $this->moduleSettings()->all( [] );
		$existing = is_array( $existing ) ? $existing : [];

		return FeatureGate::preservePremiumSettingsOnSave( $settings, $existing );
	}

	public function onSettingsSaved( string $module_id, array $settings ): void {
		unset( $settings );

		if ( $module_id === self::MODULE_ID ) {
			$this->settings_cache = null;
		}
	}

	/**
	 * @return array<string, mixed>
	 */
	public function getSettings(): array {
		if ( $this->settings_cache !== null ) {
			return $this->settings_cache;
		}

		$settings = $this->moduleSettings()->all( [] );
		$settings = is_array( $settings ) ? $settings : [];
		$defaults = FeatureGate::getDefaultSettings();

		$merged = array_merge( $defaults, $settings );

		if ( ! is_array( $merged['enabled_post_types'] ) ) {
			$merged['enabled_post_types'] = $defaults['enabled_post_types'];
		}

		$this->settings_cache = FeatureGate::applyPremiumTierLimits( $merged );

		return $this->settings_cache;
	}
}
