<?php
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 ?? 'database-cleanup' ); ?>">
		<div class="notice notice-warning inline" style="margin:0;">
			<p>
				<?php esc_html_e( 'Database Cleanup requires an active license. Activate your license to scan and clean your database.', '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;
}

use DatabaseCleanup\Module;
use WP_PowerSuite\Core\NetworkScope;
use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

if ( ! isset( $module_id ) || empty( $module_id ) ) {
	$module_id = Module::MODULE_ID;
}

$settings   = OptionStoreModuleSettingsRepository::forModule( $module_id )->all( Module::getDefaultSettings() );
$page_url   = NetworkScope::adminUrl( 'admin.php', [ 'page' => 'powersuite-database-cleanup' ] );
?>

<div class="wp-powersuite-module-settings" data-module-id="<?php echo esc_attr( $module_id ); ?>">
	<p class="description">
		<?php esc_html_e( 'Database Cleanup runs from its dedicated admin screen with section navigation, entity browsers, batch cleanup, and history.', 'wp-powersuite' ); ?>
	</p>
	<p>
		<a href="<?php echo esc_url( $page_url ); ?>" class="button button-primary">
			<?php esc_html_e( 'Open Database Cleanup', 'wp-powersuite' ); ?>
		</a>
	</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' ); ?>
		<table class="form-table" role="presentation">
			<tbody>
				<tr>
					<th scope="row"><label for="<?php echo esc_attr( $module_id ); ?>_revision_keep"><?php esc_html_e( 'Revisions to keep', 'wp-powersuite' ); ?></label></th>
					<td>
						<input
							type="number"
							min="0"
							max="50"
							class="small-text wp-powersuite-advanced-setting"
							data-setting-key="revision_keep_count"
							id="<?php echo esc_attr( $module_id ); ?>_revision_keep"
							name="<?php echo esc_attr( $module_id ); ?>[revision_keep_count]"
							value="<?php echo esc_attr( (string) (int) ( $settings['revision_keep_count'] ?? 5 ) ); ?>"
						/>
					</td>
				</tr>
				<tr>
					<th scope="row"><label for="<?php echo esc_attr( $module_id ); ?>_backup_warn"><?php esc_html_e( 'Backup warning (days)', 'wp-powersuite' ); ?></label></th>
					<td>
						<input
							type="number"
							min="0"
							max="90"
							class="small-text wp-powersuite-advanced-setting"
							data-setting-key="backup_warn_days"
							id="<?php echo esc_attr( $module_id ); ?>_backup_warn"
							name="<?php echo esc_attr( $module_id ); ?>[backup_warn_days]"
							value="<?php echo esc_attr( (string) (int) ( $settings['backup_warn_days'] ?? 7 ) ); ?>"
						/>
					</td>
				</tr>
			</tbody>
		</table>
	</form>
</div>
