package com.zioinfo.hrm.uiws.message.controller;

import com.zioinfo.hrm.common.ApiResponse;
import com.zioinfo.hrm.uiws.message.dto.MessageDtos.*;
import com.zioinfo.hrm.uiws.message.service.MessageService;
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;

/**
 * UIWS 이식 — 쪽지(모듈 03). 인증 필수(/api/messages). 원본 prefix·엔드포인트 보존.
 * Spring Data Pageable 의존 회피: page/size 단순 파라미터로 이식(ERP는 spring-data-web 미사용).
 */
@Tag(name = "UIWS-쪽지", description = "UIWS 이식: 쪽지 송수신/개봉현황")
@RestController
@RequestMapping("/api/messages")
@RequiredArgsConstructor
public class MessageController {

    private final MessageService messageService;

    @Operation(summary = "쪽지 전송/답장")
    @PostMapping
    public ApiResponse<MessageSendResponse> send(@Valid @RequestBody MessageSendDto dto) {
        return ApiResponse.ok(messageService.send(dto));
    }

    @Operation(summary = "보낸쪽지함(기본 최근 1주일)")
    @GetMapping("/sent")
    public ApiResponse<MessageService.SentPage> sent(
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate fromDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate toDate,
            @RequestParam(required = false) String titleKeyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size) {
        return ApiResponse.ok(messageService.listSent(fromDate, toDate, titleKeyword, page, size));
    }

    @Operation(summary = "보낸쪽지 상세(수신자 개봉현황)")
    @GetMapping("/sent/{id}")
    public ApiResponse<SentMessageDetailDto> sentDetail(@PathVariable("id") Long id) {
        return ApiResponse.ok(messageService.sentDetail(id));
    }

    @Operation(summary = "보낸쪽지 다중삭제")
    @DeleteMapping("/sent")
    public ApiResponse<Void> deleteSent(@Valid @RequestBody MessageIdsRequest req) {
        messageService.deleteSent(req.ids());
        return ApiResponse.ok(null);
    }

    @Operation(summary = "미열람 받은쪽지 수(배지)")
    @GetMapping("/unread-count")
    public ApiResponse<Long> unreadCount() {
        return ApiResponse.ok(messageService.unreadCount());
    }

    @Operation(summary = "받은쪽지함(기본 최근 1주일)")
    @GetMapping("/received")
    public ApiResponse<MessageService.ReceivedPage> received(
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate fromDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate toDate,
            @RequestParam(required = false) String titleKeyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size) {
        return ApiResponse.ok(messageService.listReceived(fromDate, toDate, titleKeyword, page, size));
    }

    @Operation(summary = "받은쪽지 상세(조회 시 개봉처리)")
    @GetMapping("/received/{id}")
    public ApiResponse<ReceivedMessageDetailDto> receivedDetail(@PathVariable("id") Long id) {
        return ApiResponse.ok(messageService.receivedDetail(id));
    }

    @Operation(summary = "받은쪽지 다중삭제")
    @DeleteMapping("/received")
    public ApiResponse<Void> deleteReceived(@Valid @RequestBody MessageIdsRequest req) {
        messageService.deleteReceived(req.ids());
        return ApiResponse.ok(null);
    }
}
