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 = "Teacher - Notification", description = "Notification APIs for Teacher")
@RestController
@RequestMapping("/api/teacher/notifications")
@RequiredArgsConstructor
public class TeacherNotificationController {

    private final NotificationService notificationService;

    @Operation(summary = "Get notification by ID")
    @GetMapping("/{id}")
    public Result<Notification> getNotification(@PathVariable Long id) {
        return Result.success(notificationService.getNotificationById(id));
    }

    @Operation(summary = "Get my notifications")
    @GetMapping
    public Result<PageResult<Notification>> getMyNotifications(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) 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 = "Mark notification as read")
    @PostMapping("/{id}/read")
    public Result<Void> markAsRead(@PathVariable Long id) {
        notificationService.markAsRead(id);
        return Result.success();
    }

    @Operation(summary = "Mark all notifications as read")
    @PostMapping("/read-all")
    public Result<Void> markAllAsRead() {
        Long userId = SecurityUtils.getCurrentUserId();
        notificationService.markAllAsRead(userId, "teacher");
        return Result.success();
    }

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

}
