package com.huimv.employment.controller.mp;

import com.huimv.employment.common.web.R;
import com.huimv.employment.security.LoginUserHolder;
import com.huimv.employment.service.notification.NotificationService;
import com.huimv.employment.service.notification.dto.NotificationPageResponse;
import com.huimv.employment.service.notification.dto.NotificationReadResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 消息与待办中心：当前登录用户查询、标记已读。
 */
@RestController
@RequestMapping("/api/v1/mp/notifications")
@Tag(name = "消息与待办", description = "查询当前用户消息与待办，标记已读")
@SecurityRequirement(name = "Authorization")
public class NotificationController {

    private final NotificationService notificationService;

    public NotificationController(NotificationService notificationService) {
        this.notificationService = notificationService;
    }

    @GetMapping
    @Operation(summary = "我的消息与待办",
            description = "按当前 JWT 用户 user_id 分页查询。可选 notify_type / category / read_flag / handled_flag 过滤。"
                    + "返回 unread_count、pending_count 便于角标展示。")
    public R<NotificationPageResponse> listMine(
            @Parameter(name = "notify_type", in = ParameterIn.QUERY,
                    description = "pending / error / info / success")
            @RequestParam(value = "notify_type", required = false) String notifyType,
            @Parameter(name = "category", in = ParameterIn.QUERY,
                    description = "settlement / registration / verify / workflow")
            @RequestParam(value = "category", required = false) String category,
            @Parameter(name = "read_flag", in = ParameterIn.QUERY, description = "是否已读")
            @RequestParam(value = "read_flag", required = false) Boolean readFlag,
            @Parameter(name = "handled_flag", in = ParameterIn.QUERY, description = "待办是否已处理")
            @RequestParam(value = "handled_flag", required = false) Boolean handledFlag,
            @Parameter(name = "page", in = ParameterIn.QUERY, description = "页码，从 1 开始")
            @RequestParam(value = "page", required = false) Integer page,
            @Parameter(name = "size", in = ParameterIn.QUERY, description = "每页条数，默认 20，最大 100")
            @RequestParam(value = "size", required = false) Integer size) {
        Long userId = LoginUserHolder.require().getUserId();
        return R.ok(notificationService.listByUser(userId, notifyType, category, readFlag, handledFlag, page, size));
    }

    @PostMapping("/{id}/read")
    @Operation(summary = "标记消息已读",
            description = "将当前用户名下指定消息的 read_flag 置为 true，并写入 read_time；已读则幂等返回。"
                    + "同时返回最新 unread_count。")
    public R<NotificationReadResponse> markRead(
            @Parameter(name = "id", in = ParameterIn.PATH, description = "消息 ID", required = true)
            @PathVariable("id") Long id) {
        Long userId = LoginUserHolder.require().getUserId();
        return R.ok(notificationService.markRead(userId, id));
    }
}
