<?php
/**
 * Settings tab — General
 * Variable: $options (Options instance)
 *
 * @package Relatedly
 * @since   2.0.0
 * @since   2.3.0 Rebuilt with a WordPress-native form table.
 */

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
defined( 'ABSPATH' ) || exit;

use Relatedly\Premium\FeatureGate;

$is_premium      = FeatureGate::is_premium();
$free_post_types = FeatureGate::FREE_POST_TYPES;

$cache_options = [
	HOUR_IN_SECONDS      => __( '1 Hour', 'relatedly' ),
	3 * HOUR_IN_SECONDS  => __( '3 Hours', 'relatedly' ),
	6 * HOUR_IN_SECONDS  => __( '6 Hours', 'relatedly' ),
	12 * HOUR_IN_SECONDS => __( '12 Hours', 'relatedly' ),
	DAY_IN_SECONDS       => __( '24 Hours', 'relatedly' ),
	7 * DAY_IN_SECONDS   => __( '7 Days', 'relatedly' ),
];

$categories = get_categories( [ 'hide_empty' => false ] );
$tags       = get_tags( [ 'hide_empty' => false ] );

$enabled_types    = (array) $options->get( 'post_types', [ 'post' ] );
$excluded_cats    = array_map( 'strval', (array) $options->get( 'exclude_categories', [] ) );
$excluded_tags    = array_map( 'strval', (array) $options->get( 'exclude_tags', [] ) );
$current_method   = (string) $options->get( 'related_method', 'both' );
$current_duration = (int) $options->get( 'cache_duration', 12 * HOUR_IN_SECONDS );
$is_enabled       = (bool) $options->get( 'enabled', true );
?>
<div class="relatedly-settings-panel">
	<div class="relatedly-settings-panel-header">
		<h2 class="relatedly-settings-panel-title"><?php esc_html_e( 'General Settings', 'relatedly' ); ?></h2>
		<p class="relatedly-settings-panel-desc"><?php esc_html_e( 'Choose where Relatedly appears, how it finds good matches, and what it should leave out.', 'relatedly' ); ?></p>
	</div>

	<div class="relatedly-settings-panel-body">
		<table class="form-table relatedly-settings-table">
			<tbody>

				<!-- Related Posts Display -->
				<tr>
					<th scope="row">
						<label class="relatedly-field-label" for="relatedly-enabled"><?php esc_html_e( 'Related Posts Display', 'relatedly' ); ?></label>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Turn related posts on or off across your site entirely.', 'relatedly' ); ?></p>
					</th>
					<td>
						<div class="relatedly-toggle-row">
							<div class="relatedly-toggle-switch">
								<input type="checkbox" name="enabled" id="relatedly-enabled" value="1" <?php checked( $is_enabled ); ?>>
								<label for="relatedly-enabled"></label>
							</div>
							<span class="relatedly-toggle-status <?php echo $is_enabled ? '' : 'is-inactive'; ?>" data-relatedly-toggle-status>
								<?php echo $is_enabled ? esc_html__( 'Active', 'relatedly' ) : esc_html__( 'Inactive', 'relatedly' ); ?>
							</span>
						</div>
					</td>
				</tr>

				<!-- Match Method -->
				<tr>
					<th scope="row">
						<label class="relatedly-field-label" for="relatedly-related-method"><?php esc_html_e( 'How Matches Are Found', 'relatedly' ); ?></label>
					</th>
					<td>
						<select name="related_method" id="relatedly-related-method" class="relatedly-control">
							<?php
							$methods = [
								'both'     => __( 'Categories & Tags', 'relatedly' ),
								'category' => __( 'Categories Only', 'relatedly' ),
								'tags'     => __( 'Tags Only', 'relatedly' ),
							];
							foreach ( $methods as $val => $label ) :
								?>
								<option value="<?php echo esc_attr( $val ); ?>" <?php selected( $current_method, $val ); ?>><?php echo esc_html( $label ); ?></option>
							<?php endforeach; ?>
						</select>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Choose whether Relatedly looks at categories, tags, or both to find related content.', 'relatedly' ); ?></p>
					</td>
				</tr>

				<!-- Link Target -->
				<tr>
					<th scope="row">
						<label class="relatedly-field-label" for="relatedly-link-target"><?php esc_html_e( 'Link Target', 'relatedly' ); ?></label>
					</th>
					<td>
						<select name="link_target" id="relatedly-link-target" class="relatedly-control">
							<option value="_self" <?php selected( $options->get( 'link_target', '_self' ), '_self' ); ?>><?php esc_html_e( 'Same tab', 'relatedly' ); ?></option>
							<option value="_blank" <?php selected( $options->get( 'link_target', '_self' ), '_blank' ); ?>><?php esc_html_e( 'New tab', 'relatedly' ); ?></option>
						</select>
					</td>
				</tr>

				<!-- Extra Link Instruction -->
				<tr>
					<th scope="row">
						<label class="relatedly-field-label" for="relatedly-link-rel"><?php esc_html_e( 'Extra Link Instruction', 'relatedly' ); ?></label>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Optional. Most sites should leave this blank. Use values like "nofollow" only when you know you need them.', 'relatedly' ); ?></p>
					</th>
					<td>
						<input type="text" name="link_rel" id="relatedly-link-rel" class="relatedly-control" value="<?php echo esc_attr( $options->get( 'link_rel', '' ) ); ?>">
					</td>
				</tr>

				<!-- Post Types -->
				<tr>
					<th scope="row">
						<label class="relatedly-field-label"><?php esc_html_e( 'Post Types', 'relatedly' ); ?></label>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Select which post types should display related content.', 'relatedly' ); ?></p>
					</th>
					<td>
						<div class="relatedly-checkbox-list">
							<?php
							$post_types = get_post_types( [ 'public' => true ], 'objects' );
							foreach ( $post_types as $pt ) :
								$is_free_type = in_array( $pt->name, $free_post_types, true );
								$is_disabled  = ! $is_premium && ! $is_free_type;
								$is_checked   = in_array( $pt->name, $enabled_types, true );
								?>
								<div class="relatedly-checkbox-item <?php echo $is_disabled ? 'is-disabled' : ''; ?>">
									<input type="checkbox" name="post_types[]" id="<?php echo esc_attr( 'relatedly-pt-' . $pt->name ); ?>" value="<?php echo esc_attr( $pt->name ); ?>" <?php checked( $is_checked ); ?> <?php disabled( $is_disabled ); ?>>
									<label for="<?php echo esc_attr( 'relatedly-pt-' . $pt->name ); ?>"><?php echo esc_html( $pt->label ); ?></label>
									<?php if ( $is_disabled ) : ?>
										<span class="relatedly-pro-badge"><?php esc_html_e( 'Pro', 'relatedly' ); ?></span>
									<?php endif; ?>
								</div>
							<?php endforeach; ?>
						</div>
						<?php if ( ! $is_premium ) : ?>
							<p class="relatedly-field-hint"><?php esc_html_e( 'Free version supports Posts and Pages. Upgrade to unlock custom post types.', 'relatedly' ); ?></p>
						<?php endif; ?>
					</td>
				</tr>

				<!-- Exclude Categories -->
				<tr>
					<th scope="row">
						<label class="relatedly-field-label"><?php esc_html_e( 'Exclude Categories', 'relatedly' ); ?></label>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Posts in these categories will never be shown as related.', 'relatedly' ); ?></p>
					</th>
					<td>
						<?php if ( ! empty( $categories ) ) : ?>
							<div class="relatedly-checkbox-list">
								<?php foreach ( $categories as $category ) : ?>
									<div class="relatedly-checkbox-item">
										<input type="checkbox" name="exclude_categories[]" id="<?php echo esc_attr( 'relatedly-cat-' . $category->term_id ); ?>" value="<?php echo esc_attr( $category->term_id ); ?>" <?php checked( in_array( (string) $category->term_id, $excluded_cats, true ) ); ?>>
										<label for="<?php echo esc_attr( 'relatedly-cat-' . $category->term_id ); ?>"><?php echo esc_html( $category->name ); ?></label>
									</div>
								<?php endforeach; ?>
							</div>
						<?php else : ?>
							<p class="relatedly-field-hint"><?php esc_html_e( 'No categories found.', 'relatedly' ); ?></p>
						<?php endif; ?>
					</td>
				</tr>

				<!-- Exclude Tags -->
				<tr>
					<th scope="row">
						<label class="relatedly-field-label"><?php esc_html_e( 'Exclude Tags', 'relatedly' ); ?></label>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Posts with these tags will never be shown as related.', 'relatedly' ); ?></p>
					</th>
					<td>
						<?php if ( ! empty( $tags ) ) : ?>
							<div class="relatedly-checkbox-list">
								<?php foreach ( $tags as $post_tag ) : ?>
									<div class="relatedly-checkbox-item">
										<input type="checkbox" name="exclude_tags[]" id="<?php echo esc_attr( 'relatedly-tag-' . $post_tag->term_id ); ?>" value="<?php echo esc_attr( $post_tag->term_id ); ?>" <?php checked( in_array( (string) $post_tag->term_id, $excluded_tags, true ) ); ?>>
										<label for="<?php echo esc_attr( 'relatedly-tag-' . $post_tag->term_id ); ?>"><?php echo esc_html( $post_tag->name ); ?></label>
									</div>
								<?php endforeach; ?>
							</div>
						<?php else : ?>
							<p class="relatedly-field-hint"><?php esc_html_e( 'No tags found.', 'relatedly' ); ?></p>
						<?php endif; ?>
					</td>
				</tr>

				<!-- Cache Duration -->
				<tr>
					<th scope="row">
						<label class="relatedly-field-label" for="relatedly-cache-duration"><?php esc_html_e( 'Cache Duration', 'relatedly' ); ?></label>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Reuse related post results for faster loading. Shorter times update sooner; longer times reduce database work.', 'relatedly' ); ?></p>
					</th>
					<td>
						<select name="cache_duration" id="relatedly-cache-duration" class="relatedly-control">
							<?php foreach ( $cache_options as $seconds => $label ) : ?>
								<option value="<?php echo esc_attr( (string) $seconds ); ?>" <?php selected( $current_duration, $seconds ); ?>><?php echo esc_html( $label ); ?></option>
							<?php endforeach; ?>
						</select>
					</td>
				</tr>

				<!-- Enable Cache -->
				<tr>
					<th scope="row">
						<label class="relatedly-field-label"><?php esc_html_e( 'Enable Caching', 'relatedly' ); ?></label>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Disable only while troubleshooting.', 'relatedly' ); ?></p>
					</th>
					<td>
						<div class="relatedly-checkbox-item">
							<input type="checkbox" name="enable_cache" id="relatedly-enable-cache" value="1" <?php checked( $options->get( 'enable_cache', true ) ); ?>>
							<label for="relatedly-enable-cache"><?php esc_html_e( 'Reuse related post results for faster loading', 'relatedly' ); ?></label>
						</div>
					</td>
				</tr>

				<!-- Click Tracking -->
				<tr>
					<th scope="row">
						<label class="relatedly-field-label"><?php esc_html_e( 'Relatedly Stats', 'relatedly' ); ?></label>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Count clicks and views for your reports.', 'relatedly' ); ?></p>
					</th>
					<td>
						<div class="relatedly-checkbox-item">
							<input type="checkbox" name="enable_click_tracking" id="relatedly-enable-tracking" value="1" <?php checked( $options->get( 'enable_click_tracking', true ) ); ?>>
							<label for="relatedly-enable-tracking"><?php esc_html_e( 'Count clicks and views for your reports', 'relatedly' ); ?></label>
						</div>
					</td>
				</tr>

				<!-- Data Retention -->
				<tr>
					<th scope="row">
						<label class="relatedly-field-label" for="relatedly-retention"><?php esc_html_e( 'Keep Stats For', 'relatedly' ); ?></label>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Older stats are removed automatically after this many days.', 'relatedly' ); ?></p>
					</th>
					<td>
						<input type="number" name="data_retention_days" id="relatedly-retention" class="relatedly-control" min="1" max="365" value="<?php echo esc_attr( $options->get( 'data_retention_days', 90 ) ); ?>">
					</td>
				</tr>

				<!-- Debug Mode -->
				<tr>
					<th scope="row">
						<label class="relatedly-field-label"><?php esc_html_e( 'Troubleshooting Logs', 'relatedly' ); ?></label>
						<p class="relatedly-field-hint"><?php esc_html_e( 'Leave this off unless support asks for it.', 'relatedly' ); ?></p>
					</th>
					<td>
						<div class="relatedly-checkbox-item">
							<input type="checkbox" name="debug_mode" id="relatedly-debug-mode" value="1" <?php checked( $options->get( 'debug_mode', false ) ); ?>>
							<label for="relatedly-debug-mode"><?php esc_html_e( 'Save extra details for troubleshooting', 'relatedly' ); ?></label>
						</div>
						<div class="relatedly-danger-notice" role="note">
							<span class="relatedly-danger-icon" aria-hidden="true">!</span>
							<span><?php esc_html_e( 'This can add extra details to your site logs and make log files larger. It only works when WordPress logging is turned on.', 'relatedly' ); ?></span>
						</div>
					</td>
				</tr>

			</tbody>
		</table>
	</div>

	<div class="relatedly-save-bar">
		<?php submit_button( __( 'Save Changes', 'relatedly' ), 'primary', 'submit', false ); ?>
	</div>
</div>
