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

import com.zioinfo.mes.common.ApiResponse;
import com.zioinfo.mes.uiws.worklog.dto.WorklogDtos.*;
import com.zioinfo.mes.uiws.worklog.service.WorklogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
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 의존성 미보유로 제외(보고서는 후속 트랙 — erp_feature_port.md 참조).
 */
@Tag(name = "UIWS-업무일지", description = "UIWS 이식: 업무일지/시간대별 상세/댓글")
@RestController
@RequestMapping("/api/worklogs")
@RequiredArgsConstructor
public class WorklogController {

    private final WorklogService worklogService;

    @Operation(summary = "업무일지 목록(리스트형, 기본 최근 1개월)")
    @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));
    }

    @Operation(summary = "관리자 대시보드 — 진행상태별 건수")
    @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));
    }

    @Operation(summary = "업무일지 목록(달력형)")
    @GetMapping("/calendar")
    public ApiResponse<List<WorklogCalendarDto>> calendar(
            @RequestParam(required = false) String yearMonth,
            @RequestParam(required = false) String writerId) {
        return ApiResponse.ok(worklogService.calendar(yearMonth, writerId));
    }

    @Operation(summary = "업무일지 조회 팝업")
    @GetMapping("/search")
    public ApiResponse<List<WorklogListDto>> search(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String writerId) {
        return ApiResponse.ok(worklogService.search(keyword, writerId));
    }

    @Operation(summary = "업무일지 등록")
    @PostMapping
    public ApiResponse<WorklogDetailDto> create(@Valid @RequestBody WorklogSaveDto dto) {
        return ApiResponse.ok(worklogService.create(dto));
    }

    @Operation(summary = "업무일지 상세")
    @GetMapping("/{id}")
    public ApiResponse<WorklogDetailDto> detail(@PathVariable("id") Long id) {
        return ApiResponse.ok(worklogService.detail(id));
    }

    @Operation(summary = "업무일지 수정(시간대별 일괄, _state)")
    @PutMapping("/{id}")
    public ApiResponse<WorklogDetailDto> update(@PathVariable("id") Long id, @Valid @RequestBody WorklogSaveDto dto) {
        return ApiResponse.ok(worklogService.update(id, dto));
    }

    @Operation(summary = "업무일지 삭제")
    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable("id") Long id) {
        worklogService.delete(id);
        return ApiResponse.ok(null);
    }

    @Operation(summary = "댓글 등록(+알림)")
    @PostMapping("/{id}/comments")
    public ApiResponse<WorklogCommentDto> addComment(@PathVariable("id") Long id,
                                                     @Valid @RequestBody CommentRequest req) {
        return ApiResponse.ok(worklogService.addComment(id, req));
    }

    @Operation(summary = "댓글 확인 처리(일지 작성자 전용)")
    @PostMapping("/comments/{cmtId}/confirm")
    public ApiResponse<Void> confirmComment(@PathVariable("cmtId") Long cmtId) {
        worklogService.confirmComment(cmtId);
        return ApiResponse.ok(null);
    }

    @Operation(summary = "내가 단 댓글 중 작성자 미확인 목록")
    @GetMapping("/comments/unconfirmed")
    public ApiResponse<List<UnconfirmedCommentDto>> unconfirmedComments() {
        return ApiResponse.ok(worklogService.unconfirmedByMe());
    }
}
