<?php
/**
 * Site Settings – simple forms instead of WordPress Customizer.
 *
 * @package EasySite_Pro
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Hide WordPress Customizer – clients edit on the front end.
 */
function easysite_hide_customizer_ui() {
	remove_submenu_page( 'themes.php', 'customize.php' );
}
add_action( 'admin_menu', 'easysite_hide_customizer_ui', 999 );

/**
 * Remove Customize from admin bar.
 *
 * @param WP_Admin_Bar $bar Admin bar.
 */
function easysite_remove_customize_admin_bar( $bar ) {
	$bar->remove_node( 'customize' );
}
add_action( 'admin_bar_menu', 'easysite_remove_customize_admin_bar', 999 );

/**
 * Register Site Settings page.
 */
function easysite_site_settings_menu() {
	add_submenu_page(
		'easysite-setup',
		__( 'Site Colors & Logo', 'easysite-pro' ),
		__( 'Site Colors & Logo', 'easysite-pro' ),
		'edit_theme_options',
		'easysite-site-settings',
		'easysite_render_site_settings_page'
	);
}
add_action( 'admin_menu', 'easysite_site_settings_menu', 15 );

/**
 * Enqueue media on settings page.
 *
 * @param string $hook Hook.
 */
function easysite_site_settings_assets( $hook ) {
	if ( 'easysite-setup_page_easysite-site-settings' !== $hook ) {
		return;
	}
	wp_enqueue_media();
	wp_enqueue_style( 'easysite-pro-admin', EASYSITE_PRO_URI . '/assets/css/admin.css', array(), EASYSITE_PRO_VERSION );
	wp_enqueue_script(
		'easysite-site-settings',
		EASYSITE_PRO_URI . '/assets/js/site-settings.js',
		array( 'jquery' ),
		EASYSITE_PRO_VERSION,
		true
	);
}
add_action( 'admin_enqueue_scripts', 'easysite_site_settings_assets' );

/**
 * Save site settings.
 */
