<?php

class Wiass_REST_User_API {
	/**
	 * Endpoint namespace.
	 *
	 * @var string
	 */
	private static $namespace = 'wiaas';

	public function __construct() {
		include_once dirname( __FILE__ ) . '/../class-wiaas-countries.php';
	}


	public static function register_routes() {
		register_rest_route( self::$namespace, 'user/validate-token', array(
			'methods' => 'POST',
			'callback' => array(__CLASS__, 'validate_token'),
			'permission_callback' => 'is_user_logged_in'
		) );

		register_rest_route( self::$namespace, 'user/get-countries', array(
			'methods' => 'GET',
			'callback' => array(__CLASS__, 'get_countries'),
			'permission_callback' => 'is_user_logged_in'
		) );
	}


	public static function validate_token() {
		$user = wp_get_current_user();
		
		return rest_ensure_response(array(
			'userInfo' => array(
				'wiaas_id_user' => $user->ID,
				'wiaas_is_company_admin' => 1, //TODO: don't hardcode this
				'wiaas_user_full_name' => $user->first_name . ' ' . $user->last_name,
				'wiaas_user_type' => $user->roles,
				'wiaas_username' => $user->data->user_login 
			)
		));
	}

	public static function get_countries(){
		$countries = Wiaas_Countries::get_list_of_countries();

		return rest_ensure_response($countries);
	}
}