package com.airlock.iam.publicselfservice.infrastructure.resource;

import com.airlock.iam.common.api.domain.model.otp.UnverifiedOtp;
import com.airlock.iam.flow.api.domain.model.flow.FlowResult;
import com.airlock.iam.flow.api.infrastructure.mapper.FlowResultMapper;
import com.airlock.iam.flow.shared.application.service.otp.OathOtpAuthentication;
import com.airlock.iam.flow.shared.infrastructure.transfer.request.GenericOtpCheckRequest;
import com.airlock.iam.publicselfservice.api.infrastructure.generated.transfer.response.PublicSelfServiceFlowResultDataResourceDocument;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.inject.Inject;
import jakarta.inject.Provider;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Response;

@Path("/approval/oath")
public class PublicSelfServiceOathOtpApprovalResource {
   private static final String CHECK_OATH_OTP_REQ_EXAMPLE = "{\"otp\":\"654321\"}";
   private final FlowResultMapper mapper;
   private final Provider<OathOtpAuthentication> oathOtpAuthentication;

   @Inject
   public PublicSelfServiceOathOtpApprovalResource(FlowResultMapper mapper, Provider<OathOtpAuthentication> oathAuthentication) {
      this.mapper = mapper;
      this.oathOtpAuthentication = oathAuthentication;
   }

   @ApiResponses(
      {
            @ApiResponse(
               responseCode = "200",
               description = "OTP successfully verified.",
               content = {
                     @Content(mediaType = "application/vnd.api+json", schema = @Schema(implementation = PublicSelfServiceFlowResultDataResourceDocument.class)),
                     @Content(mediaType = "application/json", schema = @Schema(implementation = PublicSelfServiceFlowResultDataResourceDocument.class))
               }
            ),
            @ApiResponse(
               responseCode = "403",
               description = "<div class=\"iam-error-code\"><table>\n<tr>\n<td>OATH_OTP_WRONG</td>\n<td>The OTP could not be validated successfully.</td>\n</tr>\n</table>\n</div>\n<br>The current public self-service flow has been aborted.\n<br>Possible next step: none.\n",
               content = {
                     @Content(mediaType = "application/vnd.api+json", schema = @Schema(ref = "json-api.yaml#/components/schemas/ErrorDocumentResponse")),
                     @Content(mediaType = "application/json", schema = @Schema(ref = "json-api.yaml#/components/schemas/ErrorDocumentResponse"))
               }
            )
      }
   )
   @Operation(
      description = "Checks the submitted OATH OTP. Note that unlike identity verification, approval REST calls require an existing user\nand cannot prevent username enumeration\n<div class=\"iam-resource-return\">Whether the check was successful. If the attribute <tt>nextAuthStep</tt> is present in the response,\n further steps are required to successfully authenticate.</div>",
      tags = "OATH OTP",
      summary = "Check OATH OTP"
   )
   @POST
   @Path("/otp/check")
   @Consumes("application/json")
   @Produces({"application/vnd.api+json", "application/json;qs=0.9"})
   public Response checkOathOtp(
      @RequestBody(
   description = "Request containing the OATH OTP.",
   content = @Content(examples = @ExampleObject(name = "Check OATH OTP", value = "{\"otp\":\"654321\"}"))
) @NotNull @Valid GenericOtpCheckRequest request
   ) {
      FlowResult result = ((OathOtpAuthentication)this.oathOtpAuthentication.get()).checkOtp(UnverifiedOtp.unverifiedOtp(request.getOtp()));
      return this.mapper.toResponse(result);
   }
}
