<?php
use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

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

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

if ( ! isset( $module_id ) || empty( $module_id ) ) {
	$module_id = 'custom-excerpt-length';
}

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

$defaults = [
	'excerpt_length' => 55,
	'read_more_text' => '',
];
$settings = array_merge( $defaults, $settings );

$excerpt_length = isset( $settings['excerpt_length'] ) ? max( 0, min( 500, (int) $settings['excerpt_length'] ) ) : 55;
$read_more_text = $settings['read_more_text'] ?? '';
?>

<div class="wp-powersuite-module-settings">
	<p class="description">
		<?php esc_html_e( 'Control excerpt length and read-more text for archive, search, and other views that use the_excerpt().', '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" role="presentation">
			<tbody>
				<tr>
					<th scope="row">
						<label for="<?php echo esc_attr( $module_id ); ?>_excerpt_length"><?php esc_html_e( 'Excerpt length', 'wp-powersuite' ); ?></label>
					</th>
					<td>
						<input
							type="number"
							id="<?php echo esc_attr( $module_id ); ?>_excerpt_length"
							name="excerpt_length"
							value="<?php echo esc_attr( $excerpt_length ); ?>"
							min="0"
							max="500"
							step="1"
							class="small-text wp-powersuite-advanced-setting"
							data-setting-key="excerpt_length"
						/>
						<span class="description"><?php esc_html_e( 'words, 0–500 (0 = theme / core default)', 'wp-powersuite' ); ?></span>
						<p class="description">
							<?php esc_html_e( 'WordPress default is 55. Manual excerpts are not shortened by this setting.', 'wp-powersuite' ); ?>
						</p>
					</td>
				</tr>
				<tr>
					<th scope="row">
						<label for="<?php echo esc_attr( $module_id ); ?>_read_more_text"><?php esc_html_e( 'Read more text', 'wp-powersuite' ); ?></label>
					</th>
					<td>
						<input
							type="text"
							id="<?php echo esc_attr( $module_id ); ?>_read_more_text"
							name="read_more_text"
							value="<?php echo esc_attr( $read_more_text ); ?>"
							class="regular-text wp-powersuite-advanced-setting"
							data-setting-key="read_more_text"
							placeholder="<?php esc_attr_e( 'e.g. ... %link%', 'wp-powersuite' ); ?>"
						/>
						<p class="description">
							<?php esc_html_e( 'Text after auto-generated excerpts. Use %link% for a Read More link. Leave empty for the default ellipsis.', 'wp-powersuite' ); ?>
						</p>
					</td>
				</tr>
			</tbody>
		</table>

		<p class="description"><strong><?php esc_html_e( 'Examples', 'wp-powersuite' ); ?>:</strong></p>
		<ul class="description">
			<li><code>...</code> — <?php esc_html_e( 'plain suffix', 'wp-powersuite' ); ?></li>
			<li><code>... %link%</code> — <?php esc_html_e( 'suffix plus link', 'wp-powersuite' ); ?></li>
		</ul>
	</form>
</div>
