<?php
/**
 * Account Profile Template
 *
 * Profile editing form for ambassadors.
 *
 * @package AmbassadorHub\Accounts\Blocks\Account
 */

defined( 'ABSPATH' ) || exit;

use AmbassadorHub\Accounts\AccountFields;
use AmbassadorHub\Accounts\ProfilePicture;
use AmbassadorHub\Accounts\Admin\Settings\SettingsManager;

$ambh_user = wp_get_current_user();
?>

<div class="ambassador-account__profile">
	<h2><?php echo esc_html__( 'Edit Profile', 'ambassadorhub' ); ?></h2>

	<!-- Profile Picture Section -->
	<div class="ambassador-account__profile-picture-section">
		<h3><?php echo esc_html__( 'Profile Picture', 'ambassadorhub' ); ?></h3>
		
		<div class="ambassador-account__profile-picture-container">
			<div class="ambassador-account__profile-picture-preview">
				<?php echo wp_kses_post( ProfilePicture::get_profile_picture( $ambh_user->ID, 'thumbnail' ) ); ?>
			</div>

			<div class="ambassador-account__profile-picture-actions">
				<?php if ( ProfilePicture::has_profile_picture( $ambh_user->ID ) ) : ?>
					<form id="ambh_remove_profile_picture_form" class="ambassador-account__profile-picture-remove" style="display: inline;">
						<?php wp_nonce_field( 'ambh_remove_profile_picture', '_wpnonce', false ); ?>
						<button type="submit" class="ambassador-account__button ambassador-account__button--secondary">
							<?php echo esc_html__( 'Remove Picture', 'ambassadorhub' ); ?>
						</button>
					</form>
				<?php endif; ?>

				<form id="ambh_upload_profile_picture_form" class="ambassador-account__profile-picture-upload" enctype="multipart/form-data">
					<?php wp_nonce_field( 'ambh_upload_profile_picture', '_wpnonce', false ); ?>
					
					<div class="ambassador-account__file-input-wrapper">
						<label for="ambh_profile_picture_file" class="ambassador-account__file-label">
							<?php echo esc_html__( 'Choose File', 'ambassadorhub' ); ?>
						</label>
						<input 
							type="file" 
							name="file" 
							id="ambh_profile_picture_file" 
							accept="image/jpeg,image/jpg,image/png,image/gif,image/webp"
							class="ambassador-account__file-input"
							required 
						/>
						<span class="ambassador-account__file-name"></span>
					</div>

					<button type="submit" class="ambassador-account__button ambassador-account__button--primary">
						<?php echo esc_html__( 'Upload Picture', 'ambassadorhub' ); ?>
					</button>
				</form>

				<p class="ambassador-account__help-text">
					<?php echo esc_html__( 'Maximum file size: 2MB. Allowed formats: JPG, PNG, GIF, WebP.', 'ambassadorhub' ); ?>
				</p>
			</div>
		</div>
	</div>

	<hr class="ambassador-account__divider" />

	<!-- Profile Information Form -->
	<div class="ambassador-account__profile-message" aria-live="polite"></div>
	<form method="post" class="ambassador-account__profile-form">
		<?php wp_nonce_field( 'wp_rest', '_wpnonce', false ); ?>

		<div class="ambassador-account__form-group">
			<label for="first_name">
				<?php echo esc_html__( 'First Name', 'ambassadorhub' ); ?>
			</label>
			<input 
				type="text" 
				id="first_name" 
				name="first_name" 
				value="<?php echo esc_attr( $ambh_user->first_name ); ?>"
				class="ambassador-account__input"
			/>
		</div>

		<div class="ambassador-account__form-group">
			<label for="last_name">
				<?php echo esc_html__( 'Last Name', 'ambassadorhub' ); ?>
			</label>
			<input 
				type="text" 
				id="last_name" 
				name="last_name" 
				value="<?php echo esc_attr( $ambh_user->last_name ); ?>"
				class="ambassador-account__input"
			/>
		</div>

		<div class="ambassador-account__form-group">
			<label>
				<?php echo esc_html__( 'Email Address', 'ambassadorhub' ); ?>
			</label>
			<p class="ambassador-account__input ambassador-account__input--readonly">
				<?php echo esc_html( $ambh_user->user_email ); ?>
			</p>
		</div>

		<hr class="ambassador-account__divider" />
		<h3 class="ambassador-account__section-heading"><?php esc_html_e( 'Address', 'ambassadorhub' ); ?></h3>

		<div class="ambassador-account__form-group">
			<label for="ambh_street">
				<?php esc_html_e( 'Street address', 'ambassadorhub' ); ?>
			</label>
			<input
				type="text"
				id="ambh_street"
				name="ambh_street"
				value="<?php echo esc_attr( get_user_meta( $ambh_user->ID, 'ambh_street', true ) ); ?>"
				class="ambassador-account__input"
				autocomplete="street-address"
			/>
		</div>

		<div class="ambassador-account__form-row">
			<div class="ambassador-account__form-group">
				<label for="ambh_postal_code">
					<?php esc_html_e( 'Postal code', 'ambassadorhub' ); ?>
				</label>
				<input
					type="text"
					id="ambh_postal_code"
					name="ambh_postal_code"
					value="<?php echo esc_attr( get_user_meta( $ambh_user->ID, 'ambh_postal_code', true ) ); ?>"
					class="ambassador-account__input"
					autocomplete="postal-code"
				/>
			</div>
			<div class="ambassador-account__form-group">
				<label for="ambh_city">
					<?php esc_html_e( 'City', 'ambassadorhub' ); ?>
				</label>
				<input
					type="text"
					id="ambh_city"
					name="ambh_city"
					value="<?php echo esc_attr( get_user_meta( $ambh_user->ID, 'ambh_city', true ) ); ?>"
					class="ambassador-account__input"
					autocomplete="address-level2"
				/>
			</div>
		</div>

		<div class="ambassador-account__form-group">
			<label for="ambh_country">
				<?php esc_html_e( 'Country', 'ambassadorhub' ); ?>
			</label>
			<?php
			$ambh_saved_country = get_user_meta( $ambh_user->ID, 'ambh_country', true );
			$ambh_countries     = array(
				'NL' => __( 'Netherlands', 'ambassadorhub' ),
				'AT' => __( 'Austria', 'ambassadorhub' ),
				'AU' => __( 'Australia', 'ambassadorhub' ),
				'BE' => __( 'Belgium', 'ambassadorhub' ),
				'CA' => __( 'Canada', 'ambassadorhub' ),
				'CH' => __( 'Switzerland', 'ambassadorhub' ),
				'CN' => __( 'China', 'ambassadorhub' ),
				'CZ' => __( 'Czech Republic', 'ambassadorhub' ),
				'DE' => __( 'Germany', 'ambassadorhub' ),
				'DK' => __( 'Denmark', 'ambassadorhub' ),
				'ES' => __( 'Spain', 'ambassadorhub' ),
				'FI' => __( 'Finland', 'ambassadorhub' ),
				'FR' => __( 'France', 'ambassadorhub' ),
				'GB' => __( 'United Kingdom', 'ambassadorhub' ),
				'GR' => __( 'Greece', 'ambassadorhub' ),
				'HU' => __( 'Hungary', 'ambassadorhub' ),
				'IE' => __( 'Ireland', 'ambassadorhub' ),
				'IN' => __( 'India', 'ambassadorhub' ),
				'IT' => __( 'Italy', 'ambassadorhub' ),
				'JP' => __( 'Japan', 'ambassadorhub' ),
				'LU' => __( 'Luxembourg', 'ambassadorhub' ),
				'MX' => __( 'Mexico', 'ambassadorhub' ),
				'NO' => __( 'Norway', 'ambassadorhub' ),
				'NZ' => __( 'New Zealand', 'ambassadorhub' ),
				'PL' => __( 'Poland', 'ambassadorhub' ),
				'PT' => __( 'Portugal', 'ambassadorhub' ),
				'RO' => __( 'Romania', 'ambassadorhub' ),
				'SE' => __( 'Sweden', 'ambassadorhub' ),
				'SG' => __( 'Singapore', 'ambassadorhub' ),
				'SK' => __( 'Slovakia', 'ambassadorhub' ),
				'TR' => __( 'Turkey', 'ambassadorhub' ),
				'UA' => __( 'Ukraine', 'ambassadorhub' ),
				'US' => __( 'United States', 'ambassadorhub' ),
				'ZA' => __( 'South Africa', 'ambassadorhub' ),
			);
			?>
			<select
				id="ambh_country"
				name="ambh_country"
				class="ambassador-account__input"
				autocomplete="country"
			>
				<option value=""><?php esc_html_e( '— Select country —', 'ambassadorhub' ); ?></option>
				<?php foreach ( $ambh_countries as $ambh_code => $ambh_label ) : ?>
					<option value="<?php echo esc_attr( $ambh_code ); ?>"<?php selected( $ambh_saved_country, $ambh_code ); ?>>
						<?php echo esc_html( $ambh_label ); ?>
					</option>
				<?php endforeach; ?>
			</select>
		</div>

		<hr class="ambassador-account__divider" />

		<?php AccountFields::render_profile_fields( $ambh_user ); ?>

		<?php
		/**
		 * Fires in the profile form.
		 *
		 * Modules can add custom profile fields here.
		 *
		 * @since 0.1.0
		 *
		 * @param WP_User $ambh_user Current user object.
		 */
		do_action( 'ambh_account_profile_fields', $ambh_user );
		?>

		<div class="ambassador-account__form-actions">
			<button type="submit" name="ambassador_update_profile" class="ambassador-account__button ambassador-account__button--primary">
				<?php echo esc_html__( 'Save Changes', 'ambassadorhub' ); ?>
			</button>
		</div>
	</form>
</div>
