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

namespace Knots.Endpoints.User;

public class GetUserEndpoint(KnotsDbContext knotsDbContext) : Endpoint <GetUserDto, GetUserDetailsDto>
{
    public override void Configure()
    { 
        Get ("/users/{@Id}", x => new { x.Username });
        AllowAnonymous();
    }

    public override async Task HandleAsync(GetUserDto req, CancellationToken ct)
    {
        Models.User? databaseLogin = await knotsDbContext.Users.SingleOrDefaultAsync(x => x.Username == req.Username, cancellationToken: ct);     
        
        if (databaseLogin == null)
        {
            await Send.NotFoundAsync(ct);
            return;
        }
        
        GetUserDetailsDto dto = new()
        {
            Username = databaseLogin.Username,
            Description = databaseLogin.Description,
            Password = databaseLogin.Password,
            Email = databaseLogin.Email,
            Tel =  databaseLogin.Tel,
            ProfilePicture =  databaseLogin.ProfilePicture
        };
        
        await Send.OkAsync(dto, ct);
    }
}