package com.airlock.iam.flow.shared.domain.model.captcha;

import com.airlock.iam.base.api.domain.model.mapification.Mapifiable;
import com.airlock.iam.base.api.domain.model.mapification.MapifiedValues;
import com.airlock.iam.base.api.domain.model.plugin.LogicForConfig;
import com.airlock.iam.base.domain.model.mapification.MapifiedValuesBuilder;
import com.airlock.iam.flow.shared.api.domain.model.captcha.CaptchaChallenge;
import com.airlock.iam.flow.shared.application.configuration.captcha.CustomCaptchaConfig;
import com.airlock.iam.flow.shared.domain.service.captcha.CaptchaValidationService;
import jakarta.inject.Inject;
import jakarta.inject.Provider;

@LogicForConfig(CustomCaptchaConfig.class)
public final class CustomCaptcha extends ReCaptchaBasedProvider {
   private final Provider<CustomCaptchaType> customCaptchaType;

   @Inject
   public CustomCaptcha(Provider<CaptchaCredentials> credentials, Provider<CaptchaValidationService> validator, Provider<CustomCaptchaType> customCaptchaType) {
      super(credentials, validator);
      this.customCaptchaType = customCaptchaType;
   }

   @Override
   public CaptchaChallenge getChallenge() {
      return new CustomCaptcha.CustomCaptchaChallenge(this.getKey(), ((CustomCaptchaType)this.customCaptchaType.get()).type());
   }

   public record CustomCaptchaChallenge(String key, String type) implements CaptchaChallenge, Mapifiable {
      private static final String KEY_KEY = "key";
      private static final String TYPE_KEY = "type";

      @Override
      public MapifiedValues mapify() {
         return MapifiedValuesBuilder.mapifiedValues().put("key", this.key).put("type", this.type).build();
      }

      public static CustomCaptcha.CustomCaptchaChallenge demapify(MapifiedValues values) {
         return new CustomCaptcha.CustomCaptchaChallenge(values.get("key"), values.get("type"));
      }
   }
}
