using LearningMaterialSystem.Data;
using LearningMaterialSystem.Models;

namespace LearningMaterialSystem.Services;

public class NhatKyTruyCapService : INhatKyTruyCapService
{
    private readonly ApplicationDbContext _context;

    public NhatKyTruyCapService(ApplicationDbContext context)
    {
        _context = context;
    }

    public async Task GhiLogAsync(string hanhDong, int? hocLieuId, int? nguoiDungId, HttpContext? httpContext)
    {
        var entry = new NhatKyTruyCap
        {
            HanhDong = hanhDong,
            HocLieuId = hocLieuId,
            NguoiDungId = nguoiDungId,
            ThoiGian = DateTime.UtcNow,
            DiaChiIp = httpContext?.Connection?.RemoteIpAddress?.ToString()
        };
        _context.NhatKyTruyCaps.Add(entry);
        await _context.SaveChangesAsync();
    }
}
