package org.octopus.internal.web.controllers

import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.validation.Valid
import lombok.AllArgsConstructor
import org.octopus.internal.common.models.DetailedReport
import org.octopus.internal.common.models.Report
import org.octopus.internal.services.ReportService
import org.octopus.internal.web.dtos.ReportDto
import org.octopus.internal.web.utils.responses.WebResponse
import org.springframework.format.annotation.DateTimeFormat
import org.springframework.web.bind.annotation.*
import java.util.Date

@RestController
@RequestMapping("/reports")
@AllArgsConstructor
@Tag(name = "Reports Management", description = "Operations related to reports.")
class ReportController(
    val reportService: ReportService
) {

    @PostMapping
    fun createReport(@Valid @RequestBody reportDto: ReportDto): WebResponse<Report> {
        return WebResponse.ok(reportService.createReport(reportDto))
    }

    @PostMapping("/projections")
    fun generateProjections(@RequestParam("endDate", required = true) @DateTimeFormat(pattern = "yyyy-MM-dd") endDate: Date): WebResponse<Boolean> {
        return WebResponse.ok(reportService.generateProjections(endDate))
    }

    @GetMapping("/resume")
    fun getAllReports(
        @RequestParam("endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") endDate: Date?): WebResponse<List<Report>> {
        return WebResponse.ok(reportService.getAllReports(endDate))
    }

    @GetMapping
    fun getAllReportsWithDetails(
        @RequestParam("endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") endDate: Date?): WebResponse<List<DetailedReport>> {
            return WebResponse.ok(reportService.getAllReportsWithDetails(endDate))

    }

    @PutMapping("/{id}")
    fun updateReport(@PathVariable("id") id: Long,
                     @Valid @RequestBody reportDto: ReportDto): WebResponse<Report> {
        return WebResponse.ok(reportService.updateReport(id, reportDto))
    }

    @GetMapping("/correction")
    fun getCorrectionValue(
        @RequestParam("endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") endDate: Date?): WebResponse<List<Report>> {
        return WebResponse.ok(reportService.getAllReportsWithAvgCorrection(endDate))
    }

}