<?php
/**
 * Last Login Column Module
 *
 * @package LastLoginColumn
 * @since 1.0.0
 */

namespace LastLoginColumn;

use WP_PowerSuite\Core\AbstractModule;
use WP_PowerSuite\Core\Contracts\DataLocationsInterface;
use WP_User;

class Module extends AbstractModule implements DataLocationsInterface {

	private const MODULE_ID = 'last-login-column';
	private const MODULE_VERSION = '1.1.1';
	private const META_KEY = 'wpps_last_login_on';

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

	public function getName(): string {
		return __( 'Last Login Column', 'wp-powersuite' );
	}

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

	public function getDescription(): string {
		return __( 'Last login time in the Users table so you can see who has been active recently.', 'wp-powersuite' );
	}

	public function getContext(): string|array {
		return [ 'admin', 'frontend', 'login', 'ajax' ];
	}

	public function getDataLocations(): array {
		return [
			'user_meta' => [ self::META_KEY ],
		];
	}

	protected function registerHooks(): void {
		add_action( 'wp_login', [ $this, 'logLoginDateTime' ], 3, 2 );

		if ( is_admin() ) {
			add_filter( 'manage_users_columns', [ $this, 'addLastLoginColumn' ] );
			add_filter( 'manage_users_custom_column', [ $this, 'showLastLoginInfo' ], 10, 3 );
			add_filter( 'manage_users_sortable_columns', [ $this, 'makeLastLoginColumnSortable' ] );
			add_action( 'pre_get_users', [ $this, 'sortByLastLogin' ] );
			add_action( 'admin_print_styles-users.php', [ $this, 'addColumnStyle' ] );
		}
	}

	public function logLoginDateTime( string $user_login, ?WP_User $user = null ): void {
		if ( ! $user instanceof WP_User || $user->ID < 1 ) {
			$user = get_user_by( 'login', $user_login );
		}

		if ( $user instanceof WP_User && $user->ID > 0 ) {
			update_user_meta( $user->ID, self::META_KEY, time() );
		}
	}

	public function addLastLoginColumn( array $columns ): array {
		$columns['wpps_last_login'] = __( 'Last Login', 'wp-powersuite' );
		return $columns;
	}

	public function showLastLoginInfo( string $output, string $column_name, int $user_id ): string {
		if ( 'wpps_last_login' !== $column_name ) {
			return $output;
		}

		$last_login_unixtime = get_user_meta( $user_id, self::META_KEY, true );
		$last_login_unixtime = is_numeric( $last_login_unixtime ) ? (int) $last_login_unixtime : 0;

		if ( $last_login_unixtime > 0 ) {
			$date_format = get_option( 'date_format', 'F j, Y' );
			$time_format = get_option( 'time_format', 'g:i a' );

			if ( function_exists( 'wp_date' ) ) {
				$output = wp_date( $date_format . ' ' . $time_format, $last_login_unixtime );
			} else {
				$output = date_i18n( $date_format . ' ' . $time_format, $last_login_unixtime );
			}
			$output = esc_html( $output );
		} else {
			$output = esc_html( __( 'Never', 'wp-powersuite' ) );
		}

		return $output;
	}

	public function makeLastLoginColumnSortable( array $sortable_columns ): array {
		$sortable_columns['wpps_last_login'] = 'wpps_last_login';
		return $sortable_columns;
	}

	public function sortByLastLogin( \WP_User_Query $query ): void {
		if ( ! $query->get( 'orderby' ) || 'wpps_last_login' !== $query->get( 'orderby' ) ) {
			return;
		}

		$query->set( 'meta_key', self::META_KEY );
		$query->set( 'orderby', 'meta_value_num' );
		$query->set( 'meta_type', 'NUMERIC' );
	}

	public function addColumnStyle(): void {
		?>
		<style>
			.column-wpps_last_login {
				min-width: 160px;
			}
		</style>
		<?php
	}
}
