<?php

namespace Api\Users;

use Exception;
use Khofmann\Api\Api;
use Khofmann\Input\Input;
use Khofmann\Models\User\User;
use Khofmann\Response\Response;
use Khofmann\ApiError\ApiError;
use Khofmann\Request\Request;

/**
 * Users route handlers
 */
class Users extends Api
{
  /**
   * Users GET handler
   *
   * Lists users. Optional parameters are `l` (limit of returned list) and `p` (page, i.e. offset).
   *
   * Returns list of users.
   */
  public function list()
  {
    // Fetch and constrain all parameters.
    $page = max(0, intval(Input::get("p", 0)));
    $limit = constrain(0, 30, intval(Input::get("l", 10)));

    // Return list of users.
    Response::json(User::list($page, $limit));
  }

  /**
   * User GET handler
   *
   * Get a single user.
   *
   * Returns user.
   *
   * @param mixed $id User ID
   *
   * @throws 404 User not found
   */
  public function get($id): void
  {
    // Try and get a user, 404 if not found.
    try {
      Response::json(User::getByID($id));
    } catch (Exception $err) {
      switch ($err->getMessage()) {
        case "NotFound":
          throw ApiError::notFound("user");
        default:
          throw $err;
      }
    }
  }

  /**
   * Users PATCH handler
   *
   * Update a user.
   *
   * Returns updated user.
   *
   * @param mixed $id User ID
   *
   * @throws 404 User not found
   * @throws 500 Failed to update user
   */
  public function patch($id): void
  {
    // Fetch all inputs.
    $username = Input::patch("username");
    $password = Input::patch("password");
    $email = Input::patch("email");

    // Try and update user.
    // Throw errors according to situation.
    try {
      Response::json(User::getByID($id)->update($username, $password, $email));
    } catch (Exception $err) {
      switch ($err->getMessage()) {
        case "NotFound":
          throw ApiError::notFound("user");
        default:
          // Due to how the failed field is handled, it's ApiError is inside the models update
          throw $err;
      }
    }
  }

  /**
   * Users PATCH handler
   *
   * Update a user. User is retrieved using the authentication `token`.
   *
   * Returns updated user.
   *
   * @throws 404 User not found
   * @throws 500 Failed to update user
   */
  public function patchSelf(): void
  {
    // Fetch all inputs.
    $token = Request::token();
    $username = Input::patch("username");
    $password = Input::patch("password");
    $email = Input::patch("email");

    // Try and update user.
    // Throw errors according to situation.
    try {
      Response::json(User::getByToken($token)->update($username, $password, $email));
    } catch (Exception $err) {
      switch ($err->getMessage()) {
        case "NotFound":
          throw ApiError::notFound("user");
        default:
          // Due to how the failed field is handled, it's ApiError is inside the models update
          throw $err;
      }
    }
  }

  /**
   * Users DELETE handler
   *
   * Deletes a user. Optional parameter is `l` (limit of list for which the returned pages is calculated).
   *
   * Returns deleted user and resulting amount of pages for a given limit.
   *
   * @param mixed $id User ID
   *
   * @throws 404 User not found
   */
  public function delete($id): void
  {
    // Fetch and constrain all parameters.
    $limit = constrain(0, 30, intval(Input::get("l", 10)));
    // Try to delete user, 404 if not found.
    try {
      Response::json(User::getByID($id)->delete($limit));
    } catch (Exception $err) {
      switch ($err->getMessage()) {
        case "NotFound":
          throw ApiError::notFound("user");
        default:
          throw $err;
      }
    }
  }
}
