using Microsoft.AspNetCore.Mvc;
using PlanTempus.Core.CommandQueries;

namespace PlanTempus.Components.Accounts.Create
{
    [ApiController]
    [Route("api/accounts")]
    public class CreateAccountController(CreateAccountHandler handler, CreateAccountValidator validator) : ControllerBase
    {
        [HttpPost]
        public async Task<ActionResult<CommandResponse>> Create([FromBody] CreateAccountCommand command)
        {
            try
            {
                var validationResult = await validator.ValidateAsync(command);
                if (!validationResult.IsValid)
                {
                    return BadRequest(validationResult.Errors);
                }

                var result = await handler.Handle(command);

                return Accepted($"/api/requests/{result.RequestId}", result);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }
    }
}
