package de.oaa.xxx.social;

import de.oaa.xxx.social.repository.MessageRepository;
import de.oaa.xxx.user.UserRepository;
import de.oaa.xxx.user.UserService;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.time.LocalDateTime;
import java.util.*;

@RestController
@RequestMapping("/notifications")
public class NotificationController {

    private final MessageRepository messageRepository;
    private final UserRepository    userRepository;
    private final UserService       userService;

    public NotificationController(MessageRepository messageRepository,
                                   UserRepository userRepository,
                                   UserService userService) {
        this.messageRepository = messageRepository;
        this.userRepository    = userRepository;
        this.userService       = userService;
    }

    @GetMapping
    public ResponseEntity<List<Map<String, Object>>> getNotifications(Principal principal) {
        UUID myId = userService.requireUser(principal).getUserId();

        List<Map<String, Object>> result = messageRepository
                .findNotificationsForUser(myId, PageRequest.of(0, 10))
                .stream()
                .map(m -> {
                    Map<String, Object> n = new LinkedHashMap<>();
                    n.put("id",        m.getMessageId().toString());
                    n.put("text",      m.getText());
                    n.put("sentAt",    m.getSentAt().toString());
                    n.put("read",      m.getReadAt() != null);
                    n.put("targetUrl", m.getTargetUrl() != null ? m.getTargetUrl() : "");
                    userRepository.findById(m.getSenderId()).ifPresent(sender -> {
                        n.put("senderName",    sender.getName());
                        n.put("senderAvatar",  sender.getProfilePicture() != null ? sender.getProfilePicture() : "");
                    });
                    return n;
                })
                .toList();
        return ResponseEntity.ok(result);
    }

    @GetMapping("/unread/count")
    public ResponseEntity<Long> getUnreadCount(Principal principal) {
        UUID myId = userService.requireUser(principal).getUserId();
        return ResponseEntity.ok(
                messageRepository.countByReceiverIdAndSystemMessageAndReadAtIsNull(myId, true));
    }

    @Transactional
    @PostMapping("/{id}/read")
    public ResponseEntity<Void> markOneRead(@PathVariable UUID id, Principal principal) {
        UUID myId = userService.requireUser(principal).getUserId();
        messageRepository.markNotificationAsRead(id, myId, LocalDateTime.now());
        return ResponseEntity.noContent().build();
    }

    @Transactional
    @PostMapping("/read-all")
    public ResponseEntity<Void> markAllRead(Principal principal) {
        UUID myId = userService.requireUser(principal).getUserId();
        messageRepository.markAllNotificationsAsRead(myId, LocalDateTime.now());
        return ResponseEntity.noContent().build();
    }
}
