package edu.fiu.repositories;

import edu.fiu.models.UserStory;
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 UserStory} through the JDBI
 */

@RegisterRowMapper(UserStory.Mapper.class)
public interface UserStoryRepository {

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

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

  /**
   * Returns a matching UserStory object that contains a given text
   * @param text in the UserStory
   * @return List of UserStory records
   */
  @SqlQuery("select * from userStory where text like '%:text%'")
  List<UserStory> get(String text);

  /**
   * Creates a userStory record for the given UserStory object
   * @param story userStory object to create a record for
   * @userID userID of the user
   * @return true if the userStory record was created 
   */
  @SqlUpdate("insert into userStory (userID, text, submitTime) values (:userID, :text, now())")
  boolean create(@BindBean UserStory story);

  /**
   * Deletes the UserStory record for the given UserStory object
   * @param story UserStory object to remove a record for
   * @return true if user entry was deleted, false otherwise
   */
  @SqlUpdate("delete from users where userID=:userID and text=:text")
  boolean delete(@BindBean UserStory story);

}
