package com.reading.platform.controller.teacher;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reading.platform.common.response.PageResult;
import com.reading.platform.common.response.Result;
import com.reading.platform.common.security.SecurityUtils;
import com.reading.platform.entity.Notification;
import com.reading.platform.service.NotificationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "教师 - 通知", description = "通知接口（教师专用）")
@RestController
@RequestMapping("/api/v1/teacher/notifications")
@RequiredArgsConstructor
public class TeacherNotificationController {

    private final NotificationService notificationService;

    @Operation(summary = "根据ID获取通知")
    @GetMapping("/{id}")
    public Result<Notification> getNotification(@PathVariable String id) {
        return Result.success(notificationService.getNotificationById(id));
    }

    @Operation(summary = "获取我的通知")
    @GetMapping
    public Result<PageResult<Notification>> getMyNotifications(
            @RequestParam(value = "page", required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) Integer isRead) {
        String userId = SecurityUtils.getCurrentUserId();
        Page<Notification> page = notificationService.getMyNotifications(userId, "teacher", pageNum, pageSize, isRead);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "标记通知为已读")
    @PostMapping("/{id}/read")
    public Result<Void> markAsRead(@PathVariable String id) {
        notificationService.markAsRead(id);
        return Result.success();
    }

    @Operation(summary = "标记所有通知为已读")
    @PostMapping("/read-all")
    public Result<Void> markAllAsRead() {
        String userId = SecurityUtils.getCurrentUserId();
        notificationService.markAllAsRead(userId, "teacher");
        return Result.success();
    }

    @Operation(summary = "获取未读通知数量")
    @GetMapping("/unread-count")
    public Result<Long> getUnreadCount() {
        String userId = SecurityUtils.getCurrentUserId();
        return Result.success(notificationService.getUnreadCount(userId, "teacher"));
    }

}
