using Microsoft.EntityFrameworkCore; using Nuuru.Server.Data; using Nuuru.Server.Models; namespace Nuuru.Server.Services { public interface IWatchService { Task ToggleWatchAsync(Guid userId, WatchTargetType targetType, int targetId); Task EnsureWatchingAsync(Guid userId, WatchTargetType targetType, int targetId); Task IsWatchingAsync(Guid userId, WatchTargetType targetType, int targetId); Task> GetWatcherUserIdsAsync(WatchTargetType targetType, int targetId); Task GetWatchCountAsync(WatchTargetType targetType, int targetId); } public class WatchService : IWatchService { private readonly ApplicationDbContext _context; public WatchService(ApplicationDbContext context) { _context = context; } public async Task ToggleWatchAsync(Guid userId, WatchTargetType targetType, int targetId) { var existing = await _context.Watches .FirstOrDefaultAsync(w => w.UserId == userId && w.TargetType == targetType && w.TargetId == targetId); if (existing != null) { _context.Watches.Remove(existing); await _context.SaveChangesAsync(); return false; } _context.Watches.Add(new Watch { UserId = userId, TargetType = targetType, TargetId = targetId, CreatedAt = DateTime.UtcNow }); await _context.SaveChangesAsync(); return true; } public async Task EnsureWatchingAsync(Guid userId, WatchTargetType targetType, int targetId) { var alreadyWatching = await _context.Watches .AnyAsync(w => w.UserId == userId && w.TargetType == targetType && w.TargetId == targetId); if (alreadyWatching) return; _context.Watches.Add(new Watch { UserId = userId, TargetType = targetType, TargetId = targetId, CreatedAt = DateTime.UtcNow }); await _context.SaveChangesAsync(); } public async Task IsWatchingAsync(Guid userId, WatchTargetType targetType, int targetId) { return await _context.Watches .AnyAsync(w => w.UserId == userId && w.TargetType == targetType && w.TargetId == targetId); } public async Task> GetWatcherUserIdsAsync(WatchTargetType targetType, int targetId) { return await _context.Watches .Where(w => w.TargetType == targetType && w.TargetId == targetId) .Select(w => w.UserId) .ToListAsync(); } public async Task GetWatchCountAsync(WatchTargetType targetType, int targetId) { return await _context.Watches .CountAsync(w => w.TargetType == targetType && w.TargetId == targetId); } } }