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

namespace Knots.Endpoints.User;

public class PatchUserProfilePictureEndpoint(KnotsDbContext knotsDbContext) : Endpoint<UpdateUserProfilePictureDto>
{
    public override void Configure()
    {
        Patch("/users/{@Id}/profilePicture/", x => new {x.Id});
        AllowAnonymous();
    }

    public override async Task HandleAsync(UpdateUserProfilePictureDto req, CancellationToken ct)
    {
        Models.User? databaseUser = await knotsDbContext.Users.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);

        if (databaseUser is null)
        {
            await Send.NotFoundAsync(ct);
            return;
        }
        
        databaseUser.ProfilePicture = req.ProfilePicture;
        await knotsDbContext.SaveChangesAsync(ct);
        await Send.NoContentAsync(ct);
    }
}