<?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 = 'breadcrumbs';
}

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

$defaults = [
	'separator'    => '›',
	'home_text'    => 'Home',
	'auto_display' => 'none',
	'post_types'   => [ 'post', 'page' ],
];
$settings = array_merge( $defaults, $settings );

$separator    = $settings['separator'] ?? '›';
$home_text    = $settings['home_text'] ?? 'Home';
$auto_display = $settings['auto_display'] ?? 'none';
$post_types   = $settings['post_types'] ?? [ 'post', 'page' ];
if ( ! is_array( $post_types ) ) {
	$post_types = [ 'post', 'page' ];
}

$excluded_types   = [ 'attachment', 'revision', 'nav_menu_item', 'custom_css', 'customize_changeset', 'oembed_cache', 'user_request', 'wp_block' ];
$available_types  = get_post_types( [ 'public' => true ], 'objects' );
$available_types  = array_diff_key( $available_types, array_flip( $excluded_types ) );
?>

<div class="wp-powersuite-module-settings">
	<p class="description">
		<?php esc_html_e( 'Add breadcrumb navigation with Schema.org markup for SEO. Use the [breadcrumbs] shortcode or enable auto-display.', '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 ); ?>_separator"><?php esc_html_e( 'Separator', 'wp-powersuite' ); ?></label>
					</th>
					<td>
						<select id="<?php echo esc_attr( $module_id ); ?>_separator" name="separator" class="wp-powersuite-advanced-setting" data-setting-key="separator">
							<option value="›" <?php selected( $separator, '›' ); ?>><?php esc_html_e( '› (Right Arrow)', 'wp-powersuite' ); ?></option>
							<option value="/" <?php selected( $separator, '/' ); ?>><?php esc_html_e( '/ (Slash)', 'wp-powersuite' ); ?></option>
							<option value=">" <?php selected( $separator, '>' ); ?>><?php esc_html_e( '> (Greater Than)', 'wp-powersuite' ); ?></option>
							<option value="→" <?php selected( $separator, '→' ); ?>><?php esc_html_e( '→ (Long Right Arrow)', 'wp-powersuite' ); ?></option>
							<option value="»" <?php selected( $separator, '»' ); ?>><?php esc_html_e( '» (Double Right Arrow)', 'wp-powersuite' ); ?></option>
							<option value="|" <?php selected( $separator, '|' ); ?>><?php esc_html_e( '| (Pipe)', 'wp-powersuite' ); ?></option>
							<option value="·" <?php selected( $separator, '·' ); ?>><?php esc_html_e( '· (Dot)', 'wp-powersuite' ); ?></option>
						</select>
						<p class="description"><?php esc_html_e( 'Character between breadcrumb items.', 'wp-powersuite' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row">
						<label for="<?php echo esc_attr( $module_id ); ?>_home_text"><?php esc_html_e( 'Home Text', 'wp-powersuite' ); ?></label>
					</th>
					<td>
						<input type="text" id="<?php echo esc_attr( $module_id ); ?>_home_text" name="home_text" class="wp-powersuite-advanced-setting regular-text" data-setting-key="home_text" value="<?php echo esc_attr( $home_text ); ?>">
						<p class="description"><?php esc_html_e( 'Label for the home link.', 'wp-powersuite' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row">
						<label for="<?php echo esc_attr( $module_id ); ?>_auto_display"><?php esc_html_e( 'Auto Display', 'wp-powersuite' ); ?></label>
					</th>
					<td>
						<select id="<?php echo esc_attr( $module_id ); ?>_auto_display" name="auto_display" class="wp-powersuite-advanced-setting" data-setting-key="auto_display">
							<option value="none" <?php selected( $auto_display, 'none' ); ?>><?php esc_html_e( 'Manual (Use Shortcode)', 'wp-powersuite' ); ?></option>
							<option value="before" <?php selected( $auto_display, 'before' ); ?>><?php esc_html_e( 'Before Content', 'wp-powersuite' ); ?></option>
							<option value="after" <?php selected( $auto_display, 'after' ); ?>><?php esc_html_e( 'After Content', 'wp-powersuite' ); ?></option>
						</select>
						<p class="description"><?php esc_html_e( 'Automatically display breadcrumbs, or use shortcode for manual placement.', 'wp-powersuite' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row">
						<label><?php esc_html_e( 'Display On', 'wp-powersuite' ); ?></label>
					</th>
					<td>
						<?php if ( empty( $available_types ) ) : ?>
							<p class="description"><?php esc_html_e( 'No public post types found.', 'wp-powersuite' ); ?></p>
						<?php else : ?>
							<p class="description" style="margin-bottom: 8px;"><?php esc_html_e( 'Select which post types show breadcrumbs when auto-display is enabled.', 'wp-powersuite' ); ?></p>
							<div class="wpps-ct-multiselect" id="<?php echo esc_attr( $module_id ); ?>_post_types_ms"
								data-label-none="<?php echo esc_attr( __( 'Select post types...', 'wp-powersuite' ) ); ?>"
								data-label-single="<?php echo esc_attr( __( '1 selected', 'wp-powersuite' ) ); ?>"
								data-label-multi="<?php echo esc_attr( __( '%d selected', 'wp-powersuite' ) ); ?>">
								<button type="button" class="wpps-ct-multiselect__trigger" aria-expanded="false" aria-haspopup="listbox" aria-controls="<?php echo esc_attr( $module_id ); ?>_post_types_list">
									<span class="wpps-ct-multiselect__trigger-text"><?php esc_html_e( 'Select post types...', 'wp-powersuite' ); ?></span>
									<span class="dashicons dashicons-arrow-down-alt2 wpps-ct-multiselect__chevron"></span>
								</button>
								<div class="wpps-ct-multiselect__dropdown" id="<?php echo esc_attr( $module_id ); ?>_post_types_list" role="listbox" hidden>
									<input type="text" class="wpps-ct-multiselect__search" placeholder="<?php esc_attr_e( 'Search post types...', 'wp-powersuite' ); ?>" autocomplete="off" />
									<div class="wpps-ct-multiselect__actions">
										<button type="button" class="wpps-ct-multiselect__action" data-action="select-all"><?php esc_html_e( 'Select all', 'wp-powersuite' ); ?></button>
										<button type="button" class="wpps-ct-multiselect__action" data-action="clear"><?php esc_html_e( 'Clear', 'wp-powersuite' ); ?></button>
									</div>
									<div class="wpps-ct-multiselect__options">
										<?php foreach ( $available_types as $pt_name => $pt_obj ) : ?>
											<?php
											$label   = $pt_obj->labels->name ?? $pt_name;
											$checked = in_array( $pt_name, $post_types, true );
											?>
											<label class="wpps-ct-multiselect__option" data-slug="<?php echo esc_attr( $pt_name ); ?>" data-label="<?php echo esc_attr( $label ); ?>">
												<input type="checkbox" name="post_types[]" class="wp-powersuite-advanced-setting" data-setting-key="post_types" value="<?php echo esc_attr( $pt_name ); ?>" <?php checked( $checked ); ?>>
												<span class="wpps-ct-multiselect__option-label"><?php echo esc_html( $label ); ?></span>
												<span class="wpps-ct-multiselect__option-slug">(<?php echo esc_html( $pt_name ); ?>)</span>
											</label>
										<?php endforeach; ?>
									</div>
								</div>
							</div>
						<?php endif; ?>
					</td>
				</tr>
			</tbody>
		</table>

		<hr style="margin: 24px 0;" />

		<h3><?php esc_html_e( 'Shortcode', 'wp-powersuite' ); ?></h3>
		<p class="description"><?php esc_html_e( 'Use [breadcrumbs] or [wpps_breadcrumbs] to place breadcrumbs anywhere in your content or theme templates.', 'wp-powersuite' ); ?></p>
		<p><code style="background: #f0f0f1; padding: 4px 8px; border-radius: 4px;">[breadcrumbs]</code></p>
		<p class="description"><?php esc_html_e( 'Override settings in the shortcode:', 'wp-powersuite' ); ?></p>
		<p><code style="background: #f0f0f1; padding: 4px 8px; border-radius: 4px;">[breadcrumbs separator="/" home_text="Start"]</code></p>
		<p class="description"><?php esc_html_e( 'In theme templates:', 'wp-powersuite' ); ?></p>
		<p><code style="background: #f0f0f1; padding: 4px 8px; border-radius: 4px;">&lt;?php echo do_shortcode('[breadcrumbs]'); ?&gt;</code></p>
	</form>
</div>
