import sys
from pathlib import Path
from hatchling.builders.hooks.plugin.interface import BuildHookInterface

class CustomBuildHook(BuildHookInterface):
    def initialize(self, version, build_data):
        """
        Runs immediately BEFORE the build starts.
        You can use this to generate files that need to be included.
        """
        print(f"🚀 Initializing build hook for version: {version}")
        
        # Example: Create a 'build_info.txt' inside your package
        pkg_dir = Path(self.root) / "src" / "yclibs"
        info_file = pkg_dir / "build_info.txt"
        
        with open(info_file, "w") as f:
            f.write(f"Build Version: {version}\n")
            f.write("Generated by Hatchling Build Hook\n")

        # Tell Hatchling to include this generated file in the build
        build_data["force_include"][str(info_file)] = "yclibs/build_info.txt"

    def finalize(self, version, build_data, artifact_path):
        """Runs immediately AFTER the build completes."""
        print("✅ Build finished. Artifact created at:", artifact_path)