package cn.meowrain.aioj.backend.blogservice.controller;

import cn.meowrain.aioj.backend.framework.core.web.Result;
import cn.meowrain.aioj.backend.framework.core.web.Results;
import cn.meowrain.aioj.backend.blogservice.dto.req.NotificationQueryRequestDTO;
import cn.meowrain.aioj.backend.blogservice.dto.resp.NotificationResponseDTO;
import cn.meowrain.aioj.backend.blogservice.service.NotificationService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 通知管理控制器
 *
 * @author AIOJ
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/notification")
@Tag(name = "通知管理", description = "通知查询、标记已读、清空通知等接口")
public class NotificationController {

    private final NotificationService notificationService;

    @PostMapping("/list")
    @Operation(summary = "分页查询通知列表", description = "支持多条件查询")
    public Result<IPage<NotificationResponseDTO>> getNotificationList(
            @Parameter(description = "查询条件")
            @RequestBody NotificationQueryRequestDTO request) {
        IPage<NotificationResponseDTO> page = notificationService.getNotificationList(request);
        return Results.success(page);
    }

    @PutMapping("/read/{notificationId}")
    @Operation(summary = "标记通知为已读", description = "将指定通知标记为已读状态")
    public Result<Void> markAsRead(
            @Parameter(description = "通知ID", required = true)
            @PathVariable("notificationId") Long notificationId) {
        notificationService.markAsRead(notificationId);
        return Results.success();
    }

    @PutMapping("/read/batch")
    @Operation(summary = "批量标记通知为已读", description = "将多个通知批量标记为已读状态")
    public Result<Void> batchMarkAsRead(
            @Parameter(description = "通知ID列表", required = true)
            @RequestBody java.util.List<Long> notificationIds) {
        notificationService.batchMarkAsRead(notificationIds);
        return Results.success();
    }

    @PutMapping("/read/all")
    @Operation(summary = "标记所有通知为已读", description = "将当前用户的所有未读通知标记为已读")
    public Result<Void> markAllAsRead() {
        notificationService.markAllAsRead();
        return Results.success();
    }

    @DeleteMapping("/clear/all")
    @Operation(summary = "清空所有通知", description = "清空当前用户的所有通知")
    public Result<Void> clearAllNotifications() {
        notificationService.clearAllNotifications();
        return Results.success();
    }

    @GetMapping("/unread/count")
    @Operation(summary = "查询未读通知数量", description = "获取当前用户的未读通知数量")
    public Result<Long> getUnreadCount() {
        Long count = notificationService.getUnreadCount();
        return Results.success(count);
    }
}
