using API.DTO.Color.Response;
using FastEndpoints;
using Microsoft.AspNetCore.Authentication;
using Microsoft.EntityFrameworkCore;

namespace API.Endpoints.Color;

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

public class GetColorEndpoint(AppDbContext appDbContext) : Endpoint<GetColorRequest, GetColorDto>
{
    public override void Configure()
    {
        Get("/colors/{@id}", x => new { x.Id});
        AllowAnonymous();
    }

    public override async Task HandleAsync(GetColorRequest req, CancellationToken ct)
    {
        Models.Color? color = await appDbContext
            .Colors
            .SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);

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

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

    }
}