package kr.co.accura.wtm.api;

import kr.co.accura.wtm.domain.wbs.dto.*;
import kr.co.accura.wtm.domain.wbs.dto.WbsDisciplineAssignmentDto;
import kr.co.accura.wtm.domain.wbs.service.WbsService;
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}")
@RequiredArgsConstructor
public class WbsController {

    private final WbsService wbsService;

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

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

    @GetMapping("/wbs/versions/{ver}")
    public ResponseEntity<List<WbsNodeDto>> getVersionNodes(@PathVariable Long projectId,
                                                            @PathVariable("ver") Integer ver) {
        return ResponseEntity.ok(wbsService.getVersionNodes(projectId, ver));
    }

    @PostMapping("/wbs/versions/{ver}/activate")
    public ResponseEntity<Void> activateVersion(@PathVariable Long projectId,
                                                @PathVariable("ver") Long ver) {
        wbsService.activateVersion(ver);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/canonical-wbs")
    public ResponseEntity<List<CanonicalWbsDto>> getCanonicalWbs(@PathVariable Long projectId) {
        return ResponseEntity.ok(wbsService.getCanonicalWbs(projectId));
    }

    @GetMapping("/wbs/compare")
    public ResponseEntity<WbsCompareResult> compare(@PathVariable Long projectId,
                                                    @RequestParam("a") int versionA,
                                                    @RequestParam("b") int versionB) {
        return ResponseEntity.ok(wbsService.compareVersions(projectId, versionA, versionB));
    }

    /* ── WBS-Discipline assignments ── */

    @GetMapping("/wbs-disciplines")
    public ResponseEntity<List<WbsDisciplineAssignmentDto>> getWbsDisciplines(
            @PathVariable Long projectId) {
        return ResponseEntity.ok(wbsService.getWbsDisciplines(projectId));
    }

    @PutMapping("/wbs-disciplines")
    public ResponseEntity<List<WbsDisciplineAssignmentDto>> saveWbsDisciplines(
            @PathVariable Long projectId,
            @RequestBody List<WbsDisciplineAssignmentDto> assignments) {
        return ResponseEntity.ok(wbsService.saveWbsDisciplines(projectId, assignments));
    }
}
