package kr.co.accura.wtm.api;

import kr.co.accura.wtm.domain.teal.dto.TealEntryDto;
import kr.co.accura.wtm.domain.teal.dto.TealVersionDto;
import kr.co.accura.wtm.domain.teal.service.TealService;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/api/wtm/projects/{projectId}/teal")
@RequiredArgsConstructor
public class TealController {

    private final TealService tealService;

    @PostMapping("/upload")
    public ResponseEntity<TealVersionDto> upload(
            @PathVariable Long projectId,
            @RequestParam("file") MultipartFile file,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate effectiveDate,
            @RequestParam(required = false) String description) {
        return ResponseEntity.ok(tealService.uploadTeal(projectId, file, effectiveDate, description));
    }

    @GetMapping("/versions")
    public ResponseEntity<List<TealVersionDto>> getVersions(@PathVariable Long projectId) {
        return ResponseEntity.ok(tealService.getVersions(projectId));
    }

    @GetMapping("/active")
    public ResponseEntity<List<TealEntryDto>> getActiveEntries(@PathVariable Long projectId) {
        return ResponseEntity.ok(tealService.getActiveTealEntries(projectId));
    }

    @GetMapping("/by-wbs/{wbsId}")
    public ResponseEntity<List<TealEntryDto>> getByWbs(@PathVariable Long projectId,
                                                       @PathVariable Long wbsId) {
        return ResponseEntity.ok(tealService.getEntriesByWbs(projectId, wbsId));
    }
}
