package edu.fiu.repositories;

import edu.fiu.models.ActivityProgress;
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 ActivityProgress} through the JDBI
 */
@RegisterRowMapper(ActivityProgress.Mapper.class)
public interface ActivityProgressRepository
{

    /**
     * Lists all progress records
     * @return List of all progress records as ActivityProgresses
     */
    @SqlQuery("select * from progress")
    List<ActivityProgress> list();

    /**
     * Lists all progress records for a user
     * @return List of all progress records as ActivityProgresses for given User
     */
    @SqlQuery("select * from progress where userId = :userId")
    List<ActivityProgress> list(int userId);

    /**
     * Returns a matching ActivityProgress object for a user record matching the given user ID and type
     * @return matching record as ActivityProgress
     * @param userId ID of user
     * @param activityType type of activity
     */
    @SqlQuery("select * from progress where userId = :userId and activityType = :activityType")
    ActivityProgress get(int userId, int activityType);

    /**
     * Creates an progress record for the given ActivityProgress object
     * @param progress ActivityProgress object to create a record for
     * @return row ID of generated progress record
     */
    @SqlUpdate("insert into progress (state, userId, content, activityId, date) values (:state, :userId, :content, :activityId, :date)")
    void create(@BindBean ActivityProgress progress);

    /**
     * Updates the progress record for the given ActivityProgress object
     * @param progress ActivityProgress object to update values for
     * @return true if progress entry was updated, false otherwise
     */
    @SqlUpdate("update progress set state=:progress.state, date=:progress.date, content=:progress.content where userId=:progress.userId and activityType=:progress.activityType")
    boolean update(@BindBean("progress") ActivityProgress progress);

    /**
     * Deletes the progress record for the given ActivityProgress object
     * @param userId ID of user
     * @param activityID type of activity
     * @return true if progress entry was deleted, false otherwise
     */
    @SqlUpdate("delete from progress where userId = :userId and activityId = :activityId")
    boolean delete(int userId, int activityId);
}
