namespace DocuMed.Repository.Repositories.Entities;

public class MedicalHistoryRepository : BaseRepository<MedicalHistory>,IMedicalHistoryRepository
{
    public MedicalHistoryRepository(ApplicationContext dbContext, ICurrentUserService currentUserService) : base(dbContext, currentUserService)
    {
    }

    public async Task<List<MedicalHistorySDto>> GetMedicalHistoriesAsync(int page = 0, CancellationToken cancellationToken = default)
    {
        if (!Guid.TryParse(CurrentUserService.UserId, out Guid userId))
            throw new AppException("توکن غیرمجاز", ApiResultStatusCode.UnAuthorized);
        var list = await TableNoTracking
            .Where(t => t.ApplicationUserId == userId)
            .OrderByDescending(t => t.CreatedAt)
            .Skip(page * 15)
            .Take(15)
            .Select(MedicalHistoryMapper.ProjectToSDto)
            .ToListAsync(cancellationToken);
        return list;
    }

    public async Task<MedicalHistoryLDto> GetMedicalHistoryAsync(Guid id, CancellationToken cancellationToken = default)
    {
        var dto = await TableNoTracking.Where(t => t.Id == id)
            .Select(MedicalHistoryMapper.ProjectToLDto)
            .FirstOrDefaultAsync(cancellationToken);
        if (dto == null)
            throw new AppException("شرح حال پیدا نشد", ApiResultStatusCode.NotFound);
        return dto;
    }
}