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

import com.airlock.iam.authentication.application.service.kerberos.KerberosAuthentication;
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 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 io.swagger.v3.oas.annotations.responses.ApiResponses;
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("/kerberos")
public final class KerberosAuthenticationResource {
   private final FlowResultMapper mapper;
   private final Provider<KerberosAuthentication> kerberos;

   @Inject
   public KerberosAuthenticationResource(FlowResultMapper mapper, Provider<KerberosAuthentication> kerberos) {
      this.mapper = mapper;
      this.kerberos = kerberos;
   }

   @ApiResponses(
      {
            @ApiResponse(
               responseCode = "200",
               description = "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))
               }
            ),
            @ApiResponse(
               responseCode = "403",
               description = "<div class=\"iam-error-code\"><table>\n<tr>\n<td>KERBEROS_AUTHENTICATION_NOT_POSSIBLE</td>\n<td>Flow has been aborted since no alternative to Kerberos is available.</td>\n</tr>\n</table></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"))
               }
            )
      }
   )
   @Operation(
      description = "Notify the flow step that Kerberos is not possible on the client.\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.</div>",
      tags = "Flow Control",
      summary = "Kerberos not possible"
   )
   @POST
   @Path("/not-possible")
   @Produces({"application/vnd.api+json", "application/json;qs=0.9"})
   public Response handleKerberosNotPossible() {
      FlowResult result = ((KerberosAuthentication)this.kerberos.get()).handleKerberosNotPossible();
      return this.mapper.toResponse(result);
   }
}
