using AutoMapper.QueryableExtensions;
using FastEndpoints;
using Knots.DTO.User;
using Microsoft.EntityFrameworkCore;

namespace Knots.Endpoints.User;

public class GetAllUsersEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : EndpointWithoutRequest<List<GetUserDetailsDto>>
{
    public override void Configure()
    { 
        Get ("/users");
        AllowAnonymous();
    }

    public override async Task HandleAsync(CancellationToken ct)
    {
        var users = await db.Users
            .ProjectTo<GetUserDetailsDto>(mapper.ConfigurationProvider)
            .ToListAsync(ct);
        
        await Send.OkAsync(users, ct);
    }
}