package edu.fiu.repositories;

import edu.fiu.models.Session;
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(Session.Mapper.class)
public interface SessionRepository
{


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

  /**
   * Returns a matching Session object for a given token
   * @param token token GUID of the session
   * @return Session object
   */
  @SqlQuery("select * from sessions where token = :token")
  public Session get(String token);

  /**
   * Creates a user record for the given User object
   * @param token GUID token of the session
   * @return The generated ID for the given session
   */
  @SqlUpdate("insert into sessions (token, userIds, active, dateTime) values (:token, :userIds, :active, :dateTime)")
  @GetGeneratedKeys
  int create(String token, String userIds, boolean active, String dateTime);

  /**
   * Deletes a session 
   * @param sessionToken GUID token of the session
   * @return true if success; false otherwise
   */
  @SqlUpdate("delete from sessions where token=:sessionToken")
  public boolean removeSession(String sessionToken);

  /**
   * Marks a session as inactive
   * @param sessionToken GUID token of the session
   * @return true if success; false otherwise
   */
  @SqlUpdate("update sessions set active=0 where token=:sessionToken")
  public boolean disableSession(String sessionToken);


  /**
   * Updates the dateTime field of the session; 
   * @param sessionToken GUID token of the session
   * @param dateTime The date time. 
   * @return true if success; false otherwise
   */
  @SqlUpdate("update sessions set dateTime=:dateTime where token=:sessionToken")
  public boolean updateSession(String sessionToken, String dateTime);

  
  /**
   * 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 users  where id=:id")
 // boolean delete(int id);

}
