<?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 ?? 'media-rename-manager' ); ?>">
		<div class="notice notice-warning inline" style="margin:0;">
			<p>
				<?php esc_html_e( 'Media Rename Manager requires an active license. Activate your license to rename files and update references across your site.', '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 ( ! \WP_PowerSuite\Core\NetworkScope::canManagePlugin() ) {
	return;
}

$module_id = $module_id ?? 'media-rename-manager';

$settings     = \WP_PowerSuite\Core\OptionStoreModuleSettingsRepository::forModule( $module_id )->all( [] );
if ( ! is_array( $settings ) ) {
	$settings = [];
}

$default_pattern     = isset( $settings['default_pattern'] ) && is_string( $settings['default_pattern'] ) ? $settings['default_pattern'] : '{title}';
$case_transformation = isset( $settings['case_transformation'] ) ? $settings['case_transformation'] : 'lowercase';
$max_filename_length = isset( $settings['max_filename_length'] ) ? max( 10, min( 200, (int) $settings['max_filename_length'] ) ) : 50;
$excluded_file_types = isset( $settings['excluded_file_types'] ) && is_string( $settings['excluded_file_types'] ) ? $settings['excluded_file_types'] : '';
$add_unique_id       = ! empty( $settings['add_unique_id_suffix'] );
$create_redirects    = ! empty( $settings['create_redirects'] );
$preserve_accents    = ! empty( $settings['preserve_accents'] );

$redirect_manager_active = \WP_PowerSuite\Core\OptionStoreModuleSettingsRepository::isModuleEnabled( 'redirect-manager' );
?>

<div class="wp-powersuite-module-settings" data-module-id="<?php echo esc_attr( $module_id ); ?>">
	<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="media-rename-default-pattern"><?php esc_html_e( 'Default renaming pattern', 'wppowersuite-lite' ); ?></label>
					</th>
					<td>
						<input type="text" name="default_pattern" id="media-rename-default-pattern" value="<?php echo esc_attr( $default_pattern ); ?>" class="regular-text wp-powersuite-advanced-setting" data-setting-key="default_pattern" />
						<p class="description"><?php esc_html_e( 'Applies when renaming files and on new uploads. Variables: {title}, {post-title}, {id}, {date}, {counter}', 'wppowersuite-lite' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row">
						<label for="media-rename-case"><?php esc_html_e( 'Case transformation', 'wppowersuite-lite' ); ?></label>
					</th>
					<td>
						<select name="case_transformation" id="media-rename-case" class="wp-powersuite-advanced-setting" data-setting-key="case_transformation">
							<option value="lowercase" <?php selected( $case_transformation, 'lowercase' ); ?>><?php esc_html_e( 'Lowercase', 'wppowersuite-lite' ); ?></option>
							<option value="uppercase" <?php selected( $case_transformation, 'uppercase' ); ?>><?php esc_html_e( 'Uppercase', 'wppowersuite-lite' ); ?></option>
							<option value="titlecase" <?php selected( $case_transformation, 'titlecase' ); ?>><?php esc_html_e( 'Title Case', 'wppowersuite-lite' ); ?></option>
							<option value="nochange" <?php selected( $case_transformation, 'nochange' ); ?>><?php esc_html_e( 'No change', 'wppowersuite-lite' ); ?></option>
						</select>
						<p class="description"><?php esc_html_e( 'Applies when renaming files and on new uploads.', 'wppowersuite-lite' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row">
						<label for="media-rename-max-length"><?php esc_html_e( 'Maximum filename length', 'wppowersuite-lite' ); ?></label>
					</th>
					<td>
						<input type="number" name="max_filename_length" id="media-rename-max-length" value="<?php echo esc_attr( (string) $max_filename_length ); ?>" min="10" max="200" class="small-text wp-powersuite-advanced-setting" data-setting-key="max_filename_length" />
						<p class="description"><?php esc_html_e( 'Characters (excluding extension). Applies when renaming files and on new uploads.', 'wppowersuite-lite' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row">
						<label for="media-rename-excluded"><?php esc_html_e( 'Excluded file types', 'wppowersuite-lite' ); ?></label>
					</th>
					<td>
						<input type="text" name="excluded_file_types" id="media-rename-excluded" value="<?php echo esc_attr( $excluded_file_types ); ?>" class="regular-text wp-powersuite-advanced-setting" data-setting-key="excluded_file_types" placeholder="pdf,zip,doc" />
						<p class="description"><?php esc_html_e( 'Comma-separated extensions to skip. Single rename: error; bulk: skipped; uploads: left unchanged.', 'wppowersuite-lite' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row">
						<label for="media-rename-unique-id"><?php esc_html_e( 'Add unique ID suffix', 'wppowersuite-lite' ); ?></label>
					</th>
					<td>
						<label for="media-rename-unique-id">
							<input type="checkbox" name="add_unique_id_suffix" id="media-rename-unique-id" value="1" <?php checked( $add_unique_id ); ?> class="wp-powersuite-advanced-setting" data-setting-key="add_unique_id_suffix" />
							<?php esc_html_e( 'Append a 3-character random ID (e.g. -a3f) when renaming or uploading files', 'wppowersuite-lite' ); ?>
						</label>
					</td>
				</tr>
				<tr>
					<th scope="row">
						<label for="media-rename-redirects"><?php esc_html_e( 'Create automatic redirects', 'wppowersuite-lite' ); ?></label>
					</th>
					<td>
						<label for="media-rename-redirects">
							<input type="checkbox" name="create_redirects" id="media-rename-redirects" value="1" <?php checked( $create_redirects ); ?> class="wp-powersuite-advanced-setting" data-setting-key="create_redirects" <?php disabled( ! $redirect_manager_active ); ?> />
							<?php esc_html_e( 'Create 301 redirect from old URL to new URL', 'wppowersuite-lite' ); ?>
						</label>
						<?php if ( ! $redirect_manager_active ) : ?>
							<p class="description" style="color:#d63638;"><?php esc_html_e( 'Requires the Redirect Manager module to be enabled.', 'wppowersuite-lite' ); ?></p>
						<?php endif; ?>
					</td>
				</tr>
				<tr>
					<th scope="row">
						<label for="media-rename-preserve-accents"><?php esc_html_e( 'Preserve accented characters', 'wppowersuite-lite' ); ?></label>
					</th>
					<td>
						<label for="media-rename-preserve-accents">
							<input type="checkbox" name="preserve_accents" id="media-rename-preserve-accents" value="1" <?php checked( $preserve_accents ); ?> class="wp-powersuite-advanced-setting" data-setting-key="preserve_accents" />
							<?php esc_html_e( 'Keep é, ñ, ü instead of converting to e, n, u', 'wppowersuite-lite' ); ?>
						</label>
					</td>
				</tr>
			</tbody>
		</table>
	</form>
</div>
