using Application.Abstractions.Messaging;
using Web.Api.Extensions;
using Web.Api.Common;

namespace Web.Api.Endpoints.Auth;

internal sealed class Registration : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        // 회원가입 완료 (이메일 인증 후 확인)
        app.MapGet("api/auth/registration/{email}", async (
            string email,
            HttpContext httpContext,
            ISender sender,
            CancellationToken ct
        ) =>
        {
            var cookieName = "isVerified-Registration";
            var cookieValue = httpContext.Request.Cookies[cookieName] ?? string.Empty;

            var command = new Application.Features.Api.Auth.Registration.Command(email, cookieValue);
            var result = await sender.Send(command, ct);

            // 성공 시 쿠키 삭제
            if (result.IsSuccess)
            {
                httpContext.Response.Cookies.Delete(cookieName);
            }

            return result.Match(
                () => ApiResponse.Ok("회원가입이 완료되었습니다."),
                CustomResults.Problem
            );
        })
        .WithTags("Auth")
        .AllowAnonymous();
    }
}