using API.DTO.Effect.Request;
using API.DTO.Effect.Response;
using FastEndpoints;
using Microsoft.AspNetCore.Server.Kestrel;
using Microsoft.EntityFrameworkCore;

namespace API.Endpoints.Effect;

public class UpdateEffectEndpoint(AppDbContext appDbContext) : Endpoint<UpdateEffectDto, GetEffectDto>
{
    public override void Configure()
    {
        Put("/effect/{@id}", x => new { x.Id });
        AllowAnonymous();
    }

    public override async Task HandleAsync(UpdateEffectDto req, CancellationToken ct)
    {
        Models.Effect? effectToEdit = await appDbContext
            .Effects 
            .SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);

        if (effectToEdit == null)
        {
            Console.WriteLine("Aucun effet avec l'id {req.Id} trouvé.");
            await Send.NotFoundAsync(ct);
            return;

        }
        effectToEdit.Label = req.Label;
        await appDbContext.SaveChangesAsync(ct);

        GetEffectDto responseDto = new()
        {
            Id = req.Id,
            Label = req.Label,
        };
        await Send.OkAsync(responseDto, ct);

    }
    
}