package com.airlock.iam.publicselfservice.domain.service.restrictions;

import com.airlock.iam.base.api.domain.model.plugin.LogicForConfig;
import com.airlock.iam.common.domain.model.principal.UserPrincipal;
import com.airlock.iam.publicselfservice.api.domain.model.PublicSelfServiceRestriction;
import com.airlock.iam.publicselfservice.application.configuration.restrictions.NonexistentUserRestrictionConfig;
import jakarta.inject.Inject;
import jakarta.inject.Provider;
import java.util.Optional;

@LogicForConfig(NonexistentUserRestrictionConfig.class)
public class NonexistentUserRestriction implements PublicSelfServiceRestriction {
   private final Provider<NonexistentUserRestrictionConfig> config;
   private final Provider<UserPrincipal> userPrincipal;

   @Inject
   public NonexistentUserRestriction(Provider<NonexistentUserRestrictionConfig> config, Provider<UserPrincipal> userPrincipal) {
      this.config = config;
      this.userPrincipal = userPrincipal;
   }

   @Override
   public Optional<PublicSelfServiceRestriction.RestrictionViolation> getViolation() {
      return ((UserPrincipal)this.userPrincipal.get()).exists()
         ? Optional.empty()
         : Optional.of(
            new PublicSelfServiceRestriction.RestrictionViolation(
               NonexistentUserRestriction.NonexistentUserRestrictionCode.USER_NOT_FOUND,
               ((NonexistentUserRestrictionConfig)this.config.get()).isEnableFeedback()
            )
         );
   }

   public enum NonexistentUserRestrictionCode implements PublicSelfServiceRestriction.ViolationCode {
      USER_NOT_FOUND;
   }
}
