package com.yimeng.okr.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yimeng.okr.dto.NotificationDto;
import com.yimeng.okr.entity.Notification;
import com.yimeng.okr.mapper.NotificationMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import com.yimeng.okr.exception.BusinessException;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

@Service
@RequiredArgsConstructor
public class NotificationService {

    private final NotificationMapper notificationMapper;

    public long getUnreadCount(Long userId) {
        return notificationMapper.selectCount(
                new LambdaQueryWrapper<Notification>()
                        .eq(Notification::getUserId, userId)
                        .eq(Notification::getIsRead, 0));
    }

    public IPage<NotificationDto> getPage(Long userId, int page, int size, String type) {
        LambdaQueryWrapper<Notification> qw = new LambdaQueryWrapper<Notification>()
                .eq(Notification::getUserId, userId)
                .orderByDesc(Notification::getCreatedAt);
        if (type != null && !type.isEmpty()) {
            qw.eq(Notification::getType, type);
        }
        IPage<Notification> result = notificationMapper.selectPage(new Page<>(page, size), qw);
        return result.convert(this::toDto);
    }

    public void markRead(Long id, Long userId) {
        Notification n = notificationMapper.selectById(id);
        if (n == null) {
            throw new BusinessException(404, "通知不存在");
        }
        if (!n.getUserId().equals(userId)) {
            throw new BusinessException(403, "无权操作此通知");
        }
        n.setIsRead(1);
        n.setReadAt(LocalDateTime.now());
        notificationMapper.updateById(n);
    }

    public void markAllRead(Long userId) {
        notificationMapper.update(null,
                new LambdaUpdateWrapper<Notification>()
                        .eq(Notification::getUserId, userId)
                        .eq(Notification::getIsRead, 0)
                        .set(Notification::getIsRead, 1)
                        .set(Notification::getReadAt, LocalDateTime.now()));
    }

    private NotificationDto toDto(Notification n) {
        NotificationDto dto = new NotificationDto();
        dto.setId(n.getId());
        dto.setType(n.getType());
        dto.setTitle(n.getTitle());
        dto.setContent(n.getContent());
        dto.setRefType(n.getRefType());
        dto.setRefId(n.getRefId());
        dto.setIsRead(n.getIsRead());
        dto.setCreatedAt(n.getCreatedAt());
        long seconds = n.getCreatedAt() != null
                ? ChronoUnit.SECONDS.between(n.getCreatedAt(), LocalDateTime.now()) : 0;
        if (seconds < 60) dto.setCreatedAtFriendly("刚刚");
        else if (seconds < 3600) dto.setCreatedAtFriendly((seconds / 60) + "分钟前");
        else if (seconds < 86400) dto.setCreatedAtFriendly((seconds / 3600) + "小时前");
        else dto.setCreatedAtFriendly((seconds / 86400) + "天前");
        return dto;
    }
}
