// This file was generated automatically by Qleany's generator, edit at your own risk! 
// If you do, be careful to not overwrite it when you run the generator again.
#include "{{ query.snake_name }}_query_handler.h"
#include <qleany/tools/automapper/automapper.h>
{% if query.dto_in_is_enabled %}#include "{{ feature_snake_name }}/{{ query.dto_in_snake_name }}.h"{% endif %}
{% if validator_enabled %}#include "{{ feature_snake_name }}/validators/{{ query.snake_name }}_query_validator.h"{% endif %}
#include <QDebug>

using namespace Qleany;
using namespace {{ application_cpp_domain_name }}::Contracts::DTO::{{ feature_pascal_name }};
{% if query.repositories %}using namespace {{ application_cpp_domain_name }}::Contracts::Repository;{% endif %}
{% if validator_enabled %}using namespace {{ application_cpp_domain_name }}::Contracts::CQRS::{{ feature_pascal_name }}::Validators;{% endif %}
using namespace {{ application_cpp_domain_name }}::Application::Features::{{ feature_pascal_name }}::Queries;

{{ query.pascal_name }}QueryHandler::{{ query.pascal_name }}QueryHandler({% for repository in query.repositories %}
Interface{{ repository.pascal_name }}Repository *{{ repository.camel_name }}Repository {% if not loop.last %},{% endif %}{% endfor %})
    {% for repository in query.repositories %}{% if loop.first %}: {% endif %}m_{{ repository.camel_name }}Repository({{ repository.camel_name }}Repository){% if not loop.last %},{% endif %}{% endfor %}
{
    if (!s_mappingRegistered)
    {
        registerMappings();
        s_mappingRegistered = true;
    }
}

Result<{{ query.dto_out_pascal_name }}>{{ query.pascal_name }}QueryHandler::handle(QPromise<Result<void> >   & progressPromise,
                                                      const {{ query.pascal_name }}Query& request)
{
    Result<{{ query.dto_out_pascal_name }}> result;

    try
    {
        result = handleImpl(progressPromise, request);
    }
    catch (const std::exception& ex)
    {
        result = Result<{{ query.dto_out_pascal_name }}>(QLN_ERROR_2(Q_FUNC_INFO, Error::Critical, "Unknown error", ex.what()));
        qDebug() << "Error handling {{ query.pascal_name }}Query:" << ex.what();
    }
    progressPromise.addResult(Result<void>(result.error()));
    return result;
}

Result<{{ query.dto_out_pascal_name }}>{{ query.pascal_name }}QueryHandler::handleImpl(QPromise<Result<void> >   & progressPromise, const {{ query.pascal_name }}Query& request)
{
    qDebug() << "{{ query.pascal_name }}QueryHandler::handleImpl called";

    // {{ application_cpp_domain_name }}::Entities::{{ feature_pascal_name }} {{ feature_snake_name }};
{% if validator_enabled %}
    // Validate the query using the validator
    auto validator               = {{ query.pascal_name }}QueryValidator({% for repository in query.repositories %}m_{{ repository.camel_name }}Repository{% if not loop.last %},{% endif %}{% endfor %});
    Result<void> validatorResult = validator.validate(request.req);

    // If the validation failed, return the error
    QLN_RETURN_IF_ERROR({{ query.dto_out_pascal_name }}, validatorResult)
{% endif %}

    // implement logic here 
    // {{ feature_snake_name }} = Qleany::Tools::AutoMapper::AutoMapper::map<{{ query.dto_out_pascal_name }}, {{ application_cpp_domain_name }}::Entities::{{ feature_pascal_name }}>(request.req);

   


    // play here with the repositories
    Q_UNIMPLEMENTED();

  
    //auto {{ feature_camel_name }}DTO = Qleany::Tools::AutoMapper::AutoMapper::map<{{ application_cpp_domain_name }}::Entities::{{ feature_pascal_name }}, {{ query.dto_out_pascal_name }}>({{ feature_snake_name }}Result.value());
    // dummy to compile
    {{ query.dto_out_pascal_name }} {{ query.dto_out_camel_name }};

    // Q_EMIT signal
    Q_EMIT {{ query.camel_name }}Changed({{ query.dto_out_camel_name }});

    // Return 
    return Result<{{ query.dto_out_pascal_name }}>({{ query.dto_out_camel_name }});

}

bool {{ query.pascal_name }}QueryHandler::s_mappingRegistered = false;

void {{ query.pascal_name }}QueryHandler::registerMappings() 
{

}
