<?php
/**
 * Event Collector.
 *
 * Wires WordPress + Helm events to the ActivityLogger. Detailed by default:
 * logins/logouts, post publish/update/trash, settings changes (Helm's own),
 * user role changes, and Helm link edits (Architect insert/restore).
 *
 * Each hook is lightweight and guarded; logging never blocks the action.
 *
 * @package Platform\Helm\Modules\Activity_Log
 */

declare( strict_types = 1 );

namespace Platform\Helm\Modules\Activity_Log;

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

final class EventCollector {

	public function __construct( private ActivityLogger $logger ) {}

	public function register_hooks(): void {
		// Auth.
		add_action( 'wp_login', [ $this, 'on_login' ], 10, 2 );
		add_action( 'wp_logout', [ $this, 'on_logout' ] );

		// Content.
		add_action( 'transition_post_status', [ $this, 'on_post_transition' ], 10, 3 );
		add_action( 'before_delete_post', [ $this, 'on_post_delete' ] );

		// Users / roles.
		add_action( 'set_user_role', [ $this, 'on_role_change' ], 10, 3 );
		add_action( 'user_register', [ $this, 'on_user_register' ] );

		// Helm's own link edits.
		add_action( 'platform_helm_link_inserted', [ $this, 'on_link_inserted' ], 10, 3 );
		add_action( 'platform_helm_link_restored', [ $this, 'on_link_restored' ], 10, 1 );
	}

	public function on_login( string $user_login, $user ): void {
		$this->logger->log( 'login', sprintf( __( '%s logged in.', 'platform-helm' ), $user_login ), 'user', is_object( $user ) ? (int) $user->ID : 0 );
	}

	public function on_logout( int $user_id = 0 ): void {
		$this->logger->log( 'logout', __( 'User logged out.', 'platform-helm' ), 'user', $user_id );
	}

	public function on_post_transition( string $new_status, string $old_status, $post ): void {
		if ( ! is_object( $post ) ) {
			return;
		}
		// Ignore auto-drafts and revisions noise.
		if ( in_array( $post->post_type, [ 'revision', 'nav_menu_item', 'customize_changeset', 'oembed_cache' ], true ) ) {
			return;
		}
		if ( 'auto-draft' === $new_status || ( $new_status === $old_status && 'publish' !== $new_status ) ) {
			return;
		}
		if ( 'publish' === $new_status && 'publish' !== $old_status ) {
			$action = 'post_published';
			$desc   = sprintf( __( 'Published "%s".', 'platform-helm' ), get_the_title( $post ) );
		} elseif ( 'trash' === $new_status ) {
			$action = 'post_trashed';
			$desc   = sprintf( __( 'Trashed "%s".', 'platform-helm' ), get_the_title( $post ) );
		} elseif ( 'publish' === $new_status ) {
			$action = 'post_updated';
			$desc   = sprintf( __( 'Updated "%s".', 'platform-helm' ), get_the_title( $post ) );
		} else {
			return;
		}
		$this->logger->log( $action, $desc, (string) $post->post_type, (int) $post->ID );
	}

	public function on_post_delete( int $post_id ): void {
		$post = get_post( $post_id );
		if ( ! $post || in_array( $post->post_type, [ 'revision', 'nav_menu_item' ], true ) ) {
			return;
		}
		$this->logger->log( 'post_deleted', sprintf( __( 'Permanently deleted "%s".', 'platform-helm' ), get_the_title( $post ) ), (string) $post->post_type, $post_id );
	}

	public function on_role_change( int $user_id, string $role, array $old_roles ): void {
		$user = get_userdata( $user_id );
		$name = $user ? $user->display_name : ( '#' . $user_id );
		$this->logger->log(
			'role_changed',
			sprintf( __( 'Role for %1$s changed to "%2$s".', 'platform-helm' ), $name, $role ),
			'user',
			$user_id
		);
	}

	public function on_user_register( int $user_id ): void {
		$user = get_userdata( $user_id );
		$name = $user ? $user->user_login : ( '#' . $user_id );
		$this->logger->log( 'user_registered', sprintf( __( 'New user registered: %s.', 'platform-helm' ), $name ), 'user', $user_id );
	}

	public function on_link_inserted( int $post_id, string $phrase, string $url ): void {
		$this->logger->log(
			'link_inserted',
			sprintf( __( 'Inserted internal link "%1$s" in "%2$s".', 'platform-helm' ), $phrase, get_the_title( $post_id ) ),
			'post',
			$post_id
		);
	}

	public function on_link_restored( int $post_id ): void {
		$this->logger->log( 'link_restored', sprintf( __( 'Restored "%s" from a link backup.', 'platform-helm' ), get_the_title( $post_id ) ), 'post', $post_id );
	}

	/**
	 * Helper other modules can call to log a settings change.
	 */
	public function log_settings_change( string $what ): void {
		$this->logger->log( 'settings_changed', sprintf( __( 'Settings changed: %s.', 'platform-helm' ), $what ), 'settings', 0 );
	}
}
