package repository

import (
	"hyapp-admin-server/internal/model"
	"time"
)

func (s *Store) ListNotifications() ([]model.Notification, error) {
	var items []model.Notification
	err := s.db.Order("created_at_ms DESC").Find(&items).Error
	return items, err
}

func (s *Store) MarkNotificationRead(id uint) (*model.Notification, error) {
	nowMS := time.Now().UTC().UnixMilli()
	if err := s.db.Model(&model.Notification{}).Where("id = ?", id).Update("read_at_ms", &nowMS).Error; err != nil {
		return nil, err
	}
	var item model.Notification
	if err := s.db.First(&item, id).Error; err != nil {
		return nil, err
	}
	return &item, nil
}

func (s *Store) MarkAllNotificationsRead() (int64, error) {
	nowMS := time.Now().UTC().UnixMilli()
	result := s.db.Model(&model.Notification{}).Where("read_at_ms IS NULL").Update("read_at_ms", &nowMS)
	return result.RowsAffected, result.Error
}

func (s *Store) DeleteNotification(id uint) error {
	return s.db.Delete(&model.Notification{}, id).Error
}
