from pathlib import Path
from conan import ConanFile
from collections import OrderedDict
from conan.tools.files import copy


class ConanOpenVapiDevSources(ConanFile):
    name = "open-vehicle-api-dev-sources"
    license = "Apache-2.0"
    author = "thommyho <thommyho1988@gmail.com>"
    url = "https://github.com/eclipse-openvehicle-api/openvehicle-api"
    homepage = "https://eclipse.dev/openvehicle-api/"

    # "header-library" indicates this package contains only headers/sources, no binaries
    package_type = "header-library"

    description = (
        "The Eclipse Open Vehicle API (VAPI) development sources/headers "
        "required for building VAPI components."
    )
    topics = ("automotive", "sdv", "headers", "c++", "sdk")

    # Settings are required to resolve the dependency, but this package itself is arch-independent
    settings = "os", "arch", "compiler", "build_type"

    exports_sources = "LICENSE"

    def build_requirements(self):
        # We need the full dev package to extract the headers from it
        self.tool_requires(f"open-vehicle-api-dev-full/{self.version}")

    def package_id(self):
        # Header files are typically platform-independent.
        # using header_only() removes os, arch, compiler, and build_type from the ID.
        self.info.clear()

    def package(self):
        # Get the build environment from the 'open-vehicle-api-dev' requirement
        dep_name = f"open-vehicle-api-dev-full/{self.version}"

        # Safety check
        if dep_name not in self.dependencies.build:
            self.output.warning(f"Dependency {dep_name} not found in build context!")
            return

        env = self.dependencies.build[dep_name].buildenv_info.vars(self)
        vars_map = OrderedDict(env)

        # Get the path to the 'export' directory from the dependency
        # Based on previous context, this variable is 'SDV_FRAMEWORK_DEV_INCLUDE'
        path_dev_include_str = vars_map.get("SDV_FRAMEWORK_DEV_INCLUDE")

        if path_dev_include_str:
            path_dev_include = Path(path_dev_include_str)

            # Copy all contents from the export folder to our package's include directory
            copy(
                self,
                "*",
                src=path_dev_include,
                dst=Path(self.package_folder) / "export",
            )
        else:
            self.output.error(
                "SDV_FRAMEWORK_DEV_INCLUDE environment variable not found in dependency!"
            )

        # Copy License
        license_src = Path(self.recipe_folder) / ".." / "es"
        if license_src.exists():
            copy(self, "LICENSE*", src=license_src, dst=self.package_folder)

    def package_info(self):
        # Tell consumers where the headers are
        self.cpp_info.includedirs = ["export"]

        # Define environment variable so consumers can find the headers explicitly if needed
        pkg_path = Path(self.package_folder)
        include_path = (pkg_path / "export").as_posix()

        self.runenv_info.define_path("SDV_FRAMEWORK_DEV_INCLUDE", include_path)
        self.buildenv_info.define_path("SDV_FRAMEWORK_DEV_INCLUDE", include_path)
