using BookHive.DTO.Member;
using BookHive.Models;
using BookHive.Repositories;
using BookHive.Specifications.Members;
using FastEndpoints;

namespace BookHive.Endpoints.Members;

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

public class GetMemberEndpoint(MemberRepository memberRepository, AutoMapper.IMapper mapper) : Endpoint<MemberRequest, GetMemberDetailsDto>
{
    public override void Configure()
    {
        Get("/members/{@Id}/", x => new { x.Id });
        AllowAnonymous();
    }

    public override async Task HandleAsync(MemberRequest req, CancellationToken ct)
    {
        Member? member = await memberRepository.SingleOrDefaultAsync(new GetMemberByIdSpec(req.Id), ct);

        if (member is null)
        {
            await Send.NoContentAsync(ct);
            return;
        }

        await Send.OkAsync(mapper.Map<GetMemberDetailsDto>(member), ct);
    }
}