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

namespace Knots.Endpoints.Role;

public class GetRoleEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint <GetRoleDto>
{
    public override void Configure()
    { 
        Get ("/roles/{@Id}", x => new { x.Libelle });
        AllowAnonymous();
    }

    public override async Task HandleAsync(GetRoleDto req, CancellationToken ct)
    {
        Models.Role? databaseRole = await db.Roles.SingleOrDefaultAsync(x => x.Libelle == req.Libelle, cancellationToken: ct);     
        
        if (databaseRole == null)
        {
            await Send.NotFoundAsync(ct);
            return;
        }
        
        var roleDto = mapper.Map<GetRoleDto>(databaseRole);
        await Send.OkAsync(roleDto, ct);
    }
}