package com.lesingle.edu.controller.parent;

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.dto.response.NotificationForParentResponse;
import com.lesingle.edu.entity.Notification;
import com.lesingle.edu.common.annotation.Log;
import com.lesingle.edu.common.enums.LogModule;
import com.lesingle.edu.common.enums.LogOperationType;
import com.lesingle.edu.common.annotation.RequireRole;
import com.lesingle.edu.common.enums.UserRole;
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.*;

import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "Parent - Notification", description = "Notification APIs for Parent")
@RestController
@RequestMapping("/api/v1/parent/notifications")
@RequiredArgsConstructor
@RequireRole(UserRole.PARENT)
public class ParentNotificationController {

    private final NotificationService notificationService;

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

    @Operation(summary = "Get my notifications")
    @GetMapping
    public Result<PageResult<NotificationForParentResponse>> getMyNotifications(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer isRead,
            @RequestParam(required = false) String notificationType) {
        Long userId = SecurityUtils.getCurrentUserId();
        Page<Notification> page = notificationService.getMyNotifications(userId, "parent", pageNum, pageSize, isRead, notificationType);
        List<NotificationForParentResponse> voList = page.getRecords().stream()
                .map(this::toParentResponse)
                .collect(Collectors.toList());
        return Result.success(PageResult.of(voList, page.getTotal(), page.getCurrent(), page.getSize()));
    }

    private NotificationForParentResponse toParentResponse(Notification n) {
        return NotificationForParentResponse.builder()
                .id(n.getId())
                .title(n.getTitle())
                .content(n.getContent())
                .notificationType(n.getType())
                .isRead(n.getIsRead() != null && n.getIsRead() == 1)
                .readAt(n.getReadAt())
                .createdAt(n.getCreatedAt())
                .build();
    }

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

    @Operation(summary = "Mark all notifications as read")
    @Log(module = LogModule.NOTIFICATION, type = LogOperationType.UPDATE, description = "标记所有通知为已读")
    @PostMapping("/read-all")
    public Result<Void> markAllAsRead() {
        Long userId = SecurityUtils.getCurrentUserId();
        notificationService.markAllAsRead(userId, "parent");
        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, "parent"));
    }

}
