<?php
/**
 * Deployment config management configuration.
 *
 * @package EaLab\WP_Deployer
 */

namespace EaLab\WP_Deployer\includes\configuration;

use EaLab\WP_Deployer\vendor\Ealab\Deployer\Utility\Settings;

/**
 * Deployment config management configuration.
 */
class DeploymentConfigManagementConfiguration extends ConfigurationBase {
	protected static function get_form_priority(): int {
		return -20;
	}

	public static function print_form(): void {
		$configuration_method = Settings::getInstance()->get( 'configuration_method' );
		
		self::enqueue_scripts( $configuration_method );
		?>
		<div class="col-xl-4 col-sm-12">
			<div class="card">
				<div class="card-body">
					<h5 class="card-title"><?php echo esc_html__( 'Deployment strategy', 'deploy-sync-content' ) ?></h5>
					
					<!-- Configuration Method Selection -->
					<div class="mb-3">
						<label class="form-label"><?php echo esc_html__( 'Configuration Method', 'deploy-sync-content' ) ?></label>
						<form id="configMethodForm" data-endpoint="<?php echo esc_attr( rest_url( 'deploy-sync-content/v1/settings/configuration-method' ) ); ?>" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_rest' ) ); ?>">
							
							<!-- Auto Configuration Option -->
							<div class="form-check border rounded" style="<?php echo $configuration_method === 'auto' ? 'border-color: #d4edda !important; background-color: #f8fff9;' : 'border-color: #dee2e6; background-color: #f8f9fa;'; ?> padding: 1rem; padding-left: 3rem; margin: 0.5rem 0;">
								<input class="form-check-input" type="radio" name="configuration_method" id="configAuto" value="auto" <?php checked( $configuration_method, 'auto' ); ?>>
								<label class="form-check-label w-100" for="configAuto">
									<div class="fw-semibold"><?php echo esc_html__( 'Automatic configuration (recommended)', 'deploy-sync-content' ) ?></div>
									<div class="text-muted small mt-1">
										<?php echo esc_html__( 'By selecting this option, I agree to the sending of metadata to EA Lab\'s service in accordance with the ', 'deploy-sync-content' ); ?>
										<a href="https://wp-deployer.io/en/general-terms-and-conditions-use-gtcu" target="_blank" rel="noopener noreferrer"><?php echo esc_html__( 'terms of use', 'deploy-sync-content' ); ?></a>.
									</div>
								</label>
							</div>

							<!-- Manual Configuration Option -->
							<div class="form-check border rounded" style="<?php echo $configuration_method === 'manual' ? 'border-color: #d1ecf1 !important; background-color: #f8f9ff;' : 'border-color: #dee2e6; background-color: #f8f9fa;'; ?> padding: 1rem; padding-left: 3rem; margin: 0.5rem 0;">
								<input class="form-check-input" type="radio" name="configuration_method" id="configManual" value="manual" <?php checked( $configuration_method, 'manual' ); ?>>
								<label class="form-check-label w-100" for="configManual">
									<div class="fw-semibold"><?php echo esc_html__( 'Manual configuration', 'deploy-sync-content' ) ?></div>
									<div class="text-muted small mt-1">
										<?php echo esc_html__( 'Configure Deploy & Sync Content manually or using a 3rd-party provider.', 'deploy-sync-content' ); ?>
									</div>
								</label>
							</div>

							<!-- Submit Buttons -->
							<div class="d-flex gap-2 mt-3">
								<!-- Button when no change or manual selected -->
								<button type="submit" class="btn btn-primary" id="saveConfigBtn" style="display: none;">
									<?php echo esc_html__( 'Save Configuration', 'deploy-sync-content' ); ?>
								</button>
								
								<!-- Button when auto is selected and needs confirmation -->
								<button type="button" class="btn btn-primary" id="confirmAutoBtn" style="display: none;">
									<?php echo esc_html__( 'Confirm Automatic Configuration', 'deploy-sync-content' ); ?>
								</button>
							</div>
						</form>
					</div>

					<?php 
					$config_repository_class = apply_filters( 'deploy_sync_content__get_config_repository', null );
					if ( $config_repository_class ) : ?>
						<div><?php include DEPLOY_SYNC_CONTENT__PLUGIN_DIR . 'admin/partials/component/refresh-config-button.php'; ?></div>
					<?php else : ?>
						<div>
							<button class="button" disabled><?php echo esc_html__( 'Refresh the deployment config', 'deploy-sync-content' ) ?></button>&nbsp;&nbsp;<a
								href="javascript:;"
								class="tooltip-icon" data-bs-placement="top" data-bs-custom-class="custom-tooltip"
								data-bs-toggle="tooltip"
								data-bs-title="<?php echo esc_attr__( 'This feature is not available without a configuration method selected.', 'deploy-sync-content' ) ?>">
								<i class="bi bi-question-circle-fill"></i></a>
							<p class="description"><?php echo esc_html__( 'Select a configuration method to enable this feature.', 'deploy-sync-content' ) ?></p>
						</div>
					<?php endif; ?>
					<div><?php include DEPLOY_SYNC_CONTENT__PLUGIN_DIR . 'admin/partials/component/reindex-items-button.php'; ?></div>
				</div>
			</div>
		</div>

		<?php
	}

	/**
	 * Enqueue scripts for deployment configuration management.
	 * 
	 * @param string $configuration_method Current configuration method.
	 */
	private static function enqueue_scripts( $configuration_method ) {
		if ( ! is_admin() ) {
			return;
		}

		$script_url = plugins_url( 'admin/assets/js/deployment-config-management.js', DEPLOY_SYNC_CONTENT__PLUGIN_FILE );
		$script_version = filemtime( DEPLOY_SYNC_CONTENT__PLUGIN_DIR . 'admin/assets/js/deployment-config-management.js' );

		wp_enqueue_script(
			'deployment-config-management',
			$script_url,
			array(),
			$script_version,
			true
		);

		wp_localize_script(
			'deployment-config-management',
			'deploymentConfigManagement',
			array(
				'currentMethod' => $configuration_method,
				'endpoint' => rest_url( 'deploy-sync-content/v1/settings/configuration-method' ),
				'nonce' => wp_create_nonce( 'wp_rest' ),
				'confirmMessage' => esc_js( __( 'Are you sure you want to enable automatic configuration?\n\nThis will send metadata about your WordPress installation to EA Lab servers for configuration purposes. By proceeding, you confirm that you have read and accept the terms of use.', 'deploy-sync-content' ) ),
				'errorMessage' => esc_js( __( 'Error saving configuration method', 'deploy-sync-content' ) )
			)
		);
	}
}
