<?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 ?? 'wp-config-editor' ); ?>">
		<div class="notice notice-warning inline" style="margin:0;">
			<p>
				<?php esc_html_e( 'WP Config Editor requires an active license. Activate your license to edit wp-config.php from 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 ( ! isset( $module_id ) || empty( $module_id ) ) {
	$module_id = 'wp-config-editor';
}

use WP_PowerSuite\Core\WpConfigHelper;

$config_status = WpConfigHelper::getConfigStatus();
$path          = $config_status['path'];
$writable      = $config_status['writable'];
$layout        = $config_status['layout'];
$layout_msg    = $config_status['layout_message'];
$is_production = WpConfigHelper::isProductionEnvironment();
?>

<div class="wp-powersuite-module-settings">
	<?php if ( $is_production ) : ?>
	<div class="notice notice-warning" style="margin: 0 0 16px 0; padding: 12px 16px;">
		<p style="margin: 0;"><strong><?php esc_html_e( 'Production environment:', 'wp-powersuite' ); ?></strong>
		<?php esc_html_e( 'Changes to wp-config.php affect every request. Create a backup before saving and verify the site immediately afterward.', 'wp-powersuite' ); ?></p>
	</div>
	<?php endif; ?>

	<div class="notice notice-warning" style="margin: 0 0 16px 0; padding: 12px 16px;">
		<p style="margin: 0;"><strong><?php esc_html_e( 'Warning:', 'wp-powersuite' ); ?></strong>
		<?php esc_html_e( 'Editing wp-config.php can break your site if done incorrectly. Always create a backup before saving. Keep FTP/SSH access as a fallback.', 'wp-powersuite' ); ?></p>
	</div>

	<?php if ( ! empty( $config_status['requires_network_admin'] ) ) : ?>
	<p class="notice notice-info inline" style="margin: 0 0 16px 0;">
		<?php esc_html_e( 'Multisite: only network administrators can edit wp-config.php from the dashboard.', 'wp-powersuite' ); ?>
	</p>
	<?php endif; ?>

	<?php if ( $layout_msg !== '' ) : ?>
	<p class="notice notice-warning inline" style="margin: 0 0 16px 0;">
		<?php echo esc_html( $layout_msg ); ?>
	</p>
	<?php endif; ?>

	<p class="description">
		<?php esc_html_e( 'Edit wp-config.php securely from the admin panel. Lightweight, no bloat. Use the Create Backup button to create a backup before saving.', 'wp-powersuite' ); ?>
	</p>

	<form class="wp-powersuite-module-settings-form" data-module-id="<?php echo esc_attr( $module_id ); ?>" data-ajax-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_powersuite_ajax' ) ); ?>">
		<?php wp_nonce_field( 'wp_powersuite_save_module_settings_' . $module_id, 'wp_powersuite_nonce' ); ?>
		<div class="wpps-wpconfig-editor" data-module-id="<?php echo esc_attr( $module_id ); ?>">

			<?php if ( empty( $path ) || $layout === 'bedrock' ) : ?>
				<p class="notice notice-error inline" style="margin: 0;">
					<?php esc_html_e( 'wp-config.php not found or not editable on this install layout.', 'wp-powersuite' ); ?>
				</p>
			<?php else : ?>
				<p class="description" style="margin-bottom: 12px;">
					<strong><?php esc_html_e( 'File:', 'wp-powersuite' ); ?></strong>
					<code><?php echo esc_html( $path ); ?></code>
					<?php if ( ! $writable ) : ?>
						<span style="color: #b32d2e;"> — <?php esc_html_e( 'Not writable. Use FTP or file manager to edit.', 'wp-powersuite' ); ?></span>
					<?php endif; ?>
				</p>

				<p class="wpps-wpconfig-actions">
					<button type="button" class="button wpps-wpconfig-load"><?php esc_html_e( 'Load', 'wp-powersuite' ); ?></button>
					<button type="button" class="button wpps-wpconfig-reveal" <?php disabled( ! $writable ); ?> style="display: none;">
						<?php esc_html_e( 'Reveal Secrets', 'wp-powersuite' ); ?>
					</button>
					<button type="button" class="button wpps-wpconfig-backup" <?php disabled( ! $writable ); ?>>
						<?php esc_html_e( 'Create Backup', 'wp-powersuite' ); ?>
					</button>
				</p>

				<p class="wpps-wpconfig-mask-notice" style="display: none; margin-bottom: 12px; padding: 10px 12px; background: #f0f6fc; border-left: 4px solid #2271b1;">
					<?php esc_html_e( 'Sensitive values (database credentials and security keys) are masked. Use Reveal Secrets only when you need to edit them.', 'wp-powersuite' ); ?>
				</p>

				<textarea
					id="wpps-wpconfig-content"
					class="large-text code wpps-wpconfig-code-editor"
					rows="24"
					style="width: 100%; font-family: Consolas, Monaco, monospace; font-size: 13px;"
					placeholder="<?php esc_attr_e( 'Click Load to fetch wp-config.php content.', 'wp-powersuite' ); ?>"
					<?php disabled( ! $writable ); ?>
				></textarea>
			<?php endif; ?>
		</div>
	</form>
</div>
