<?php

namespace ModernFields\FieldGroups\Entities;

use WP_User;

/**
 * Represents a user entity.
 * 
 * @since 1.0.0
 */
class User extends Entity
{

  /**
   * @var int
   */
  protected int $ID;

  /**
   * @var string
   */
  protected string $login;

  /**
   * @var string
   */
  protected string $first_name;

  /**
   * @var string
   */
  protected string $last_name;

  /**
   * @var string
   */
  protected string $email;

  /**
   * @var array
   */
  protected array $roles;

  /**
   * @var array
   */
  protected array $capabilities;

  /**
   * @var array
   */
  protected array $meta;

  /**
   * @param WP_User $user
   */
  public function __construct(WP_User $user)
  {
    $this->ID = (int) $user->ID;
    $this->login = $user->data->user_login;
    $this->first_name = get_user_meta($user->ID, 'first_name', true);
    $this->last_name = get_user_meta($user->ID, 'last_name', true);
    $this->email = $user->data->user_email;
    $this->roles = $user->roles;
    $this->capabilities = $user->allcaps;
    $this->meta = get_user_meta($user->ID);
  }

  public function getId(): int
  {
    return $this->ID;
  }

  public function getLogin(): string
  {
    return $this->login;
  }

  public function getFirstName(): string
  {
    return $this->first_name;
  }

  public function getLastName(): string
  {
    return $this->last_name;
  }

  public function getEmail(): string
  {
    return $this->email;
  }

  public function getRoles(): array
  {
    return $this->roles;
  }

  public function getCapabilities(): array
  {
    return $this->capabilities;
  }

  public function toArray(): array
  {
    return [
      'ID' => $this->ID,
      'login' => $this->login,
      'first_name' => $this->first_name,
      'last_name' => $this->last_name,
      'email' => $this->email,
      'roles' => $this->roles,
      'capabilities' => $this->capabilities,
      'meta' => $this->prepareMetaForJson(),
    ];
  }
}
