package com.atlassian.stash.internal.repository.ref.restriction.dao.v0;

import com.atlassian.bitbucket.user.ApplicationUser;
import com.google.common.base.Function;
import net.java.ao.Accessor;
import net.java.ao.Preload;
import net.java.ao.RawEntity;
import net.java.ao.schema.AutoIncrement;
import net.java.ao.schema.NotNull;
import net.java.ao.schema.PrimaryKey;
import net.java.ao.schema.Table;

import javax.annotation.Nullable;

@Table("PERMITTED_ENTITY")
@Preload
public interface AoPermittedEntityV0 extends RawEntity<Integer> {

    String ID_COLUMN = "ENTITY_ID";
    String RESTRICTED_ACCESSOR_VALUE = "FK_RESTRICTED_";
    String RESTRICTED_COLUMN = RESTRICTED_ACCESSOR_VALUE + "ID";
    String USER_ID_COLUMN = "USER_ID";
    String GROUP_NAME_COLUMN = "GROUP_ID";

    @AutoIncrement
    @NotNull
    @PrimaryKey(ID_COLUMN)
    Integer getId();

    @NotNull
    @Accessor(RESTRICTED_ACCESSOR_VALUE)
    @SuppressWarnings("unused")
    AoRestrictedRefV0 getRestricted();

    /**
     * @return the group the permission was granted to. Can be null.
     */
    @Accessor(GROUP_NAME_COLUMN)
    @Nullable
    String getGroup();

    /**
     * @return the user the permission was granted to. Can be null.
     */
    @Accessor(USER_ID_COLUMN)
    @Nullable
    Integer getUserId();

    /**
     * {@link com.google.common.base.Function} to retrieve the {@link ApplicationUser#getId() user ID} from a
     * {@code AoPermittedEntity}.
     */
    Function<AoPermittedEntityV0, Integer> TO_USER_ID = new Function<AoPermittedEntityV0, Integer>() {
        public Integer apply(AoPermittedEntityV0 entity) {
            return entity.getUserId();
        }
    };

}
