using Application.Endpoints.Pacients.Login;
using Application.Services.Database;
using Core.Entities;

namespace Application.Endpoints.Pacients.Registration;

public class PacientRegistrationHandler
{
    private readonly IPacientRepository _pacientRepository;

    public PacientRegistrationHandler(IPacientRepository pacientRepository)
    {
        _pacientRepository = pacientRepository;
    }

    public async Task<BaseResponse> Handle(PacientRegistrationDto registrationDTO)
    {
        var validation = new PacientRegistrationValidation(_pacientRepository);
        var validationResult = await validation.ValidateAsync(registrationDTO);

        if (!validationResult.IsValid)
        {
            var errorMessage = validationResult.Errors.Select(e => e.ErrorMessage).ToList();
            return new BaseResponse
            {
                Success = false,
                Message = string.Join(", ", errorMessage),
                Data = null
            };
        }

        var pacient = new Pacient
        {
            Email = registrationDTO.Email,
            Password = registrationDTO.Password,
            Name = registrationDTO.Name
        };

        await _pacientRepository.AddAsync(pacient);

        return new BaseResponse
        {
            Success = true,
            Message = "Pacient registered successfully",
            Data = pacient // Be careful with sending sensitive data like Passwords
        };
    }
}
