"""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 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 (works for both editable and regular install)
        # For regular install, setuptools copies the package dir to site-packages
        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"


setup(cmdclass={"build_ext": SwiftBuildExt})
