"""SwiftKeychain build — compiles Swift source into a Python-loadable .so"""
import os
import shutil
import subprocess
import sys
import sysconfig
from pathlib import Path

from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext


class SwiftBuildExt(build_ext):
    """Custom build_ext that calls `swift build` to compile the Swift extension."""

    def run(self):
        if sys.platform != "darwin":
            raise RuntimeError("swiftkeychain only supports macOS")

        swift_dir = Path(__file__).parent / "swift"
        pkg_config_path = sysconfig.get_config_var("LIBPC") or ""

        env = os.environ.copy()
        env["PKG_CONFIG_PATH"] = pkg_config_path

        print("🔨 Building Swift extension...")
        subprocess.check_call(
            ["swift", "build"],
            cwd=swift_dir,
            env=env,
        )

        # Find the built .dylib
        build_dir = swift_dir / ".build" / "debug"
        dylib = build_dir / "libSwiftKeychain.dylib"
        if not dylib.exists():
            raise RuntimeError(f"Build succeeded but {dylib} not found")

        # Copy to package dir — setuptools will pick it up as package_data
        dest = Path(__file__).parent / "swiftkeychain" / "swiftkeychain.so"
        print(f"📦 Installing {dylib.name} → {dest}")
        shutil.copy2(dylib, dest)

    def get_ext_filename(self, ext_name):
        return ext_name + ".so"


# Dummy extension so setuptools invokes build_ext
# The actual compilation is done by SwiftBuildExt via `swift build`
setup(
    ext_modules=[Extension("swiftkeychain._swift", sources=[])],
    cmdclass={"build_ext": SwiftBuildExt},
)
