<?php
namespace AdvancedUserActivityLog;

use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

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

if ( ! function_exists( 'wp_powersuite_is_license_valid' ) || ! wp_powersuite_is_license_valid() ) {
	$license_page_url = admin_url( 'admin.php?page=powersuite-license' );
	?>
	<div class="wp-powersuite-module-settings wpps-settings-panel" data-module-id="<?php echo esc_attr( $module_id ?? 'advanced-user-activity-log' ); ?>">
		<div class="notice notice-warning inline" style="margin:0;">
			<p>
				<?php esc_html_e( 'Activity Log requires an active license. Activate your license to configure logging and view the dashboard.', 'wppowersuite-lite' ); ?>
				<a href="<?php echo esc_url( $license_page_url ); ?>"><?php esc_html_e( 'Activate license', 'wppowersuite-lite' ); ?></a>
			</p>
		</div>
	</div>
	<?php
	return;
}

if ( ! isset( $module_id ) ) {
	$module_id = 'advanced-user-activity-log';
}

$default_settings = [
	'log_logins'                     => true,
	'log_logouts'                    => true,
	'log_failed_logins'              => true,
	'log_session_manager'            => true,
	'log_post_edits'                 => true,
	'log_post_deletes'               => true,
	'log_user_changes'               => true,
	'log_user_deletes'               => true,
	'log_plugin_changes'             => true,
	'log_theme_changes'              => true,
	'log_settings_changes'           => false,
	'log_media_uploads'              => true,
	'log_comments'                   => true,
	'log_widget_changes'             => true,
	'log_menu_changes'               => true,
	'log_core_updates'               => true,
	'log_taxonomy_changes'           => true,
	'log_permalink_changes'          => true,
	'log_role_changes'               => true,
	'retention_days'                 => 90,
	'auto_cleanup'                   => true,
	'enable_ip_geolocation'          => false,
	'trust_proxy_headers'            => false,
	'exclude_internal_types_in_list' => true,
];

$settings = OptionStoreModuleSettingsRepository::forModule( $module_id )->all( [] );
if ( ! is_array( $settings ) ) {
	$settings = [];
}
$settings = array_merge( $default_settings, $settings );
$settings['retention_days'] = max( 1, min( 365, (int) ( $settings['retention_days'] ?? 90 ) ) );
$settings['auto_cleanup']   = true;

$login_session_manager_enabled = OptionStoreModuleSettingsRepository::isModuleEnabled( 'login-session-manager' );
if ( ! $login_session_manager_enabled ) {
	$settings['log_session_manager'] = false;
}

$uid = preg_replace( '/[^a-z0-9_-]/i', '', $module_id );

/**
 * @param array<string, mixed> $settings
 */
$al_chk = static function ( string $key, string $label, array $settings ) : void {
	$on = ! empty( $settings[ $key ] );
	?>
	<label>
		<input type="checkbox"
			name="settings[<?php echo esc_attr( $key ); ?>]"
			class="wp-powersuite-advanced-setting"
			data-setting-key="<?php echo esc_attr( $key ); ?>"
			value="1"
			<?php checked( $on ); ?>
		/>
		<?php echo esc_html( $label ); ?>
	</label>
	<?php
};
?>

