package com.lesingle.edu.controller.teacher;

import com.lesingle.edu.common.annotation.Log;
import com.lesingle.edu.common.enums.LogModule;
import com.lesingle.edu.common.enums.LogOperationType;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lesingle.edu.common.response.PageResult;
import com.lesingle.edu.common.response.Result;
import com.lesingle.edu.common.security.SecurityUtils;
import com.lesingle.edu.entity.Notification;
import com.lesingle.edu.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 = "教师端通知 API")
@RestController
@RequestMapping("/api/v1/teacher/notifications")
@RequiredArgsConstructor
public class TeacherNotificationController {

    private final NotificationService notificationService;

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

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

    @Operation(summary = "标记通知为已读")
    @Log(module = LogModule.NOTIFICATION, type = LogOperationType.OTHER, description = "标记通知为已读")
    @PostMapping("/{id}/read")
    public Result<Void> markAsRead(@PathVariable Long id) {
        notificationService.markAsRead(id);
        return Result.success();
    }

    @Operation(summary = "标记所有通知为已读")
    @Log(module = LogModule.NOTIFICATION, type = LogOperationType.OTHER, description = "标记所有通知为已读")
    @PostMapping("/read-all")
    public Result<Void> markAllAsRead() {
        Long userId = SecurityUtils.getCurrentUserId();
        notificationService.markAllAsRead(userId, "teacher");
        return Result.success();
    }

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

}
