using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Knot.Contracts.Stories.Infrastructure.Persistence;
using MongoDB.Driver;
using DomainStory = Knot.Modules.Stories.Domain.Story;

namespace Knot.Modules.Stories.Infrastructure.Persistence.Mongo;

public class StoryCollection : Knot.Contracts.Stories.Infrastructure.Persistence.IStoryCollection
{
    private readonly IMongoCollection<DomainStory> _stories;

    public StoryCollection(IMongoDatabase database)
    {
        _stories = database.GetCollection<DomainStory>("stories");
    }

    public async Task<List<Knot.Contracts.Stories.Infrastructure.Persistence.StoryContract>> GetAllAsync(CancellationToken cancellationToken)
    {
        var stories = await _stories.Find(_ => true).ToListAsync(cancellationToken);
        return stories.Select(s => new Knot.Contracts.Stories.Infrastructure.Persistence.StoryContract
        {
            Id = s.Id,
            UserId = s.UserId,
            MediaUrl = s.MediaUrl,
            MediaType = s.Type.ToString(),
            CreatedAt = s.CreatedAt,
            ExpiresAt = null
        }).ToList();
    }
}