package kr.co.accura.wtm.api;

import kr.co.accura.wtm.domain.report.dto.ProjectHoursReport;
import kr.co.accura.wtm.domain.report.dto.ReportFilter;
import kr.co.accura.wtm.domain.report.dto.WbsHoursReport;
import kr.co.accura.wtm.domain.report.service.ReportService;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@RestController
@RequestMapping("/api/wtm/reports")
@RequiredArgsConstructor
public class ReportController {

    private final ReportService reportService;

    @GetMapping("/project-hours")
    public ProjectHoursReport projectHours(
            @RequestParam(required = false) Long projectId,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate from,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate to,
            @RequestParam(required = false) String groupBy) {
        return reportService.getProjectHoursReport(
                new ReportFilter(projectId, from, to, groupBy, null, null));
    }

    @GetMapping("/project-hours/export")
    public ProjectHoursReport projectHoursExport(
            @RequestParam(required = false) Long projectId,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate from,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate to,
            @RequestParam(required = false) String groupBy) {
        // TODO: Return Excel byte[] with proper content type
        return reportService.getProjectHoursReport(
                new ReportFilter(projectId, from, to, groupBy, null, null));
    }

    @GetMapping("/wbs-hours")
    public WbsHoursReport wbsHours(
            @RequestParam(required = false) Long projectId,
            @RequestParam(required = false) Integer wbsLevel,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate from,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate to) {
        return reportService.getWbsHoursReport(
                new ReportFilter(projectId, from, to, null, wbsLevel, null));
    }

    @GetMapping("/wbs-hours/export")
    public WbsHoursReport wbsHoursExport(
            @RequestParam(required = false) Long projectId,
            @RequestParam(required = false) Integer wbsLevel,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate from,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate to) {
        // TODO: Return Excel byte[] with proper content type
        return reportService.getWbsHoursReport(
                new ReportFilter(projectId, from, to, null, wbsLevel, null));
    }

    @GetMapping("/phase-ratio")
    public ProjectHoursReport phaseRatio(
            @RequestParam(required = false) Long projectId,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate from,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate to) {
        // TODO: Implement phase ratio logic
        return reportService.getProjectHoursReport(
                new ReportFilter(projectId, from, to, null, null, null));
    }

    @GetMapping("/np-ratio")
    public ProjectHoursReport npRatio(
            @RequestParam(required = false) String department,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate from,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate to) {
        // TODO: Implement NP ratio logic
        return reportService.getProjectHoursReport(
                new ReportFilter(null, from, to, null, null, department));
    }
}
