<?php
/**
 * The core user profile framework class.
 *
 * @package Sifency
 */

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

class Sifency_Addons_User_Profile {

	/**
	 * Array of all user profile configurations.
	 *
	 * @var array
	 */
	private $profiles;

	/**
	 * The constructor.
	 */
	public function __construct( array $profile_definitions ) {
		$this->profiles = $profile_definitions;

		add_action( 'show_user_profile', array( $this, 'render_fields' ) );
		add_action( 'edit_user_profile', array( $this, 'render_fields' ) );

		add_action( 'personal_options_update', array( $this, 'save_fields' ) );
		add_action( 'edit_user_profile_update', array( $this, 'save_fields' ) );
	}

	/**
	 * Renders the custom profile fields on the user profile page.
	 *
	 * @param WP_User $user The user object being edited.
	 */
	public function render_fields( $user ) {
		foreach ( $this->profiles as $meta_key => $profile ) {
			echo '<h2>' . esc_html( $profile['title'] ) . '</h2>';
			echo '<table class="form-table">';

			wp_nonce_field( 'Sifency_Addons_User_Profile_nonce_action', 'Sifency_Addons_User_Profile_nonce' );

			$user_meta = get_user_meta( $user->ID, $meta_key, true );
			$user_meta = is_array( $user_meta ) ? $user_meta : array();

			foreach ( $profile['fields'] as $field ) {
				$value = $user_meta[ $field['id'] ] ?? '';
				?>
				<tr>
					<th><label for="<?php echo esc_attr( $field['id'] ); ?>"><?php echo esc_html( $field['title'] ); ?></label></th>
					<td>
						<?php $this->render_field( $meta_key, $field, $value ); ?>
					</td>
				</tr>
				<?php
			}
			echo '</table>';
		}
	}

	/**
	 * Renders the HTML for a single input field.
	 *
	 * @param string $meta_key The top-level meta key.
	 * @param array  $field    The field configuration.
	 * @param mixed  $value    The current value of the field.
	 */
	private function render_field( $meta_key, $field, $value ) {
		$field_name = $meta_key . '[' . $field['id'] . ']';
		$field_id   = $field['id'];
		$type       = $field['type'] ?? 'text';

		switch ( $type ) {
			case 'url':
				echo '<input type="url" id="' . esc_attr( $field_id ) . '" name="' . esc_attr( $field_name ) . '" value="' . esc_attr( $value ) . '" class="regular-text" />';
				break;

			case 'text':
			default:
				echo '<input type="text" id="' . esc_attr( $field_id ) . '" name="' . esc_attr( $field_name ) . '" value="' . esc_attr( $value ) . '" class="regular-text" />';
				break;
		}
	}

	/**
	 * Saves the custom profile fields.
	 *
	 * @param int $user_id The ID of the user being updated.
	 */
	public function save_fields( $user_id ) {
		if ( ! isset( $_POST['Sifency_Addons_User_Profile_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['Sifency_Addons_User_Profile_nonce'] ), 'Sifency_Addons_User_Profile_nonce_action' ) ) {
			return;
		}
		if ( ! current_user_can( 'edit_user', $user_id ) ) {
			return;
		}

		foreach ( $this->profiles as $meta_key => $profile ) {
			if ( ! isset( $_POST[ $meta_key ] ) || ! is_array( $_POST[ $meta_key ] ) ) {
				continue;
			}

			$posted_data   = $_POST[ $meta_key ]; // phpcs:ignore
			$sanitized_data = array();

			foreach ( $profile['fields'] as $field ) {
				$field_id = $field['id'];
				if ( isset( $posted_data[ $field_id ] ) ) {
					$sanitized_data[ $field_id ] = $this->sanitize_field( $posted_data[ $field_id ], $field );
				}
			}

			update_user_meta( $user_id, $meta_key, $sanitized_data );
		}
	}

	/**
	 * Sanitizes a single field's value based on its type.
	 *
	 * @param mixed $value The raw value.
	 * @param array $field The field configuration.
	 * @return mixed The sanitized value.
	 */
	private function sanitize_field( $value, $field ) {
		$type = $field['type'] ?? 'text';

		switch ( $type ) {
			case 'url':
				return esc_url_raw( $value );

			case 'text':
			default:
				return sanitize_text_field( $value );
		}
	}
}