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

namespace Knots.Endpoints.User;

public class DeleteUserEndpoint(KnotsDbContext knotsDbContext) : Endpoint <GetUserDto, GetUserDetailsDto>
{
    public override void Configure()
    { 
        Delete ("/users/{@Id}", x => new { x.Username });
    }

    public override async Task HandleAsync(GetUserDto req, CancellationToken ct)
    {
        Models.User? databaseUser = await knotsDbContext.Users.SingleOrDefaultAsync(x => x.Username == req.Username, cancellationToken: ct);     
        
        if (databaseUser == null)
        {
            await Send.NotFoundAsync(ct);
            return;
        }
        knotsDbContext.Users.Remove(databaseUser);
        await knotsDbContext.SaveChangesAsync(ct);
        
        await Send.NoContentAsync(ct);
    }
}