package com.coreoz.plume.admin.webservices.logApi;

import com.coreoz.plume.admin.db.daos.LogApiTrimmed;
import com.coreoz.plume.admin.jersey.feature.RestrictToAdmin;
import com.coreoz.plume.admin.services.configuration.LogApiConfigurationService;
import com.coreoz.plume.admin.services.logApi.LogApiBean;
import com.coreoz.plume.admin.services.logApi.LogApiService;
import com.coreoz.plume.admin.services.permission.ApiLogAdminPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import java.time.Instant;
import java.util.List;

@Path("/admin/logs")
@Api("Application HTTP API trace")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RestrictToAdmin(ApiLogAdminPermissions.MANAGE_API_LOGS)
@Singleton
public class LogApiWs {
    private LogApiService logApiService;
    private static Integer DEFAULT_LIMIT;

    @Inject
    public LogApiWs(LogApiService logApiService, LogApiConfigurationService logApiConfigurationService) {
        this.logApiService = logApiService;
        DEFAULT_LIMIT = logApiConfigurationService.defaultLimit();
    }

    @GET
    @ApiOperation("Fetch API trimmed logs (without request/response bodies) by query filters")
    public List<LogApiTrimmed> fetchAllLogs(
        @QueryParam("limit") Integer limit,
        @QueryParam("method") String method,
        @QueryParam("statusCode") Integer statusCode,
        @QueryParam("apiName") String apiName,
        @QueryParam("url") String url,
        @QueryParam("startDate") Instant startDate,
        @QueryParam("endDate") Instant endDate
    ) {
        if (limit == null || limit == 0) {
            limit = DEFAULT_LIMIT;
        }
        return logApiService.fetchAllTrimmedLogs(limit, method, statusCode, apiName, url, startDate, endDate);
    }

    @GET
    @ApiOperation("Fetch the headers and the trimmed body of a request/response")
    @Path("/{idLog}")
    public LogApiBean details(@PathParam("idLog") Long id) {
        return logApiService.fetchLogDetails(id);
    }

    @GET
    @ApiOperation("Download the body of a request or a response")
    @Path("/{idLog}/{isRequest}")
    public Response bodyFile(@PathParam("idLog") Long id, @PathParam("isRequest") Boolean isRequest) {
        return logApiService
            .findBodyPart(id, isRequest)
            .map(bodyPart -> Response
                .ok(bodyPart.getBody().getBytes())
                .header(
                    "Content-Disposition",
                    "attachment; filename=\"" + bodyPart.getApiName() + "." + bodyPart.getFileExtension() + "\""
                )
                .build()
            )
            .orElseGet(() -> Response.status(Status.NOT_FOUND).build());
    }

}

@@ -45,16 +45,404 @@
 ogApi;%0A%0A
+import java.time.Instant;%0Aimport java.util.List;%0A%0Aimport javax.inject.Inject;%0Aimport javax.inject.Singleton;%0Aimport javax.ws.rs.Consumes;%0Aimport javax.ws.rs.GET;%0Aimport javax.ws.rs.Path;%0Aimport javax.ws.rs.PathParam;%0Aimport javax.ws.rs.Produces;%0Aimport javax.ws.rs.QueryParam;%0Aimport javax.ws.rs.core.MediaType;%0Aimport javax.ws.rs.core.Response;%0Aimport javax.ws.rs.core.Response.Status;%0A%0A
 import c
@@ -822,16 +822,17 @@
 ssions;%0A
+%0A
 import i
@@ -907,395 +907,8 @@
 n;%0A%0A
-import javax.inject.Inject;%0Aimport javax.inject.Singleton;%0Aimport javax.ws.rs.Consumes;%0Aimport javax.ws.rs.GET;%0Aimport javax.ws.rs.Path;%0Aimport javax.ws.rs.PathParam;%0Aimport javax.ws.rs.Produces;%0Aimport javax.ws.rs.QueryParam;%0Aimport javax.ws.rs.core.MediaType;%0Aimport javax.ws.rs.core.Response;%0Aimport javax.ws.rs.core.Response.Status;%0Aimport java.time.Instant;%0Aimport java.util.List;%0A%0A
 @Pat
@@ -1184,23 +1184,16 @@
 ate 
-static 
 Integer 
 DEFA
@@ -1188,29 +1188,30 @@
 Integer 
-DEFAULT_LIMIT
+maxLogsToFetch
 ;%0A%0A    @
@@ -1375,29 +1375,35 @@
         
-DEFAULT_LIMIT
+this.maxLogsToFetch
  = logAp
@@ -1971,16 +1971,42 @@
 mit == 0
+ %7C%7C limit %3E maxLogsToFetch
 ) %7B%0A    
@@ -2025,21 +2025,22 @@
 t = 
-DEFAULT_LIMIT
+maxLogsToFetch
 ;%0A  

Fix limit