package com.r11.tools.controller;

import com.r11.tools.model.EventRequestDto;
import com.r11.tools.service.EtrackService;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * It's the REST api for {@link com.r11.tools.model.Event}
 * @author Gabriel Modzelewski
 */
@RestController
@RequestMapping(path = "/api/event")
@AllArgsConstructor
public class EventController {
    private final EtrackService service;

    /**
     * Returns the list of Events in given time bracket.
     * The list of objects is received via {@link EventRequestDto}, which contains time brackets,
     * as well as the key - uuid.
     * @param event EventRequestDto object that contains data needed to query the database
     * @return list of {@link com.r11.tools.model.Event}
     */
    @PostMapping
    public ResponseEntity filterHistory(@RequestBody EventRequestDto event){
        return new ResponseEntity(service.getEventsByDateTimeAndBusinessKeys(event), HttpStatus.OK);
    }

    /**
     * Returns the list of Events of last 24h from given date.
     * @param uuid unique id of message list
     * @param messageId unique id of message in message list
     * @return list of {@link com.r11.tools.model.Event}
     */
    @GetMapping(path = "/{uuid}/{messageId}")
    public ResponseEntity getLastDay(@PathVariable UUID uuid,
                                     @PathVariable Integer messageId){
        LocalDateTime requestTime = LocalDateTime.now();
        LocalDateTime dayBeforeRequest = requestTime.minusDays(1L);
        EventRequestDto eventRequestDto = EventRequestDto.builder()
                .clientUUID(uuid)
                .mockedResponseId(messageId)
                .localDateTimeFrom(dayBeforeRequest)
                .localDateTimeTo(requestTime)
                .build();
        return new ResponseEntity(service.getEventsByDateTimeAndBusinessKeys(eventRequestDto), HttpStatus.OK);
    }


}

