using Microsoft.EntityFrameworkCore;
using NexusReader.Application.Abstractions.Persistence;
using NexusReader.Data.Persistence;
using NexusReader.Domain.Entities;

namespace NexusReader.Infrastructure.Persistence;

/// <summary>
/// EF Core implementation of <see cref="IQuizResultRepository"/>.
/// </summary>
internal sealed class QuizResultRepository : IQuizResultRepository
{
    private readonly AppDbContext _context;

    public QuizResultRepository(AppDbContext context)
    {
        _context = context;
    }

    /// <inheritdoc />
    public async Task<NexusUser?> FindUserByIdAsync(string userId, CancellationToken cancellationToken = default)
    {
        return await _context.Users.FirstOrDefaultAsync(u => u.Id == userId, cancellationToken);
    }

    /// <inheritdoc />
    public void AddQuizResult(QuizResult quizResult)
    {
        _context.QuizResults.Add(quizResult);
    }

    /// <inheritdoc />
    public Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
    {
        return _context.SaveChangesAsync(cancellationToken);
    }
}
