using FastEndpoints;
using Microsoft.EntityFrameworkCore;

namespace API.Endpoints.Color;

public class DeleteColorRequest
{
    public int Id { get; set; }
}

public class DeleteColorEndpoint(AppDbContext appDbContext) : Endpoint<DeleteColorRequest>
{
    public override void Configure()
    {
        Delete("/colors/{@id}", x => new { x.Id });
        AllowAnonymous();
    }
    
    public override async Task HandleAsync(DeleteColorRequest req, CancellationToken ct)
    {
        Models.Color? colorToDelete = await appDbContext
            .Colors
            .SingleOrDefaultAsync(a => a.Id == req.Id, cancellationToken: ct);        

        if (colorToDelete == null)
        {
            Console.WriteLine($"Aucune couleur avec l'ID {req.Id} trouvé.");
            await Send.NotFoundAsync(ct);
            return;
        }

        appDbContext.Colors.Remove(colorToDelete);
        await appDbContext.SaveChangesAsync(ct);

        await Send.NoContentAsync(ct);
    }
}