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

import com.airlock.iam.flow.api.domain.model.flow.Flow;
import com.airlock.iam.flow.api.domain.model.flow.FlowResult;
import com.airlock.iam.flow.api.infrastructure.mapper.FlowResultMapper;
import com.airlock.iam.flow.auth.api.infrastructure.generated.transfer.response.AuthenticationFlowResultDataResourceDocument;
import com.airlock.iam.flow.shared.domain.model.step.acknowledgemessage.AcknowledgeMessageStep;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.inject.Inject;
import jakarta.inject.Provider;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Response;

@Path("/message")
public class AuthenticationAcknowledgeMessageFlowResource {
   private final Provider<Flow> flow;
   private final FlowResultMapper mapper;

   @Inject
   public AuthenticationAcknowledgeMessageFlowResource(Provider<Flow> flow, FlowResultMapper mapper) {
      this.flow = flow;
      this.mapper = mapper;
   }

   @ApiResponse(
      responseCode = "200",
      description = "Authentication successful or further steps required (as documented <a href=\"#nextAuthStepCodes\">here</a>).",
      content = {
            @Content(mediaType = "application/vnd.api+json", schema = @Schema(implementation = AuthenticationFlowResultDataResourceDocument.class)),
            @Content(mediaType = "application/json", schema = @Schema(implementation = AuthenticationFlowResultDataResourceDocument.class))
      }
   )
   @Operation(
      description = "Acknowledges a previously received message.\nThe message can be a pre-configured message ID or server-generated message, depending on the step configuration.\nIt has been received as an additional in a previous step response (as documented <a href=\"#additionalAuthAttributes\">here</a>).\n<div class=\"iam-resource-return\">If the attribute <tt>nextAuthStep</tt> is present in the response,\n further steps are required to successfully authenticate. If the attribute is missing, the user is\n successfully authenticated.</div>",
      tags = "Message Acknowledgement",
      summary = "Acknowledge message"
   )
   @POST
   @Path("/acknowledge")
   @Produces({"application/vnd.api+json", "application/json;qs=0.9"})
   public Response acknowledgeMessageId() {
      FlowResult result = ((Flow)this.flow.get()).execute(AcknowledgeMessageStep::acknowledge, AcknowledgeMessageStep.class);
      return this.mapper.toResponse(result);
   }
}
