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

import com.airlock.iam.base.infrastructure.error.ErrorResponse;
import com.airlock.iam.common.api.domain.model.persistency.exception.PersisterException;
import com.airlock.iam.flow.shared.domain.service.cronto.CrontoOrderAdditionalLetterService;
import com.airlock.iam.login.rest.infrastructure.logger.ProtectedHelpdeskLogger;
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("/my/tokens/cronto/activation-letters")
public class UserCrontoLetterResource {
   private final ProtectedHelpdeskLogger helpdeskLogger;
   private final Provider<CrontoOrderAdditionalLetterService> service;

   @Inject
   public UserCrontoLetterResource(ProtectedHelpdeskLogger logger, Provider<CrontoOrderAdditionalLetterService> service) {
      this.helpdeskLogger = logger;
      this.service = service;
   }

   @ApiResponses(
      {
            @ApiResponse(responseCode = "204", description = "The letter has been ordered."),
            @ApiResponse(
               responseCode = "400",
               description = "The letter could not be ordered.",
               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 = "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 = "404",
               description = "The user has no Cronto account.",
               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 = "> [!caution]\n> This endpoint is deprecated. Use the dedicated protected self-service flow step instead.\n<p>Re-orders a Cronto activation letter.</p>\n<p>\nAny existing letter is deleted and replaced with the new letter.\nDuring this time until the customer receives the new letter,\nthe customer cannot activate another Cronto app or device.\n</p>\n<p>\nRe-ordering a Cronto activation letter is not always possible,\ne.g. if this feature is not configured or when the user doesn't have an existing\nCronto letter or a letter has been ordered too recently.\n</p>\n<div class=\"iam-resource-return\">empty document</div>",
      tags = "Cronto",
      summary = "Re-order letter"
   )
   @POST
   @Path("/order")
   @Produces({"application/vnd.api+json", "application/json;qs=0.9"})
   @Deprecated
   public Response orderLetter() {
      try {
         CrontoOrderAdditionalLetterService.OrderResult orderResult = this.service().orderAdditionalLetter();
         if (CrontoOrderAdditionalLetterService.OrderResult.OK == orderResult) {
            this.helpdeskLogger.log("Ordered additional Cronto activation letter");
            return Response.noContent().build();
         } else {
            this.helpdeskLogger.log("Failed to order additional Cronto activation letter (%s)", orderResult);
            return CrontoOrderAdditionalLetterService.OrderResult.NO_CRONTO_ACCOUNT == orderResult
               ? ErrorResponse.notFound().build().toResponse()
               : ErrorResponse.badRequest().build().toResponse();
         }
      } catch (PersisterException | RuntimeException e) {
         this.helpdeskLogger.log("Failed to order additional Cronto activation letter (technical failure)");
         return ErrorResponse.badRequest().build().toResponse();
      }
   }

   private CrontoOrderAdditionalLetterService service() {
      return (CrontoOrderAdditionalLetterService)this.service.get();
   }
}
