<?php
namespace LimitMediaUploadSize;

use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

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

if ( ! \WP_PowerSuite\Core\NetworkScope::canManagePlugin() ) {
	return;
}

if ( ! isset( $module_id ) ) {
	$module_id = 'limit-media-upload-size';
}

$settings            = Module::normalizeSettings( OptionStoreModuleSettingsRepository::forModule( $module_id )->all( [] ) );
$max_upload_mb       = $settings['max_upload_size'];
$effective_max_bytes = Module::getEffectiveMaxBytes( $max_upload_mb );
$upload_max_filesize = ini_get( 'upload_max_filesize' );
$post_max_size       = ini_get( 'post_max_size' );

?>
<div class="wp-powersuite-module-settings">
	<p class="description">
		<?php esc_html_e( 'Set a maximum file size limit for media uploads. This helps prevent large files from consuming server storage space.', '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' ); ?>

		<table class="form-table">
			<tbody>
				<tr>
					<th scope="row">
						<label for="<?php echo esc_attr( $module_id . '_max_upload_size' ); ?>">
							<?php esc_html_e( 'Maximum Upload Size:', 'wp-powersuite' ); ?>
						</label>
					</th>
					<td>
						<input type="number"
							id="<?php echo esc_attr( $module_id . '_max_upload_size' ); ?>"
							name="max_upload_size"
							class="wp-powersuite-advanced-setting"
							data-setting-key="max_upload_size"
							value="<?php echo esc_attr( $max_upload_mb ); ?>"
							min="1"
							max="1000"
							step="1"
						/>
						<span><?php esc_html_e( 'MB', 'wp-powersuite' ); ?></span>
						<p class="description">
							<?php esc_html_e( 'Set the maximum file size allowed for media uploads in megabytes (MB).', 'wp-powersuite' ); ?>
							<br>
							<strong><?php esc_html_e( 'Effective upload limit:', 'wp-powersuite' ); ?></strong>
							<?php echo esc_html( size_format( $effective_max_bytes, 2 ) ); ?>
							<br>
							<?php if ( $upload_max_filesize ) : ?>
								<strong><?php esc_html_e( 'Server upload_max_filesize:', 'wp-powersuite' ); ?></strong> <?php echo esc_html( $upload_max_filesize ); ?>
								<br>
							<?php endif; ?>
							<?php if ( $post_max_size ) : ?>
								<strong><?php esc_html_e( 'Server post_max_size:', 'wp-powersuite' ); ?></strong> <?php echo esc_html( $post_max_size ); ?>
							<?php endif; ?>
						</p>
					</td>
				</tr>
			</tbody>
		</table>

		<p class="description">
			<strong><?php esc_html_e( 'Note:', 'wp-powersuite' ); ?></strong>
			<?php esc_html_e( 'The effective limit is the lower of your module setting and your server\'s PHP upload_max_filesize and post_max_size values. If you need to allow larger uploads, contact your hosting provider.', 'wp-powersuite' ); ?>
		</p>
	</form>
</div>
