using Health.Application.Calendars;

namespace Health.Infrastructure.Calendars;

public sealed class EfCalendarRepository(AppDbContext db) : ICalendarRepository
{
    private readonly AppDbContext _db = db;

    public async Task<CalendarDataSnapshot> GetSnapshotAsync(Guid userId, DateOnly start, DateOnly end, CancellationToken ct)
    {
        var startUtc = start.ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc);
        var endUtc = end.ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc);

        var medications = await _db.Medications
            .Where(m => m.UserId == userId && m.IsActive)
            .ToListAsync(ct);
        var exercisePlans = await _db.ExercisePlans
            .Include(p => p.Items)
            .Where(p => p.UserId == userId && p.StartDate < end && p.EndDate >= start)
            .ToListAsync(ct);
        var followUps = await _db.FollowUps
            .Where(f => f.UserId == userId && f.ScheduledAt >= startUtc && f.ScheduledAt < endUtc)
            .ToListAsync(ct);

        return new CalendarDataSnapshot(medications, exercisePlans, followUps);
    }
}