function easysite_save_site_settings() {
	if ( ! isset( $_POST['easysite_site_settings_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['easysite_site_settings_nonce'] ) ), 'easysite_site_settings' ) ) {
		wp_die( esc_html__( 'Security check failed.', 'easysite-pro' ) );
	}
	if ( ! current_user_can( 'edit_theme_options' ) ) {
		wp_die( esc_html__( 'Permission denied.', 'easysite-pro' ) );
	}

	$color_fields = array( 'easysite_main_color', 'easysite_accent_color', 'easysite_gradient_color_1', 'easysite_gradient_color_2' );
	foreach ( $color_fields as $key ) {
		if ( isset( $_POST[ $key ] ) ) {
			set_theme_mod( $key, sanitize_hex_color( wp_unslash( $_POST[ $key ] ) ) );
		}
	}

	$text_fields = array(
		'easysite_font_pair',
		'easysite_design_preset',
		'easysite_gradient_preset',
		'easysite_gradient_direction',
		'easysite_phone',
		'easysite_email',
		'easysite_address',
		'easysite_hours',
		'easysite_footer_text',
		'easysite_social_facebook',
		'easysite_social_instagram',
		'easysite_social_twitter',
		'easysite_social_linkedin',
		'easysite_seo_description',
		'easysite_seo_social_image',
		'easysite_ai_api_key',
		'easysite_ai_model',
		'easysite_form_label_name',
		'easysite_form_label_email',
		'easysite_form_label_message',
		'easysite_form_label_submit',
		'easysite_agency_name',
		'easysite_agency_support_email',
		'easysite_help_message',
	);
	foreach ( $text_fields as $key ) {
		if ( isset( $_POST[ $key ] ) ) {
			set_theme_mod( $key, sanitize_text_field( wp_unslash( $_POST[ $key ] ) ) );
		}
	}

	set_theme_mod( 'easysite_use_gradients', ! empty( $_POST['easysite_use_gradients'] ) );
	set_theme_mod( 'easysite_gradient_buttons', ! empty( $_POST['easysite_gradient_buttons'] ) );
	set_theme_mod( 'easysite_gradient_hero', ! empty( $_POST['easysite_gradient_hero'] ) );
	set_theme_mod( 'easysite_client_mode', ! empty( $_POST['easysite_client_mode'] ) );
	set_theme_mod( 'easysite_hide_wp_branding', ! empty( $_POST['easysite_hide_wp_branding'] ) );

	if ( ! empty( $_POST['easysite_design_preset'] ) ) {
		easysite_apply_design_preset( sanitize_key( wp_unslash( $_POST['easysite_design_preset'] ) ) );
	}

	if ( ! empty( $_POST['custom_logo_id'] ) ) {
		set_theme_mod( 'custom_logo', absint( $_POST['custom_logo_id'] ) );
	}

	if ( isset( $_POST['easysite_ai_api_key'] ) ) {
		set_theme_mod( 'easysite_ai_api_key', sanitize_text_field( wp_unslash( $_POST['easysite_ai_api_key'] ) ) );
	}

	easysite_apply_one_click_branding();

	wp_safe_redirect( add_query_arg( 'saved', '1', admin_url( 'admin.php?page=easysite-site-settings' ) ) );
	exit;
}
add_action( 'admin_post_easysite_save_site_settings', 'easysite_save_site_settings' );

/**
 * Render settings page.
 */
function easysite_render_site_settings_page() {
	$front_id = (int) get_option( 'page_on_front' );
	$edit_url = $front_id ? add_query_arg( 'easysite_edit', '1', get_permalink( $front_id ) ) : add_query_arg( 'easysite_edit', '1', home_url( '/' ) );
	$logo_id  = get_theme_mod( 'custom_logo' );
	$logo_url = $logo_id ? wp_get_attachment_image_url( $logo_id, 'medium' ) : '';
	?>
	<div class="wrap easysite-site-settings">
		<h1><?php esc_html_e( 'Site Colors & Logo', 'easysite-pro' ); ?></h1>

		<?php if ( isset( $_GET['saved'] ) ) : // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>
			<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Settings saved!', 'easysite-pro' ); ?></p></div>
		<?php endif; ?>

		<p class="easysite-intro">
			<?php esc_html_e( 'Set your brand colors and logo here. To edit page text and images, use the visual editor on your site.', 'easysite-pro' ); ?>
			<a href="<?php echo esc_url( $edit_url ); ?>" class="button button-primary" style="margin-left:0.5rem;">
				<?php esc_html_e( 'Edit your site visually', 'easysite-pro' ); ?>
			</a>
		</p>

		<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
			<?php wp_nonce_field( 'easysite_site_settings', 'easysite_site_settings_nonce' ); ?>
			<input type="hidden" name="action" value="easysite_save_site_settings" />

			<h2><?php esc_html_e( 'Logo', 'easysite-pro' ); ?></h2>
			<table class="form-table">
				<tr>
					<th><?php esc_html_e( 'Site logo', 'easysite-pro' ); ?></th>
					<td>
						<div class="easysite-logo-picker">
							<img id="easysite-logo-preview" src="<?php echo esc_url( $logo_url ); ?>" alt="" style="max-height:80px;<?php echo $logo_url ? '' : 'display:none;'; ?>" />
							<input type="hidden" name="custom_logo_id" id="custom_logo_id" value="<?php echo esc_attr( $logo_id ); ?>" />
							<p>
								<button type="button" class="button" id="easysite-pick-logo"><?php esc_html_e( 'Choose logo', 'easysite-pro' ); ?></button>
								<button type="button" class="button" id="easysite-remove-logo" <?php echo $logo_url ? '' : 'style="display:none"'; ?>><?php esc_html_e( 'Remove', 'easysite-pro' ); ?></button>
							</p>
						</div>
					</td>
				</tr>
			</table>

			<h2><?php esc_html_e( 'Brand colors', 'easysite-pro' ); ?></h2>
			<table class="form-table">
				<tr>
					<th><label for="easysite_main_color"><?php esc_html_e( 'Main color', 'easysite-pro' ); ?></label></th>
					<td><input type="color" name="easysite_main_color" id="easysite_main_color" value="<?php echo esc_attr( easysite_get_mod( 'easysite_main_color', '#2563eb' ) ); ?>" /></td>
				</tr>
				<tr>
					<th><label for="easysite_accent_color"><?php esc_html_e( 'Accent color', 'easysite-pro' ); ?></label></th>
					<td><input type="color" name="easysite_accent_color" id="easysite_accent_color" value="<?php echo esc_attr( easysite_get_mod( 'easysite_accent_color', '#0ea5e9' ) ); ?>" /></td>
				</tr>
				<tr>
					<th><?php esc_html_e( 'Design preset', 'easysite-pro' ); ?></th>
					<td>
						<select name="easysite_design_preset">
							<?php foreach ( easysite_get_design_presets() as $key => $preset ) : ?>
								<option value="<?php echo esc_attr( $key ); ?>" <?php selected( easysite_get_mod( 'easysite_design_preset', 'modern' ), $key ); ?>><?php echo esc_html( $preset['label'] ); ?></option>
							<?php endforeach; ?>
						</select>
					</td>
				</tr>
				<tr>
					<th><label for="easysite_font_pair"><?php esc_html_e( 'Fonts', 'easysite-pro' ); ?></label></th>
					<td>
						<select name="easysite_font_pair" id="easysite_font_pair">
							<?php foreach ( easysite_get_font_pairs() as $key => $pair ) : ?>
								<option value="<?php echo esc_attr( $key ); ?>" <?php selected( easysite_get_mod( 'easysite_font_pair', 'inter-system' ), $key ); ?>><?php echo esc_html( $pair['label'] ); ?></option>
							<?php endforeach; ?>
						</select>
					</td>
				</tr>
			</table>

			<h2><?php esc_html_e( 'Gradient colors', 'easysite-pro' ); ?></h2>
			<table class="form-table">
				<tr>
					<th><?php esc_html_e( 'Enable gradients', 'easysite-pro' ); ?></th>
					<td><label><input type="checkbox" name="easysite_use_gradients" value="1" <?php checked( easysite_get_mod( 'easysite_use_gradients', true ) ); ?> /> <?php esc_html_e( 'Use gradient colors on buttons and sections', 'easysite-pro' ); ?></label></td>
				</tr>
				<tr>
					<th><?php esc_html_e( 'Gradient preset', 'easysite-pro' ); ?></th>
					<td>
						<select name="easysite_gradient_preset" id="gradient_preset">
							<?php foreach ( easysite_get_gradient_presets() as $key => $g ) : ?>
								<option value="<?php echo esc_attr( $key ); ?>" <?php selected( easysite_get_mod( 'easysite_gradient_preset', 'brand' ), $key ); ?>><?php echo esc_html( $g['label'] ); ?></option>
							<?php endforeach; ?>
						</select>
						<?php easysite_render_gradient_swatches( easysite_get_mod( 'easysite_gradient_preset', 'brand' ) ); ?>
					</td>
				</tr>
				<tr>
					<th><label for="easysite_gradient_color_1"><?php esc_html_e( 'Custom start', 'easysite-pro' ); ?></label></th>
					<td><input type="color" name="easysite_gradient_color_1" id="easysite_gradient_color_1" value="<?php echo esc_attr( easysite_get_mod( 'easysite_gradient_color_1', '#2563eb' ) ); ?>" /></td>
				</tr>
				<tr>
					<th><label for="easysite_gradient_color_2"><?php esc_html_e( 'Custom end', 'easysite-pro' ); ?></label></th>
					<td><input type="color" name="easysite_gradient_color_2" id="easysite_gradient_color_2" value="<?php echo esc_attr( easysite_get_mod( 'easysite_gradient_color_2', '#0ea5e9' ) ); ?>" /></td>
				</tr>
				<tr>
					<th><?php esc_html_e( 'Gradient buttons', 'easysite-pro' ); ?></th>
					<td><label><input type="checkbox" name="easysite_gradient_buttons" value="1" <?php checked( easysite_get_mod( 'easysite_gradient_buttons', true ) ); ?> /></label></td>
				</tr>
			</table>

			<h2><?php esc_html_e( 'Contact & footer', 'easysite-pro' ); ?></h2>
			<table class="form-table">
				<tr>
					<th><label for="easysite_phone"><?php esc_html_e( 'Phone', 'easysite-pro' ); ?></label></th>
					<td><input type="text" name="easysite_phone" id="easysite_phone" class="regular-text" value="<?php echo esc_attr( easysite_get_mod( 'easysite_phone' ) ); ?>" /></td>
				</tr>
				<tr>
					<th><label for="easysite_email"><?php esc_html_e( 'Email', 'easysite-pro' ); ?></label></th>
					<td><input type="email" name="easysite_email" id="easysite_email" class="regular-text" value="<?php echo esc_attr( easysite_get_mod( 'easysite_email' ) ); ?>" /></td>
				</tr>
				<tr>
					<th><label for="easysite_address"><?php esc_html_e( 'Address', 'easysite-pro' ); ?></label></th>
					<td><textarea name="easysite_address" id="easysite_address" rows="2" class="large-text"><?php echo esc_textarea( easysite_get_mod( 'easysite_address' ) ); ?></textarea></td>
				</tr>
				<tr>
					<th><label for="easysite_footer_text"><?php esc_html_e( 'Footer text', 'easysite-pro' ); ?></label></th>
					<td><input type="text" name="easysite_footer_text" id="easysite_footer_text" class="large-text" value="<?php echo esc_attr( easysite_get_mod( 'easysite_footer_text' ) ); ?>" /></td>
				</tr>
				<tr>
					<th><?php esc_html_e( 'Social links', 'easysite-pro' ); ?></th>
					<td>
						<p><input type="url" name="easysite_social_facebook" placeholder="Facebook URL" class="regular-text" value="<?php echo esc_attr( easysite_get_mod( 'easysite_social_facebook' ) ); ?>" /></p>
						<p><input type="url" name="easysite_social_instagram" placeholder="Instagram URL" class="regular-text" value="<?php echo esc_attr( easysite_get_mod( 'easysite_social_instagram' ) ); ?>" /></p>
						<p><input type="url" name="easysite_social_twitter" placeholder="Twitter / X URL" class="regular-text" value="<?php echo esc_attr( easysite_get_mod( 'easysite_social_twitter' ) ); ?>" /></p>
						<p><input type="url" name="easysite_social_linkedin" placeholder="LinkedIn URL" class="regular-text" value="<?php echo esc_attr( easysite_get_mod( 'easysite_social_linkedin' ) ); ?>" /></p>
					</td>
				</tr>
			</table>

			<h2><?php esc_html_e( 'SEO & social sharing', 'easysite-pro' ); ?></h2>
			<table class="form-table">
				<tr>
					<th><label for="easysite_seo_description"><?php esc_html_e( 'Site meta description', 'easysite-pro' ); ?></label></th>
					<td>
						<textarea name="easysite_seo_description" id="easysite_seo_description" rows="3" class="large-text"><?php echo esc_textarea( easysite_get_mod( 'easysite_seo_description' ) ); ?></textarea>
						<p class="description"><?php esc_html_e( 'Default description for search engines when a page has no custom description.', 'easysite-pro' ); ?></p>
					</td>
				</tr>
				<tr>
					<th><label for="easysite_seo_social_image"><?php esc_html_e( 'Social share image URL', 'easysite-pro' ); ?></label></th>
					<td>
						<input type="url" name="easysite_seo_social_image" id="easysite_seo_social_image" class="large-text" value="<?php echo esc_attr( easysite_get_mod( 'easysite_seo_social_image' ) ); ?>" placeholder="https://" />
						<p class="description"><?php esc_html_e( 'Image shown when your site is shared on social media.', 'easysite-pro' ); ?></p>
					</td>
				</tr>
			</table>

			<h2><?php esc_html_e( 'AI writing assistant', 'easysite-pro' ); ?></h2>
			<table class="form-table">
				<tr>
					<th><label for="easysite_ai_api_key"><?php esc_html_e( 'OpenAI API key', 'easysite-pro' ); ?></label></th>
					<td>
						<input type="password" name="easysite_ai_api_key" id="easysite_ai_api_key" class="regular-text" value="<?php echo esc_attr( easysite_get_mod( 'easysite_ai_api_key' ) ); ?>" autocomplete="off" />
						<p class="description"><?php esc_html_e( 'Powers “Write for me” in the visual editor. Optional — demo text is used without a key.', 'easysite-pro' ); ?></p>
					</td>
				</tr>
				<tr>
					<th><label for="easysite_ai_model"><?php esc_html_e( 'AI model', 'easysite-pro' ); ?></label></th>
					<td>
						<input type="text" name="easysite_ai_model" id="easysite_ai_model" class="regular-text" value="<?php echo esc_attr( easysite_get_mod( 'easysite_ai_model', 'gpt-4o-mini' ) ); ?>" />
					</td>
				</tr>
			</table>

			<h2><?php esc_html_e( 'Contact form labels', 'easysite-pro' ); ?></h2>
			<table class="form-table">
				<tr>
					<th><label for="easysite_form_label_name"><?php esc_html_e( 'Name field', 'easysite-pro' ); ?></label></th>
					<td><input type="text" name="easysite_form_label_name" id="easysite_form_label_name" class="regular-text" value="<?php echo esc_attr( easysite_get_mod( 'easysite_form_label_name', __( 'Your name', 'easysite-pro' ) ) ); ?>" /></td>
				</tr>
				<tr>
					<th><label for="easysite_form_label_email"><?php esc_html_e( 'Email field', 'easysite-pro' ); ?></label></th>
					<td><input type="text" name="easysite_form_label_email" id="easysite_form_label_email" class="regular-text" value="<?php echo esc_attr( easysite_get_mod( 'easysite_form_label_email', __( 'Email', 'easysite-pro' ) ) ); ?>" /></td>
				</tr>
				<tr>
					<th><label for="easysite_form_label_message"><?php esc_html_e( 'Message field', 'easysite-pro' ); ?></label></th>
					<td><input type="text" name="easysite_form_label_message" id="easysite_form_label_message" class="regular-text" value="<?php echo esc_attr( easysite_get_mod( 'easysite_form_label_message', __( 'Message', 'easysite-pro' ) ) ); ?>" /></td>
				</tr>
				<tr>
					<th><label for="easysite_form_label_submit"><?php esc_html_e( 'Submit button', 'easysite-pro' ); ?></label></th>
					<td><input type="text" name="easysite_form_label_submit" id="easysite_form_label_submit" class="regular-text" value="<?php echo esc_attr( easysite_get_mod( 'easysite_form_label_submit', __( 'Send Message', 'easysite-pro' ) ) ); ?>" /></td>
				</tr>
			</table>

			<?php if ( easysite_bundle_active() ) : ?>
			<h2><?php esc_html_e( 'Agency & white-label', 'easysite-pro' ); ?></h2>
			<table class="form-table">
				<tr>
					<th><label for="easysite_agency_name"><?php esc_html_e( 'Agency / brand name', 'easysite-pro' ); ?></label></th>
					<td><input type="text" name="easysite_agency_name" id="easysite_agency_name" class="regular-text" value="<?php echo esc_attr( easysite_get_mod( 'easysite_agency_name' ) ); ?>" placeholder="<?php esc_attr_e( 'Shown in editor and client dashboard', 'easysite-pro' ); ?>" /></td>
				</tr>
				<tr>
					<th><label for="easysite_agency_support_email"><?php esc_html_e( 'Support email', 'easysite-pro' ); ?></label></th>
					<td><input type="email" name="easysite_agency_support_email" id="easysite_agency_support_email" class="regular-text" value="<?php echo esc_attr( easysite_get_mod( 'easysite_agency_support_email' ) ); ?>" /></td>
				</tr>
				<tr>
					<th><label for="easysite_help_message"><?php esc_html_e( 'Client welcome message', 'easysite-pro' ); ?></label></th>
					<td><textarea name="easysite_help_message" id="easysite_help_message" rows="2" class="large-text"><?php echo esc_textarea( easysite_get_mod( 'easysite_help_message' ) ); ?></textarea></td>
				</tr>
				<tr>
					<th><?php esc_html_e( 'Client mode', 'easysite-pro' ); ?></th>
					<td>
						<label><input type="checkbox" name="easysite_client_mode" value="1" <?php checked( easysite_get_mod( 'easysite_client_mode', false ) ); ?> /> <?php esc_html_e( 'Simplify dashboard for clients', 'easysite-pro' ); ?></label><br />
						<label><input type="checkbox" name="easysite_hide_wp_branding" value="1" <?php checked( easysite_get_mod( 'easysite_hide_wp_branding', false ) ); ?> /> <?php esc_html_e( 'Hide WordPress branding in admin', 'easysite-pro' ); ?></label>
					</td>
				</tr>
			</table>
			<?php endif; ?>

			<p class="submit">
				<button type="submit" class="button button-primary button-hero"><?php esc_html_e( 'Save settings', 'easysite-pro' ); ?></button>
			</p>
		</form>
	</div>
	<?php
}
