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

import com.airlock.iam.authentication.infrastructure.generated.transfer.response.SelectionOptionDataCollectionDocument;
import com.airlock.iam.authentication.infrastructure.transfer.response.SelectionOptionData;
import com.airlock.iam.flow.api.domain.model.flow.FlowResult;
import com.airlock.iam.flow.api.domain.model.selection.Option;
import com.airlock.iam.flow.api.infrastructure.mapper.FlowResultMapper;
import com.airlock.iam.flow.application.configuration.selection.step.SelectionOptions;
import com.airlock.iam.flow.auth.api.infrastructure.generated.transfer.response.AuthenticationFlowResultDataResourceDocument;
import com.airlock.iam.flow.domain.model.selection.step.InteractiveSelection;
import com.airlock.iam.flow.infrastructure.mapper.FlowResultToCollectionDocumentMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
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 io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.inject.Inject;
import jakarta.inject.Provider;
import jakarta.validation.constraints.Size;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Response;
import ma.glasnost.orika.MapperFacade;

@Path("selection")
public final class AuthenticationInteractiveSelectionResource {
   private final Provider<InteractiveSelection> selection;
   private final FlowResultMapper mapper;
   private final MapperFacade mapperFacade;

   @Inject
   public AuthenticationInteractiveSelectionResource(Provider<InteractiveSelection> selection, FlowResultMapper mapper, MapperFacade mapperFacade) {
      this.selection = selection;
      this.mapper = mapper;
      this.mapperFacade = mapperFacade;
   }

   @ApiResponse(
      responseCode = "200",
      description = "Available selection options retrieved.",
      content = {
            @Content(mediaType = "application/vnd.api+json", schema = @Schema(implementation = SelectionOptionDataCollectionDocument.class)),
            @Content(mediaType = "application/json", schema = @Schema(implementation = SelectionOptionDataCollectionDocument.class))
      }
   )
   @Deprecated
   @Operation(
      description = "> [!caution]\n> This endpoint is deprecated. The selection options are returned as <a href=\"#additionalAuthAttributes\">additional attribute</a> with the <tt>SELECTION_REQUIRED</tt> next step code.\n<p>Retrieves the available options the user can choose from to proceed in the authentication process.</p>\n<div class=\"iam-resource-return\">The available options.</div>",
      tags = "Flow Control",
      summary = "List options"
   )
   @POST
   @Path("options/retrieve")
   @Produces({"application/vnd.api+json", "application/json;qs=0.9"})
   public Response retrieveOptions() {
      FlowResult result = ((InteractiveSelection)this.selection.get()).getOptions();
      return FlowResultToCollectionDocumentMapper.flowResultDataToCollectionDocumentMapper(this.mapper, this.mapperFacade)
         .map(result, SelectionOptions.class, SelectionOptionData.class, Option::getName);
   }

   @ApiResponses(
      {
            @ApiResponse(
               responseCode = "200",
               description = "Selected the chosen step and requires next step actions.",
               content = {
                     @Content(mediaType = "application/vnd.api+json", schema = @Schema(implementation = AuthenticationFlowResultDataResourceDocument.class)),
                     @Content(mediaType = "application/json", schema = @Schema(implementation = AuthenticationFlowResultDataResourceDocument.class))
               }
            ),
            @ApiResponse(
               responseCode = "400",
               description = "<div class=\"iam-error-code\"><table>\n<tr>\n<td>SELECTION_FAILED</td>\n<td>The selected option is not available.</td>\n</tr>\n</table>\n</div>\n<br>Possible next step: <tt>SELECTION_REQUIRED</tt> (as documented <a href=\"#nextAuthStepCodes\">here</a>).\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 = "Selects the given option if available.\n<div class=\"iam-resource-return\">The selected next step or an error if the option is invalid.</div>",
      tags = "Flow Control",
      summary = "Select option"
   )
   @POST
   @Path("options/{id}/select")
   @Consumes("application/json")
   @Produces({"application/vnd.api+json", "application/json;qs=0.9"})
   public Response selectOption(@Parameter(description = "option to select.") @PathParam("id") @Size(min = 1, max = 1000) String id) {
      FlowResult result = ((InteractiveSelection)this.selection.get()).select(Option.of(id));
      return this.mapper.toResponse(result);
   }
}
