package com.airlock.iam.login.rest.infrastructure.resource.prot;

import ch.ergon.restal.jsonapi.document.data.CollectionDocument;
import ch.ergon.restal.jsonapi.document.data.ResourceObject;
import com.airlock.iam.authentication.domain.service.secretquestions.SecretQuestionsLoginService;
import com.airlock.iam.login.rest.infrastructure.generated.transfer.SecretQuestionDataCollectionDocument;
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.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Response;
import java.util.List;

@Path("/secret-questions")
public class SecretQuestionsResource {
   private final Provider<SecretQuestionsLoginService> secretQuestionsServiceProvider;

   @Inject
   public SecretQuestionsResource(Provider<SecretQuestionsLoginService> serviceProvider) {
      this.secretQuestionsServiceProvider = serviceProvider;
   }

   @ApiResponses(
      {
            @ApiResponse(
               responseCode = "200",
               description = "Available questions retrieved.",
               content = {
                     @Content(mediaType = "application/vnd.api+json", schema = @Schema(implementation = SecretQuestionDataCollectionDocument.class)),
                     @Content(mediaType = "application/json", schema = @Schema(implementation = SecretQuestionDataCollectionDocument.class))
               }
            ),
            @ApiResponse(
               responseCode = "401",
               description = "Invalid or missing authentication.",
               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 the requested service is forbidden. Authentication will not help.",
               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(
      tags = "Secret Questions",
      description = "Retrieves all available questions.\n<div class=\"iam-resource-return\">A collection document with all available question IDs.</div>",
      summary = "List questions"
   )
   @GET
   @Produces({"application/vnd.api+json", "application/json;qs=0.9"})
   public Response retrieveConfiguredSecretQuestions() {
      List<String> availableQuestions = this.getService().getAvailableQuestions();
      CollectionDocument<Void> document = new CollectionDocument();

      for (String questionId : availableQuestions) {
         ResourceObject<Void> resource = ResourceObject.builder("secret-question", questionId).build();
         document.add(resource);
      }

      return Response.ok(document).build();
   }

   private SecretQuestionsLoginService getService() {
      return (SecretQuestionsLoginService)this.secretQuestionsServiceProvider.get();
   }
}
