/*
 * Copyright 2017-2023 EPAM Systems, Inc. (https://www.epam.com/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.epam.pipeline.controller.log;

import com.epam.pipeline.controller.AbstractRestController;
import com.epam.pipeline.controller.Result;
import com.epam.pipeline.entity.log.LogEntry;
import com.epam.pipeline.entity.log.LogFilter;
import com.epam.pipeline.entity.log.LogPagination;
import com.epam.pipeline.acl.log.LogApiService;
import com.epam.pipeline.entity.log.LogRequest;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
public class LogController extends AbstractRestController {

    private final LogApiService logApiService;


    @PostMapping(value = "/log/filter")
    @ApiOperation(
            value = "Filter logs.",
            notes = "Filter logs.",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(
            value = {@ApiResponse(code = HTTP_STATUS_OK, message = API_STATUS_DESCRIPTION)
            })
    public Result<LogPagination> filter(@RequestBody LogFilter logFilter) {
        return Result.success(logApiService.filter(logFilter));
    }

    @GetMapping(value = "/log/filter")
    @ApiOperation(
            value = "Get possible values for filters.",
            notes = "Get possible values for filters.",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(
            value = {@ApiResponse(code = HTTP_STATUS_OK, message = API_STATUS_DESCRIPTION)
            })
    public Result<LogFilter> filter() {
        return Result.success(logApiService.getFilters());
    }

    @PostMapping(value = "/log/group")
    @ApiOperation(
            value = "Filter and group logs by a field.",
            notes = "Filter and group logs by a field.",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(
            value = {@ApiResponse(code = HTTP_STATUS_OK, message = API_STATUS_DESCRIPTION)
            })
    public Result<Map<String, Long>> group(@RequestBody final LogRequest logRequest) {
        return Result.success(logApiService.group(logRequest));
    }

    @PostMapping(value = "/log")
    @ApiOperation(
            value = "Save logs.",
            notes = "Save logs.",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(
            value = {@ApiResponse(code = HTTP_STATUS_OK, message = API_STATUS_DESCRIPTION)
            })
    public Result<Boolean> save(@RequestBody final List<LogEntry> logEntries) {
        logApiService.save(logEntries);
        return Result.success();
    }
}
