// This file is part of GQCG-GQCP.
//
// Copyright (C) 2017-2020  the GQCG developers
//
// GQCG-GQCP is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GQCG-GQCP is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with GQCG-GQCP.  If not, see <http://www.gnu.org/licenses/>.

#include "QCModel/HF/StabilityMatrices/RHFStabilityMatrices.hpp"
#include "Utilities/complex.hpp"
#include "gqcpy/include/interfaces.hpp"

#include <pybind11/eigen.h>
#include <pybind11/pybind11.h>


namespace gqcpy {


// Provide some shortcuts for frequent namespaces.
namespace py = pybind11;
using namespace GQCP;


/**
 *  Add Python bindings for some APIs related to `RHF Stability matrices`.
 * 
 *  @tparam Class               The type of the Pybind11 `class_` (generated by the compiler).
 *  
 *  @param py_class             The Pybind11 `class_` that should obtain APIs related to `RHFStabilityMatrices`.
 */
template <typename Class>
void bindQCModelRHFStabilityInterface(Class& py_class) {

    // The C++ type corresponding to the Python class.
    using Type = typename Class::type;
    using Scalar = typename Type::Scalar;

    py_class
        .def(
            "singletA",
            &Type::singletA,
            "Return the singlet A stability matrix.")

        .def(
            "singletB",
            &Type::singletB,
            "Return the singlet B stability matrix.")

        .def(
            "tripletA",
            &Type::tripletA,
            "Return the triplet A stability matrix.")

        .def(
            "tripletB",
            &Type::tripletB,
            "Return the triplet B stability matrix.")

        .def(
            "restrictedUnrestricted",
            [](const Type& stability_matrices) {
                return stability_matrices.restrictedUnrestricted();
            },
            "Return the restricted->unrestricted external RHF stability matrix.")

        .def("isTripletStable",
             &Type::isTripletStable,
             py::arg("threshold") = -1.0e-05,
             "Return a boolean, telling us if the restricted->unrestricted stability matrix belongs to a stable or unstable set of parameters.");
}


void bindRHFStabilityMatrices(py::module& module) {

    // Define Python class related to `real RHFStabilityMatrices` and expose their interfaces.
    py::class_<RHFStabilityMatrices<double>> py_RHFStabilityMatrices_d {module, "RHFStabilityMatrices_d", "The real generalized Hartree-Fock stability matrices."};

    // Expose python bindings unique to the real method.
    py_RHFStabilityMatrices_d

        .def(
            "isComplexConjugateStable",
            [](const RHFStabilityMatrices<double>& stability_matrices, const double threshold) {
                return stability_matrices.isComplexConjugateStable(threshold);
            },
            py::arg("threshold") = -1.0e-05,
            "Return a boolean, telling us if the real->complex stability matrix belongs to a stable or unstable set of parameters.");

    // Expose the `HartreeFockRealStability` interface.
    bindQCModelHartreeFockRealStabilityInterface(py_RHFStabilityMatrices_d);

    // Expose the `RHFStability` interface.
    bindQCModelRHFStabilityInterface(py_RHFStabilityMatrices_d);

    // Define Python class related to `complex RHFStabilityMatrices` and expose their interfaces.
    py::class_<RHFStabilityMatrices<complex>> py_RHFStabilityMatrices_cd {module, "RHFStabilityMatrices_cd", "The complex generalized Hartree-Fock stability matrices."};

    // Expose python bindings unique to the complex method.
    py_RHFStabilityMatrices_cd

        .def(
            "isExternallyStable",
            [](const RHFStabilityMatrices<complex>& stability_matrices, const double threshold) {
                return stability_matrices.isExternallyStable(threshold);
            },
            py::arg("threshold") = -1.0e-05,
            "Return a boolean, telling us if the complex valued external stability matrix belongs to a stable or unstable set of parameters.");

    // Expose the `HartreeFockComplexStability` interface.
    bindQCModelHartreeFockComplexStabilityInterface(py_RHFStabilityMatrices_cd);

    // Expose the `RHFStability` interface.
    bindQCModelRHFStabilityInterface(py_RHFStabilityMatrices_cd);
}


}  // namespace gqcpy
