// 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/UHFStabilityMatrices.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 `UHF 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 `UHFStabilityMatrices`.
 */
template <typename Class>
void bindQCModelUHFStabilityInterface(Class& py_class) {

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

    py_class
        .def(
            "spinConservedA",
            &Type::spinConservedA,
            "Return the spin conserved A stability matrix.")

        .def(
            "spinConservedB",
            &Type::spinConservedB,
            "Return the spin conserved B stability matrix.")

        .def(
            "spinUnconservedA",
            &Type::spinUnconservedA,
            "Return the spin unconserved A stability matrix.")

        .def(
            "spinUnconservedB",
            &Type::spinUnconservedB,
            "Return the spin unconserved B stability matrix.")

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

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

        .def(
            "instabilityRotationMatrix",
            [](const Type& stability_matrices, const size_t occupied_alpha_orbitals, const size_t occupied_beta_orbitals, const size_t virtual_alpha_orbitals, const size_t virtual_beta_orbitals, const double& scaling_factor) {
                return stability_matrices.instabilityRotationMatrix(occupied_alpha_orbitals, occupied_beta_orbitals, virtual_alpha_orbitals, virtual_beta_orbitals, scaling_factor);
            },
            py::arg("N_occupied_alpha"),
            py::arg("N_occupied_beta"),
            py::arg("N_virtual_alpha"),
            py::arg("N_virtual_beta"),
            py::arg("scaling_factor") = 1.0,
            "Return the transformation corresponding to the lowest-lying eigenvector of the internal instability Hessian.");
}


void bindUHFStabilityMatrices(py::module& module) {

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

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

        .def(
            "isComplexConjugateStable",
            [](const UHFStabilityMatrices<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_UHFStabilityMatrices_d);

    // Expose the `UHFStability` interface.
    bindQCModelUHFStabilityInterface(py_UHFStabilityMatrices_d);

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

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

        .def(
            "isExternallyStable",
            [](const UHFStabilityMatrices<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_UHFStabilityMatrices_cd);

    // Expose the `UHFStability` interface.
    bindQCModelUHFStabilityInterface(py_UHFStabilityMatrices_cd);
}


}  // namespace gqcpy
