package com.sn.sowsysrestapi.api.controller;

import com.sn.sowsysrestapi.api.converter.CurrentTaskConverter;
import com.sn.sowsysrestapi.api.dto.CurrentTaskDTO;
import com.sn.sowsysrestapi.api.dto.input.CurrentTaskInputDTO;
import com.sn.sowsysrestapi.domain.entity.CurrentTask;
import com.sn.sowsysrestapi.domain.exception.BusinessException;
import com.sn.sowsysrestapi.domain.exception.CurrentTaskNotFoundException;
import com.sn.sowsysrestapi.domain.repository.CurrentTaskRepo;
import com.sn.sowsysrestapi.domain.service.CurrentTaskService;
import com.sn.sowsysrestapi.domain.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/reports/{reportId}/current-tasks")
public class CurrentTaskController {

    @Autowired
    private CurrentTaskRepo currentTaskRepo;

    @Autowired
    private CurrentTaskService currentTaskService;

    @Autowired
    private CurrentTaskConverter currentTaskConverter;

    @Autowired
    private ReportService reportService;

    @GetMapping
    public List<CurrentTaskDTO> listByReport(@PathVariable Long reportId){

        reportService.findOrFail(reportId);

        return currentTaskConverter.toCollectionDTO(currentTaskRepo.findAllByReportId(reportId));

    }

    @GetMapping("/{currentTaskId}")
    public CurrentTaskDTO find(@PathVariable Long reportId, @PathVariable Long currentTaskId){

        reportService.findOrFail(reportId);

        CurrentTask currentTask = currentTaskService.findOrFail(currentTaskId);

        CurrentTaskDTO currentTaskDTO = currentTaskConverter.toDto(currentTask);

        return currentTaskDTO;

    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public CurrentTaskDTO add(@PathVariable Long reportId,
                              @RequestBody @Valid CurrentTaskInputDTO currentTaskInputDTO){

        reportService.findOrFail(reportId);

        try {
            CurrentTask currentTask = currentTaskConverter.toEntity(currentTaskInputDTO);

            return currentTaskConverter.toDto(currentTaskService.save(reportId, currentTask));
        } catch (CurrentTaskNotFoundException e) {
            throw new BusinessException(e.getMessage());
        }

    }

    @PutMapping("/{currentTaskId}")
    public CurrentTaskDTO update(@PathVariable Long reportId, @PathVariable Long currentTaskId,
                                 @RequestBody @Valid CurrentTaskInputDTO currentTaskInputDTO) {

        reportService.findOrFail(reportId);

        try {
            CurrentTask currentTaskPresent = currentTaskService.findOrFail(currentTaskId);

            currentTaskConverter.copyToEntity(currentTaskInputDTO, currentTaskPresent);

            return currentTaskConverter.toDto(currentTaskService.save(reportId, currentTaskPresent));

        } catch (CurrentTaskNotFoundException e) {
            throw new BusinessException(e.getMessage());
        }

    }

    @DeleteMapping("/{currentTaskId}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void delete(@PathVariable Long reportId, @PathVariable Long currentTaskId) {

        reportService.findOrFail(reportId);

        currentTaskService.delete(currentTaskId);
    }


}
