<?php
/**
 * Ghost Purge Module.
 *
 * Module 6 — bulk-delete inactive users (a specific role, last login > N
 * months ago) in batched AJAX so the request never times out.
 *
 * Tracks last-login via a `wp_login` action that writes a user meta
 * `_platform_helm_last_login`. For users who registered before the plugin was
 * active, `user_registered` is the conservative fallback.
 *
 * @package Platform\Helm\Modules\GhostPurge
 */

declare( strict_types = 1 );

namespace Platform\Helm\Modules\GhostPurge;

use Platform\Helm\Contracts\ModuleInterface;
use Platform\Helm\Core\Assets;

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

final class GhostPurgeModule implements ModuleInterface {

	public const LAST_LOGIN_META  = '_platform_helm_last_login';
	public const BATCH_SIZE       = 25;
	public const JOB_TRANSIENT    = 'platform_helm_gp_job_';
	public const JOB_TTL_SECONDS  = 3600;
	public const DEFAULT_MONTHS   = 12;

	private UserFinder $finder;
	private AjaxController $ajax;
	private AdminUI $ui;

	public function __construct( Assets $assets ) {
		$this->finder = new UserFinder();
		$this->ajax   = new AjaxController( $this->finder );
		$this->ui     = new AdminUI( $assets );
	}

	public function id(): string {
		return 'ghost_purge';
	}

	public function name(): string {
		return __( 'Ghost Purge', 'platform-helm' );
	}

	public function description(): string {
		return __( 'Bulk-delete users in a given role who have not logged in for N months. Batched, dry-run-first, reassigns content to admin.', 'platform-helm' );
	}

	public function default_enabled(): bool {
		return false; // Destructive; opt-in.
	}

	public function boot(): void {
		add_action( 'wp_login', [ $this, 'record_login' ], 10, 2 );
		$this->ui->register_hooks();
		$this->ajax->register_hooks();
	}

	public function record_login( string $user_login, \WP_User $user ): void {
		update_user_meta( $user->ID, self::LAST_LOGIN_META, time() );
	}
}
