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

import com.zioinfo.esn.common.ApiResponse;
import com.zioinfo.esn.uiws.message.dto.MessageDtos.*;
import com.zioinfo.esn.uiws.message.service.MessageService;
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 미사용).
 */
@RestController
@RequestMapping("/api/messages")
@RequiredArgsConstructor
public class MessageController {

    private final MessageService messageService;

    @PostMapping
    public ApiResponse<MessageSendResponse> send(@Valid @RequestBody MessageSendDto dto) {
        return ApiResponse.ok(messageService.send(dto));
    }

    @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));
    }

    @GetMapping("/sent/{id}")
    public ApiResponse<SentMessageDetailDto> sentDetail(@PathVariable("id") Long id) {
        return ApiResponse.ok(messageService.sentDetail(id));
    }

    @DeleteMapping("/sent")
    public ApiResponse<Void> deleteSent(@Valid @RequestBody MessageIdsRequest req) {
        messageService.deleteSent(req.ids());
        return ApiResponse.ok(null);
    }

    @GetMapping("/unread-count")
    public ApiResponse<Long> unreadCount() {
        return ApiResponse.ok(messageService.unreadCount());
    }

    @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));
    }

    @GetMapping("/received/{id}")
    public ApiResponse<ReceivedMessageDetailDto> receivedDetail(@PathVariable("id") Long id) {
        return ApiResponse.ok(messageService.receivedDetail(id));
    }

    @DeleteMapping("/received")
    public ApiResponse<Void> deleteReceived(@Valid @RequestBody MessageIdsRequest req) {
        messageService.deleteReceived(req.ids());
        return ApiResponse.ok(null);
    }
}