<div class="wp-powersuite-module-settings wpps-settings-panel wpps-settings-panel--accordion-stack" data-module-id="<?php echo esc_attr( $module_id ); ?>">
	<p class="description" style="margin-top:0;">
		<?php esc_html_e( 'Choose which events are stored in the activity log. Retention and privacy options are in the sections below.', 'wppowersuite-lite' ); ?>
	</p>

	<form class="wp-powersuite-module-settings-form" data-module-id="<?php echo esc_attr( $module_id ); ?>">
		<?php wp_nonce_field( 'wp_powersuite_save_module_settings_' . $module_id, 'wp_powersuite_nonce' ); ?>

		<div class="wpps-ts-accordions">
			<div class="wpps-ts-accordion is-open">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_signin" aria-expanded="true" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_signin">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'Sign-in', 'wppowersuite-lite' ); ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_signin" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_signin">
					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<span><?php esc_html_e( 'Authentication events', 'wppowersuite-lite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: sign-in logging', 'wppowersuite-lite' ),
								__( 'Choose whether successful logins, logouts, failed attempts, and Login Session Manager revocations are written to the activity log.', 'wppowersuite-lite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<?php $al_chk( 'log_logins', __( 'Successful logins', 'wppowersuite-lite' ), $settings ); ?>
								<?php $al_chk( 'log_logouts', __( 'Logouts', 'wppowersuite-lite' ), $settings ); ?>
								<?php $al_chk( 'log_failed_logins', __( 'Failed logins', 'wppowersuite-lite' ), $settings ); ?>
								<label<?php echo $login_session_manager_enabled ? '' : ' style="opacity:0.55;color:#646970;"'; ?>>
									<input type="checkbox"
										name="settings[log_session_manager]"
										class="wp-powersuite-advanced-setting"
										data-setting-key="log_session_manager"
										value="1"
										<?php checked( $login_session_manager_enabled && ! empty( $settings['log_session_manager'] ) ); ?>
										<?php disabled( ! $login_session_manager_enabled ); ?>
									/>
									<?php esc_html_e( 'Login Session Manager (revocations & limits)', 'wppowersuite-lite' ); ?>
								</label>
								<?php if ( ! $login_session_manager_enabled ) : ?>
									<p class="description"><?php esc_html_e( 'Requires the Login Session Manager module to be enabled.', 'wppowersuite-lite' ); ?></p>
								<?php endif; ?>
							</div>
						</div>
					</div>
				</div>
			</div>

			<div class="wpps-ts-accordion">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_content" aria-expanded="false" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_content">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'Content', 'wppowersuite-lite' ); ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_content" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_content">
					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<span><?php esc_html_e( 'Posts, media, and comments', 'wppowersuite-lite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: content logging', 'wppowersuite-lite' ),
								__( 'Tracks edits and deletes to posts and pages, media uploads, and comments.', 'wppowersuite-lite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<?php $al_chk( 'log_post_edits', __( 'Posts & pages (save)', 'wppowersuite-lite' ), $settings ); ?>
								<?php $al_chk( 'log_post_deletes', __( 'Posts & pages (delete)', 'wppowersuite-lite' ), $settings ); ?>
								<?php $al_chk( 'log_media_uploads', __( 'Media library', 'wppowersuite-lite' ), $settings ); ?>
								<?php $al_chk( 'log_comments', __( 'Comments', 'wppowersuite-lite' ), $settings ); ?>
							</div>
						</div>
					</div>
				</div>
			</div>

			<div class="wpps-ts-accordion">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_code" aria-expanded="false" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_code">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'Code & core', 'wppowersuite-lite' ); ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_code" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_code">
					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<span><?php esc_html_e( 'Plugins, themes, and core', 'wppowersuite-lite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: code & core logging', 'wppowersuite-lite' ),
								__( 'Plugins, themes, core updates, and selected high-impact site options.', 'wppowersuite-lite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<?php $al_chk( 'log_plugin_changes', __( 'Plugins', 'wppowersuite-lite' ), $settings ); ?>
								<?php $al_chk( 'log_theme_changes', __( 'Themes', 'wppowersuite-lite' ), $settings ); ?>
								<?php $al_chk( 'log_core_updates', __( 'WordPress core updates', 'wppowersuite-lite' ), $settings ); ?>
								<?php $al_chk( 'log_settings_changes', __( 'Key site options', 'wppowersuite-lite' ), $settings ); ?>
							</div>
							<p class="description"><?php esc_html_e( 'Key site options include title, URLs, admin email, registration, and default role.', 'wppowersuite-lite' ); ?></p>
						</div>
					</div>
				</div>
			</div>

			<div class="wpps-ts-accordion">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_structure" aria-expanded="false" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_structure">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'Structure', 'wppowersuite-lite' ); ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_structure" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_structure">
					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<span><?php esc_html_e( 'Widgets, menus, and taxonomies', 'wppowersuite-lite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: structure logging', 'wppowersuite-lite' ),
								__( 'Widgets, menus, taxonomy changes, and permalink structure updates.', 'wppowersuite-lite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<?php $al_chk( 'log_widget_changes', __( 'Widgets', 'wppowersuite-lite' ), $settings ); ?>
								<?php $al_chk( 'log_menu_changes', __( 'Menus', 'wppowersuite-lite' ), $settings ); ?>
								<?php $al_chk( 'log_taxonomy_changes', __( 'Terms & taxonomies', 'wppowersuite-lite' ), $settings ); ?>
								<?php $al_chk( 'log_permalink_changes', __( 'Permalink structure', 'wppowersuite-lite' ), $settings ); ?>
							</div>
						</div>
					</div>
				</div>
			</div>

			<div class="wpps-ts-accordion">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_users" aria-expanded="false" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_users">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'Users', 'wppowersuite-lite' ); ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_users" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_users">
					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<span><?php esc_html_e( 'Accounts and roles', 'wppowersuite-lite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: user logging', 'wppowersuite-lite' ),
								__( 'Registration, profile edits, role changes, and account deletion.', 'wppowersuite-lite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<?php $al_chk( 'log_user_changes', __( 'Registration & profile', 'wppowersuite-lite' ), $settings ); ?>
								<?php $al_chk( 'log_role_changes', __( 'Role changes', 'wppowersuite-lite' ), $settings ); ?>
								<?php $al_chk( 'log_user_deletes', __( 'Account deletion', 'wppowersuite-lite' ), $settings ); ?>
							</div>
						</div>
					</div>
				</div>
			</div>

			<div class="wpps-ts-accordion">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_retention" aria-expanded="false" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_retention">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'Retention', 'wppowersuite-lite' ); ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_retention" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_retention">
					<table class="form-table" role="presentation">
						<tbody>
							<tr>
								<th scope="row">
									<label for="<?php echo esc_attr( $module_id . '_retention_days' ); ?>">
										<?php esc_html_e( 'Keep logs (days)', 'wppowersuite-lite' ); ?>
									</label>
									<?php
									wpps_render_modal_settings_tip(
										__( 'Help: retention', 'wppowersuite-lite' ),
										__( 'How many days of events to keep before they can be removed.', 'wppowersuite-lite' )
									);
									?>
								</th>
								<td>
									<input type="number"
										id="<?php echo esc_attr( $module_id . '_retention_days' ); ?>"
										name="settings[retention_days]"
										class="wp-powersuite-advanced-setting small-text"
										data-setting-key="retention_days"
										value="<?php echo esc_attr( (string) ( $settings['retention_days'] ?? 90 ) ); ?>"
										min="1"
										max="365"
									/>
								</td>
							</tr>
							<tr>
								<th scope="row"><?php esc_html_e( 'Automatic cleanup', 'wppowersuite-lite' ); ?></th>
								<td>
									<label class="wpps-ts-inline-check">
										<input type="checkbox"
											name="settings[auto_cleanup]"
											class="wp-powersuite-advanced-setting"
											data-setting-key="auto_cleanup"
											value="1"
											checked="checked"
											disabled="disabled"
										/>
										<?php esc_html_e( 'Delete expired entries once per day', 'wppowersuite-lite' ); ?>
									</label>
									<p class="description"><?php esc_html_e( 'Automatic cleanup is always enabled to prevent unbounded database growth.', 'wppowersuite-lite' ); ?></p>
								</td>
							</tr>
						</tbody>
					</table>
				</div>
			</div>

			<div class="wpps-ts-accordion">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_privacy" aria-expanded="false" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_privacy">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'Privacy & network', 'wppowersuite-lite' ); ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_privacy" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_privacy">
					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<span><?php esc_html_e( 'Optional metadata', 'wppowersuite-lite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: privacy & network', 'wppowersuite-lite' ),
								__( 'Optional geolocation, trusting proxy headers (only behind a trusted proxy), and hiding noisy internal event types in the admin list.', 'wppowersuite-lite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<?php $al_chk( 'enable_ip_geolocation', __( 'Country lookup (third-party API)', 'wppowersuite-lite' ), $settings ); ?>
								<?php $al_chk( 'trust_proxy_headers', __( 'Trust proxy IP headers', 'wppowersuite-lite' ), $settings ); ?>
								<?php $al_chk( 'exclude_internal_types_in_list', __( 'Hide internal types in list & export', 'wppowersuite-lite' ), $settings ); ?>
							</div>
							<p class="description"><?php esc_html_e( 'Geolocation uses HTTPS and sends visitor IPs to a third-party service. Trust proxy headers only when a trusted reverse proxy is in front of WordPress.', 'wppowersuite-lite' ); ?></p>
						</div>
					</div>
				</div>
			</div>
		</div>

		<p class="wpps-ts-dashboard-link">
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=wpps-activity-log' ) ); ?>"><?php esc_html_e( 'Open Activity Log', 'wppowersuite-lite' ); ?></a>
		</p>
	</form>
</div>
