package edu.fiu.repositories;

import edu.fiu.models.User;
import org.jdbi.v3.sqlobject.config.RegisterRowMapper;
import org.jdbi.v3.sqlobject.customizer.BindBean;
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;

import java.util.List;

/**
 * Provides CRUD operations for {@link User} through the JDBI
 */
@RegisterRowMapper(User.Mapper.class)
public interface UserRepository {

  /**
   * Lists all user records
   * 
   * @return List of all user records as User objects
   */
  @SqlQuery("select * from users")
  List<User> list();

  /**
   * Returns a matching User object for a user record matching the given ID
   * 
   * @param id ID of user to get
   * @return user
   */
  @SqlQuery("select * from users where id = :id")
  User get(int id);

  /**
   * Returns a matching User object for a user record matching the given ID
   * 
   * @param name of user to get
   * @return user
   */
  @SqlQuery("select * from users where name = :name")
  User get(String name);

  /**
   * Checks if the user is marked as active in the DB.
   * 
   * @param name of user
   * @return Integer 1 if active; 0 otherwise
   */
  @SqlQuery("select active from users where name = :name")
  Integer getUserStatus(String name);

  /**
   * Creates a user record for the given User object
   * 
   * @param user User object to create a record for
   * @return The generated ID for the given User
   */
  @SqlUpdate("insert into users (name, hash) values (:name, :hash)")
  @GetGeneratedKeys
  int create(@BindBean User user);

  /**
   * Updates the user record for the given User object
   * 
   * @param user User object to update values for
   * @return true if user entry was updated, false otherwise
   */
  @SqlUpdate("update users set name=:user.name, hash=:user.hash where id=:user.id")
  boolean update(@BindBean("user") User user);

  /**
   * Deletes the user record for the given User object
   * 
   * @param user User object to remove a record for
   * @return true if user entry was deleted, false otherwise
   */
  @SqlUpdate("delete from users where id=:id")
  boolean delete(int id);

  /**
   * Marks the user active in the users table
   * 
   * @param userName
   * @return true if operation is sucessful, false otherwise
   */
  @SqlUpdate("update users set active=1 where name=:userName")
  boolean markActive(String userName);

  /**
   * Sets the host in the users table from which the user is loggin in
   * @param userName
   * @param host the machine the user is in
   * @return true if operations sucessful, false otherwise; 
   */
  @SqlUpdate("update users set host=:host where name=:userName")
  boolean setHost(String userName, String host);

  /**
   * Marks the user inactive in the users table
   * 
   * @param userUD
   * @return true if operation is sucessful, false otherwise
   */
  @SqlUpdate("update users set active=0 where id=:userID")
  boolean markInactive(Integer userID);

  /**
   * Removes the host name for a given userID
   * 
   * @param userUD
   * @return true if operation is sucessful, false otherwise
   */
  @SqlUpdate("update users set host=null where id=:userID")
  boolean clearHost(Integer userID);


}
