from conan import ConanFile
from conan.tools.cmake import CMake, CMakeToolchain, CMakeDeps, cmake_layout
import os

class XdlOrmConan(ConanFile):
    name = "xdlorm"
    version = "1.5.0"
    description = "QxOrm library wrapper/extension"
    license = "GPL-3.0"
    author = "XDL Team"
    topics = ("orm", "database", "qt")
    settings = "os", "compiler", "build_type", "arch"
    package_type = "library"

    options = {
        "shared": [True, False],
        "fPIC": [True, False],
        "enable_boost": [True, False],
        "enable_qt_gui": [True, False],
        "enable_qt_network": [True, False],
        "enable_mongodb": [True, False]
    }
    default_options = {
        "shared": False,
        "fPIC": True,
        "enable_boost": False,
        "enable_qt_gui": True,
        "enable_qt_network": True,
        "enable_mongodb": False
    }

    exports_sources = "CMakeLists.txt", "XdlOrm.cmake", "include/*", "src/*", "inl/*", "qt/*"

    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC

    def configure(self):
        if self.options.get_safe("shared"):
            self.options.rm_safe("fPIC")

    def layout(self):
        cmake_layout(self)

    def requirements(self):
        if self.options.enable_boost:
            self.requires("boost/[>=1.78.0]", transitive_headers=True, transitive_libs=True)
            
        # IMPORTANT: We explicitly exclude Qt from Conan requirements.
        # User requested to use local Qt via find_package(Qt5) instead.
        # CMAKE_PREFIX_PATH should be provided by the consumer to locate Qt.

    def generate(self):
        tc = CMakeToolchain(self)
        tc.variables["_QX_STATIC_BUILD"] = not self.options.shared
        tc.variables["_QX_ENABLE_BOOST"] = self.options.enable_boost
        tc.variables["_QX_ENABLE_QT_GUI"] = self.options.enable_qt_gui
        tc.variables["_QX_ENABLE_QT_NETWORK"] = self.options.enable_qt_network
        tc.variables["_QX_ENABLE_MONGODB"] = self.options.enable_mongodb
        tc.generate()

        deps = CMakeDeps(self)
        deps.generate()

    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

    def package(self):
        cmake = CMake(self)
        cmake.install()

    def package_info(self):
        self.cpp_info.set_property("cmake_file_name", "xdlorm")
        self.cpp_info.set_property("cmake_target_name", "xdlorm::xdlorm")
        
        self.cpp_info.libs = ["XdlOrm"]

        if self.options.enable_boost:
            self.cpp_info.requires.append("boost::boost")
            
        if self.settings.os == "Windows" and not self.options.shared:
            self.cpp_info.defines.append("_QX_STATIC_BUILD")
