using Health.Application.Reports;

namespace Health.Infrastructure.Reports;

public sealed class EfReportRepository(AppDbContext db) : IReportRepository
{
    private readonly AppDbContext _db = db;

    public async Task<IReadOnlyList<Report>> ListAsync(Guid userId, CancellationToken ct) =>
        await _db.Reports
            .Where(r => r.UserId == userId)
            .OrderByDescending(r => r.CreatedAt)
            .ToListAsync(ct);

    public Task<Report?> GetOwnedAsync(Guid userId, Guid reportId, CancellationToken ct) =>
        _db.Reports.FirstOrDefaultAsync(r => r.Id == reportId && r.UserId == userId, ct);

    public Task<Report?> GetByIdAsync(Guid reportId, CancellationToken ct) =>
        _db.Reports.FirstOrDefaultAsync(r => r.Id == reportId, ct);

    public async Task AddAsync(Report report, CancellationToken ct) =>
        await _db.Reports.AddAsync(report, ct);

    public void Delete(Report report) =>
        _db.Reports.Remove(report);

    public Task SaveChangesAsync(CancellationToken ct) =>
        _db.SaveChangesAsync(ct);
}
