package com.airlock.iam.admin.infrastructure.resource.prot;

import com.airlock.iam.admin.infrastructure.generated.transfer.response.LogsDataResourceDocument;
import com.airlock.iam.admin.infrastructure.service.RestResponseLogsService;
import com.airlock.iam.base.infrastructure.validation.DocumentedFilterParameter;
import com.airlock.iam.base.infrastructure.validation.DocumentedSortParameter;
import com.airlock.iam.base.infrastructure.validation.SanitizedPageParameter;
import com.airlock.iam.base.infrastructure.validation.ValidPageParameter;
import com.airlock.iam.common.api.infrastructure.annotation.DisableCsrfFilter;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.extensions.Extension;
import io.swagger.v3.oas.annotations.extensions.ExtensionProperty;
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.BeanParam;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.Response;

@Path("/logs")
public class LogResource {
   private final Provider<RestResponseLogsService> logService;

   @Inject
   public LogResource(Provider<RestResponseLogsService> logService) {
      this.logService = logService;
   }

   @ApiResponses(
      {
            @ApiResponse(
               responseCode = "200",
               description = "The response contains logs found.",
               content = {
                     @Content(mediaType = "text/plain", schema = @Schema(implementation = String.class)),
                     @Content(mediaType = "application/vnd.api+json", schema = @Schema(implementation = LogsDataResourceDocument.class)),
                     @Content(mediaType = "application/json", schema = @Schema(implementation = LogsDataResourceDocument.class))
               }
            ),
            @ApiResponse(
               responseCode = "404",
               description = "Logfile name/key not found.",
               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"))
               }
            )
      }
   )
   @GET
   @Operation(
      description = "Retrieve the logs of a particular log file. To filter the content of a log,\nuse 'filter[content]=...'. The limit the number of logs returned, use 'page[limit]=...'.\nAnd for sorting, choose one of 'sort=timestamp', 'sort=message' or 'sort=signature'.\n<div class=\"iam-resource-return\">The logs applying to the filter, sort and page parameters in the requested log file.</div>",
      extensions = @Extension(properties = @ExtensionProperty(name = "x-internal", value = "true")),
      tags = "Logs",
      summary = "Get logs from file"
   )
   @Path("{logKey}")
   @Produces({"text/plain", "application/vnd.api+json", "application/json;qs=0.9"})
   @DisableCsrfFilter(RestResponseLogsService.HasPlaintextExportParameter.class)
   public Response getLogs(
      @Parameter(description = "The key of the log file as defined in the configuration.") @PathParam("logKey") String logKey,
      @Parameter(description = "Restricts the set of logs to those matching the filter condition. Currently, only 'filter[content]' is supported.") @BeanParam DocumentedFilterParameter filterParameter,
      @Parameter(description = "Sorts the logs. Sort is possible by 'timestamp', 'message' or 'signature'.") @BeanParam DocumentedSortParameter sortParameter,
      @Parameter(description = "Restricts the logs to those on the requested page. Currently, only 'page[limit]' is supported.") @ValidPageParameter @BeanParam SanitizedPageParameter pageParameter,
      @Parameter(description = "(optional) request for exporting the data in a particular format (currently supported: 'plaintext')") @QueryParam("export") String export
   ) {
      return "plaintext".equals(export)
         ? ((RestResponseLogsService)this.logService.get())
            .getLogs(
               logKey, filterParameter, sortParameter, pageParameter, RestResponseLogsService.LogResponseFormat.PLAIN_TEXT, pageParameter.getDefaultPageSize()
            )
         : ((RestResponseLogsService)this.logService.get())
            .getLogs(logKey, filterParameter, sortParameter, pageParameter, RestResponseLogsService.LogResponseFormat.JSON, pageParameter.getDefaultPageSize());
   }
}
