// SPDX-License-Identifier: Apache-2.0
// Copyright 2020 - 2025 Pionix GmbH and Contributors to EVerest

#pragma once

#include "API.hpp"
#include <optional>
#include <string>

namespace everest::lib::API::V1_0::types::error_history {

std::string serialize(State val) noexcept;
std::string serialize(SeverityFilter val) noexcept;
std::string serialize(Severity val) noexcept;
std::string serialize(ImplementationIdentifier const& val) noexcept;
std::string serialize(TimeperiodFilter const& val) noexcept;
std::string serialize(FilterArguments const& val) noexcept;
std::string serialize(ErrorObject const& val) noexcept;
std::string serialize(ErrorList const& val) noexcept;

std::ostream& operator<<(std::ostream& os, State const& val);
std::ostream& operator<<(std::ostream& os, SeverityFilter const& val);
std::ostream& operator<<(std::ostream& os, Severity const& val);
std::ostream& operator<<(std::ostream& os, ImplementationIdentifier const& val);
std::ostream& operator<<(std::ostream& os, TimeperiodFilter const& val);
std::ostream& operator<<(std::ostream& os, FilterArguments const& val);
std::ostream& operator<<(std::ostream& os, ErrorObject const& val);
std::ostream& operator<<(std::ostream& os, ErrorList const& val);

template <class T> T deserialize(std::string const& val);
template <class T> std::optional<T> try_deserialize(std::string const& val) {
    try {
        return deserialize<T>(val);
    } catch (...) {
        return std::nullopt;
    }
}
template <class T> bool adl_deserialize(std::string const& json_data, T& obj) {
    auto opt = try_deserialize<T>(json_data);
    if (opt) {
        obj = opt.value();
        return true;
    }
    return false;
}

} // namespace everest::lib::API::V1_0::types::error_history
