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

import com.airlock.iam.authentication.application.service.saml2.idp.Saml2SpInitiatedSsoAccessService;
import com.airlock.iam.authentication.infrastructure.transfer.request.Saml2IdpAccessRequest;
import com.airlock.iam.common.api.infrastructure.annotation.DisableResponseDelay;
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.saml2.domain.model.idp.Saml2RequestId;
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.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("/saml2/idp/sso/init")
public class Saml2IdpAuthenticationFlowResource {
   private final Provider<Saml2SpInitiatedSsoAccessService> service;
   private final Provider<FlowResultMapper> mapper;

   @Inject
   public Saml2IdpAuthenticationFlowResource(Provider<Saml2SpInitiatedSsoAccessService> service, Provider<FlowResultMapper> mapper) {
      this.service = service;
      this.mapper = mapper;
   }

   @ApiResponses(
      {
            @ApiResponse(
               responseCode = "200",
               description = "Authorization is complete, target application can be accessed.",
               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>UNEXPECTED_CALL</td>\n<td>A flow is already in progress. To start a new flow, the running flow must first be terminated.</td>\n</tr>\n</table>\n</div>\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"))
               }
            ),
            @ApiResponse(
               responseCode = "401",
               description = "<div class=\"iam-error-code\"><table>\n<tr>\n<td>NOT_AUTHORIZED</td>\n<td>Access not yet granted, more authentication/authorization steps are required.</td>\n</tr>\n</table>\n</div>\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"))
               }
            ),
            @ApiResponse(
               responseCode = "403",
               description = "Access to this target application cannot be granted.",
               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 = "Access a target application by SAML request ID for SP-initiated SSO. If a flow is already in progress,\nan error will be returned.\n<p>\nRetrieving information about ongoing flows can be done by calling\n<tt><span class=\"httpMethod small GET\">GET</span>/public/flow/</tt>.\n</p>\n<div class=\"iam-resource-return\">A success response without next step if the client is authorized to access the\n target application or an error response if further steps are required.</div>",
      tags = "SAML",
      summary = "Access app by request ID"
   )
   @POST
   @Produces({"application/vnd.api+json", "application/json;qs=0.9"})
   @Consumes("application/json")
   @DisableResponseDelay
   public Response access(@Parameter(description = "Containing the request ID for SP-initiated SSO.") @NotNull @Valid Saml2IdpAccessRequest request) {
      String requestId = request.getRequestId();
      FlowResult flowResult = ((Saml2SpInitiatedSsoAccessService)this.service.get()).startNewFlowBySamlRequestId(Saml2RequestId.requestId(requestId));
      return ((FlowResultMapper)this.mapper.get()).toResponse(flowResult);
   }
}
