<?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.', 'wp-powersuite' ); ?>
				<a href="<?php echo esc_url( $license_page_url ); ?>"><?php esc_html_e( 'Activate license', 'wp-powersuite' ); ?></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 );

$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.', 'wp-powersuite' ); ?>
	</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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: sign-in logging', 'wp-powersuite' ),
								__( 'Choose whether successful logins, logouts, failed attempts, and Login Session Manager revocations are written to the activity log.', 'wp-powersuite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<?php $al_chk( 'log_logins', __( 'Successful logins', 'wp-powersuite' ), $settings ); ?>
								<?php $al_chk( 'log_logouts', __( 'Logouts', 'wp-powersuite' ), $settings ); ?>
								<?php $al_chk( 'log_failed_logins', __( 'Failed logins', 'wp-powersuite' ), $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)', 'wp-powersuite' ); ?>
								</label>
								<?php if ( ! $login_session_manager_enabled ) : ?>
									<p class="description"><?php esc_html_e( 'Requires the Login Session Manager module to be enabled.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: content logging', 'wp-powersuite' ),
								__( 'Tracks edits and deletes to posts and pages, media uploads, and comments.', 'wp-powersuite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<?php $al_chk( 'log_post_edits', __( 'Posts & pages (save)', 'wp-powersuite' ), $settings ); ?>
								<?php $al_chk( 'log_post_deletes', __( 'Posts & pages (delete)', 'wp-powersuite' ), $settings ); ?>
								<?php $al_chk( 'log_media_uploads', __( 'Media library', 'wp-powersuite' ), $settings ); ?>
								<?php $al_chk( 'log_comments', __( 'Comments', 'wp-powersuite' ), $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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: code & core logging', 'wp-powersuite' ),
								__( 'Plugins, themes, core updates, and selected high-impact site options.', 'wp-powersuite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<?php $al_chk( 'log_plugin_changes', __( 'Plugins', 'wp-powersuite' ), $settings ); ?>
								<?php $al_chk( 'log_theme_changes', __( 'Themes', 'wp-powersuite' ), $settings ); ?>
								<?php $al_chk( 'log_core_updates', __( 'WordPress core updates', 'wp-powersuite' ), $settings ); ?>
								<?php $al_chk( 'log_settings_changes', __( 'Key site options', 'wp-powersuite' ), $settings ); ?>
							</div>
							<p class="description"><?php esc_html_e( 'Key site options include title, URLs, admin email, registration, and default role.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: structure logging', 'wp-powersuite' ),
								__( 'Widgets, menus, taxonomy changes, and permalink structure updates.', 'wp-powersuite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<?php $al_chk( 'log_widget_changes', __( 'Widgets', 'wp-powersuite' ), $settings ); ?>
								<?php $al_chk( 'log_menu_changes', __( 'Menus', 'wp-powersuite' ), $settings ); ?>
								<?php $al_chk( 'log_taxonomy_changes', __( 'Terms & taxonomies', 'wp-powersuite' ), $settings ); ?>
								<?php $al_chk( 'log_permalink_changes', __( 'Permalink structure', 'wp-powersuite' ), $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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: user logging', 'wp-powersuite' ),
								__( 'Registration, profile edits, role changes, and account deletion.', 'wp-powersuite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<?php $al_chk( 'log_user_changes', __( 'Registration & profile', 'wp-powersuite' ), $settings ); ?>
								<?php $al_chk( 'log_role_changes', __( 'Role changes', 'wp-powersuite' ), $settings ); ?>
								<?php $al_chk( 'log_user_deletes', __( 'Account deletion', 'wp-powersuite' ), $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', 'wp-powersuite' ); ?></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)', 'wp-powersuite' ); ?>
									</label>
									<?php
									wpps_render_modal_settings_tip(
										__( 'Help: retention', 'wp-powersuite' ),
										__( 'How many days of events to keep before they can be removed.', 'wp-powersuite' )
									);
									?>
								</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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?>
									</label>
									<p class="description"><?php esc_html_e( 'Automatic cleanup is always enabled to prevent unbounded database growth.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: privacy & network', 'wp-powersuite' ),
								__( 'Optional geolocation, trusting proxy headers (only behind a trusted proxy), and hiding noisy internal event types in the admin list.', 'wp-powersuite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<?php $al_chk( 'enable_ip_geolocation', __( 'Country lookup (third-party API)', 'wp-powersuite' ), $settings ); ?>
								<?php $al_chk( 'trust_proxy_headers', __( 'Trust proxy IP headers', 'wp-powersuite' ), $settings ); ?>
								<?php $al_chk( 'exclude_internal_types_in_list', __( 'Hide internal types in list & export', 'wp-powersuite' ), $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.', 'wp-powersuite' ); ?></p>
							<p class="description"><?php esc_html_e( 'Developer filter: wpps_activity_log_geo_api_url', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></a>
		</p>
	</form>
</div>
