package com.atlassian.bitbucket.mesh.repository;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.Instant;
import java.util.List;

public interface WorkTreeStateDao {

    /**
     * Create the {@link WorkTreeState} in the database. The provided builder will be updated with the ID of the stored
     * information.
     *
     * @return the ID of the stored {@link WorkTreeState}
     */
    long create(@Nonnull Instant expiry, boolean hasAlternates, @Nonnull String repositoryId);

    /**
     * Delete the {@link WorkTreeState} associated with the given ID. If no state exists for the given ID no operation
     * is performed and no error is reported.
     *
     * @return the number of rows affected
     */
    int deleteById(long id);

    /**
     * Delete all work trees in the provided list.
     *
     * @param ids list of IDs to remove
     */
    void deleteByIds(@Nonnull List<Long> ids);

    /**
     * Get the {@link WorkTreeState state} for the work tree with the given ID.
     *
     * @return the {@link WorkTreeState} or {code null} if no state could be found
     */
    @Nullable
    WorkTreeState findById(long id);

    /**
     * Delete all work tree states for a given repository.
     *
     * @param repositoryId repository to delete work tree states for
     * @return number of work tree states removed
     */
    @Nonnull
    List<WorkTreeState> findByRepository(@Nonnull String repositoryId);

    /**
     * @return a list of all work tree IDs, both expired and active
     */
    @Nonnull
    List<Long> getAllIds();
}
