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

namespace Knots.Endpoints.Group;

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

    public override async Task HandleAsync(UpdateGroupProfilePictureDto req, CancellationToken ct)
    {
        Models.Group? databaseGroup = await knotsDbContext.Groups.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);

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