<?php

use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

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 ?? 'content-report-widget' ); ?>">
		<div class="notice notice-warning inline" style="margin:0;">
			<p>
				<?php esc_html_e( 'Content Report Widget requires an active license. Activate your license to show content statistics on the dashboard.', 'wp-powersuite' ); ?>
				<a href="<?php echo esc_url( $license_page_url ); ?>"><?php esc_html_e( 'Activate license', 'wp-powersuite' ); ?></a>
			</p>
		</div>
	</div>
	<?php
	return;
}

if ( ! current_user_can( 'manage_options' ) ) {
	return;
}

if ( ! isset( $module_id ) ) {
	$module_id = 'content-report-widget';
}
$default_settings = [
	'show_empty_cpts' => false,
];

$settings = OptionStoreModuleSettingsRepository::forModule( $module_id )->all( [] );
$settings = is_array( $settings ) ? $settings : [];
$settings = array_merge( $default_settings, $settings );

?>
<div class="wp-powersuite-module-settings">
	<p class="description">
		<?php esc_html_e( 'This module displays content statistics in a dashboard widget. Stats are cached for 5 minutes and refresh when content changes. Click any stat to go to that section.', '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"><?php esc_html_e( 'Custom Post Types', 'wp-powersuite' ); ?></th>
					<td>
						<label>
							<input type="checkbox"
								name="show_empty_cpts"
								class="wp-powersuite-advanced-setting"
								data-setting-key="show_empty_cpts"
								value="1"
								<?php checked( ! empty( $settings['show_empty_cpts'] ) ); ?>
							/>
							<?php esc_html_e( 'Show custom post types with zero items', 'wp-powersuite' ); ?>
						</label>
						<p class="description">
							<?php esc_html_e( 'By default, only CPTs with content are shown. Enable to display all registered custom post types including empty ones.', 'wp-powersuite' ); ?>
						</p>
					</td>
				</tr>
			</tbody>
		</table>
	</form>
</div>
