package com.zioinfo.esn.uiws.worklog.controller;

import com.zioinfo.esn.common.ApiResponse;
import com.zioinfo.esn.uiws.worklog.dto.WorklogDtos.*;
import com.zioinfo.esn.uiws.worklog.service.WorklogService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

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

/**
 * UIWS 이식 — 업무일지(모듈 06). 인증 필수(/api/worklogs).
 * 원본 /pdf(JasperReports) 엔드포인트는 ERP 의존성 미보유로 제외(보고서는 후속 트랙 — esn_port.md 참조).
 */
@RestController
@RequestMapping("/api/worklogs")
@RequiredArgsConstructor
public class WorklogController {

    private final WorklogService worklogService;

    @GetMapping
    public ApiResponse<WorklogPage> list(
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate fromDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate toDate,
            @RequestParam(required = false) String writerId,
            @RequestParam(required = false) String progressCd,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size) {
        return ApiResponse.ok(worklogService.list(fromDate, toDate, writerId, progressCd, page, size));
    }

    @GetMapping("/dashboard/progress")
    public ApiResponse<List<ProgressSummaryDto>> progressSummary(
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate fromDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate toDate) {
        return ApiResponse.ok(worklogService.progressSummary(fromDate, toDate));
    }

    @GetMapping("/calendar")
    public ApiResponse<List<WorklogCalendarDto>> calendar(
            @RequestParam(required = false) String yearMonth,
            @RequestParam(required = false) String writerId) {
        return ApiResponse.ok(worklogService.calendar(yearMonth, writerId));
    }

    @GetMapping("/search")
    public ApiResponse<List<WorklogListDto>> search(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String writerId) {
        return ApiResponse.ok(worklogService.search(keyword, writerId));
    }

    @PostMapping
    public ApiResponse<WorklogDetailDto> create(@Valid @RequestBody WorklogSaveDto dto) {
        return ApiResponse.ok(worklogService.create(dto));
    }

    @GetMapping("/{id}")
    public ApiResponse<WorklogDetailDto> detail(@PathVariable("id") Long id) {
        return ApiResponse.ok(worklogService.detail(id));
    }

    @PutMapping("/{id}")
    public ApiResponse<WorklogDetailDto> update(@PathVariable("id") Long id, @Valid @RequestBody WorklogSaveDto dto) {
        return ApiResponse.ok(worklogService.update(id, dto));
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable("id") Long id) {
        worklogService.delete(id);
        return ApiResponse.ok(null);
    }

    @PostMapping("/{id}/comments")
    public ApiResponse<WorklogCommentDto> addComment(@PathVariable("id") Long id,
                                                     @Valid @RequestBody CommentRequest req) {
        return ApiResponse.ok(worklogService.addComment(id, req));
    }

    @PostMapping("/comments/{cmtId}/confirm")
    public ApiResponse<Void> confirmComment(@PathVariable("cmtId") Long cmtId) {
        worklogService.confirmComment(cmtId);
        return ApiResponse.ok(null);
    }

    @GetMapping("/comments/unconfirmed")
    public ApiResponse<List<UnconfirmedCommentDto>> unconfirmedComments() {
        return ApiResponse.ok(worklogService.unconfirmedByMe());
    }
}
