using Application.Services.Database;

namespace Application.Endpoints.Pacients.Profile;

public class PacientProfileHandler
{
    private readonly IPacientRepository _pacientRepository;

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

    public async Task<BaseResponse> HandleGet(Guid id)
    {
        var pacient = await _pacientRepository.GetByIdAsync(id).ConfigureAwait(false);
        if (pacient != null)
        {
            return new BaseResponse
            {
                Success = true,
                Message = $"Retrieved pacient with id: {id}",
                Data = pacient
            };
        }

        return new BaseResponse
        {
            Success = false,
            Message = $"Pacient with id: {id} not found",
            Data = null
        };
    }

    public async Task<BaseResponse> HandleGetAll()
    {
        var pacients = await _pacientRepository.GetAllAsync().ConfigureAwait(false);
        if (pacients.Any())
        {
            return new BaseResponse
            {
                Success = true,
                Message = "Retrieved pacients",
                Data = pacients.ToList()
            };
        }

        return new BaseResponse
        {
            Success = false,
            Message = "Pacients not found",
            Data = null
        };
    }

    public async Task<BaseResponse> HandleUpdate(Guid id, PacientProfileDTO updateDto)
    {
        var validation = new PacientProfileValidation(_pacientRepository);
        var validationResult = await validation.ValidateAsync(updateDto);

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

        var pacientToUpdate = await _pacientRepository.GetByIdAsync(id);

        if (pacientToUpdate == null)
        {
            return new BaseResponse
            {
                Success = false,
                Message = "Pacient not found for given Id",
                Data = null
            };
        }

        pacientToUpdate.Email = updateDto.Email;
        pacientToUpdate.Password = updateDto.Password;
        pacientToUpdate.Name = updateDto.Name;

        await _pacientRepository.UpdateAsync(pacientToUpdate);

        return new BaseResponse
        {
            Success = true,
            Message = "Pacient updated successfully",
            Data = pacientToUpdate
        };
    }

    public async Task<BaseResponse> HandleDelete(Guid id)
    {
        var pacientToDelete = await _pacientRepository.GetByIdAsync(id);
        if (pacientToDelete == null)
        {
            return new BaseResponse
            {
                Success = false,
                Message = $"Pacient with id: {id} does not exist",
                Data = null
            };
        }

        await _pacientRepository.DeleteAsync(pacientToDelete);

        return new BaseResponse
        {
            Success = true,
            Message = $"Pacient with id: {id} was succesfully deleted",
            Data = pacientToDelete
        };
    }
}
