#!/usr/bin/env python3
import os
import shutil

def safe_symlink(src, dst):
    if os.path.exists(dst) or os.path.islink(dst):
        if os.path.isdir(dst) and not os.path.islink(dst):
            shutil.rmtree(dst)
        else:
            os.unlink(dst)
    os.symlink(src, dst)

def main():
    # 1. Clean up any old mock_repos and recreate it from our static packaging template
    dest_dir = "./mock_repos"
    if os.path.exists(dest_dir):
        shutil.rmtree(dest_dir)
    shutil.copytree("debian/mock_repos", dest_dir)
    print("Copied static mock templates to mock_repos/")

    # 2. Write workspace_root.bzl into rules_jvm_external mock
    rje_dir = os.path.join(dest_dir, "rules_jvm_external")
    with open(os.path.join(rje_dir, "workspace_root.bzl"), "w") as f:
        f.write(f'WORKSPACE_ROOT = "{os.path.abspath(os.getcwd())}"\n')
    print("Generated rules_jvm_external/workspace_root.bzl")

    # 3. Setup protobuf mock dynamic files
    protobuf_dir = os.path.join(dest_dir, "protobuf")
    
    # Copy well-known protos from system path
    proto_src_dir = os.path.join(protobuf_dir, "google", "protobuf")
    os.makedirs(proto_src_dir, exist_ok=True)
    system_proto_dir = "/usr/include/google/protobuf"
    well_known_protos = ["any.proto", "wrappers.proto", "descriptor.proto", "timestamp.proto", "duration.proto", "empty.proto"]
    for proto in well_known_protos:
        src_path = os.path.join(system_proto_dir, proto)
        dest_path = os.path.join(proto_src_dir, proto)
        if os.path.exists(src_path):
            shutil.copy2(src_path, dest_path)
        else:
            with open(dest_path, "w") as f:
                f.write(f"// Mock {proto}\n")
    print("Copied well-known proto schemas")

    # Symlink system Jars into the packages
    safe_symlink("/usr/share/java/protobuf.jar", os.path.join(protobuf_dir, "protobuf-java.jar"))
    safe_symlink("/usr/share/java/protobuf-util.jar", os.path.join(protobuf_dir, "protobuf-java-util.jar"))
    
    java_core_dir = os.path.join(protobuf_dir, "java", "core")
    java_util_dir = os.path.join(protobuf_dir, "java", "util")
    safe_symlink("/usr/share/java/protobuf.jar", os.path.join(java_core_dir, "protobuf-java.jar"))
    safe_symlink("/usr/share/java/protobuf-util.jar", os.path.join(java_util_dir, "protobuf-java-util.jar"))
    print("Linked system Protobuf Java libraries")

    # Setup blake3 mock dynamic files: bridge system include path (usr/include/blake3.h -> c/blake3.h)
    blake3_dir = os.path.join(dest_dir, "blake3")
    blake3_c_dir = os.path.join(blake3_dir, "c")
    os.makedirs(blake3_c_dir, exist_ok=True)
    system_header = "/usr/include/blake3.h"
    mock_header = os.path.join(blake3_c_dir, "blake3.h")
    if os.path.exists(system_header) and not os.path.exists(mock_header):
        os.symlink(system_header, mock_header)
    print("Bridged system blake3 header")

    # Setup rules_jvm_external mock: generate a valid, minimal 22-byte empty zip archive
    jvm_dir = os.path.join(dest_dir, "rules_jvm_external")
    os.makedirs(jvm_dir, exist_ok=True)
    empty_jar_path = os.path.join(jvm_dir, "empty.jar")
    import zipfile
    with zipfile.ZipFile(empty_jar_path, "w") as z:
        pass
    print("Generated valid empty Jar template")

    # Setup zstd-jni mock dynamic files
    zstd_dir = os.path.join(dest_dir, "zstd-jni")
    safe_symlink("/usr/share/java/zstd-jni.jar", os.path.join(zstd_dir, "zstd-jni.jar"))
    print("Linked system Zstd-JNI libraries")

    # 4. Symlink system rules packages (bazel_skylib, rules_cc, etc.)
    system_mappings = {
        "bazel_skylib": "/usr/share/bazel/tools/skylib",
        "rules_python": "/usr/share/bazel/rules/python",
        "rules_java": "/usr/share/bazel/rules/java",
        "platforms": "/usr/share/bazel/tools/platforms",
        "rules_license": "/usr/share/bazel/rules/license",
        "rules_pkg": "/usr/share/bazel/rules/pkg",
        "rules_cc": "/usr/share/bazel/rules/cc",
        "rules_proto": "/usr/share/bazel/rules/proto"
    }

    for mod, system_dir in system_mappings.items():
        mod_dir = os.path.join(dest_dir, mod)
        os.makedirs(mod_dir, exist_ok=True)
        # Write clean Bzlmod metadata if they don't exist
        if not os.path.exists(os.path.join(mod_dir, "MODULE.bazel")):
            with open(os.path.join(mod_dir, "MODULE.bazel"), "w") as f:
                f.write(f'module(name = "{mod}")\n')
        if not os.path.exists(os.path.join(mod_dir, "BUILD")):
            with open(os.path.join(mod_dir, "BUILD"), "w") as f:
                f.write("# Dummy BUILD\n")

        # Symlink all files from system rules folder
        if os.path.exists(system_dir):
            for item in os.listdir(system_dir):
                src = os.path.join(system_dir, item)
                dst = os.path.join(mod_dir, item)
                safe_symlink(src, dst)
            print(f"Symlinked system {mod} rules")

        # Ensure LICENSE and AUTHORS are present (even as dummies) to satisfy source tarball packaging!
        for metadata_file in ["LICENSE", "AUTHORS"]:
            metadata_path = os.path.join(mod_dir, metadata_file)
            if not os.path.exists(metadata_path):
                with open(metadata_path, "w") as f:
                    f.write(f"Mock {metadata_file} for Bzlmod packaging\n")

    # Setup zlib mock to link against system zlib
    zlib_dir = os.path.join(dest_dir, "zlib")
    os.makedirs(zlib_dir, exist_ok=True)
    with open(os.path.join(zlib_dir, "MODULE.bazel"), "w") as f:
        f.write('module(name = "zlib")\n')
    with open(os.path.join(zlib_dir, "BUILD"), "w") as f:
        f.write('''
cc_library(
    name = "zlib",
    linkopts = ["-lz"],
    visibility = ["//visibility:public"],
)
''')
    print("Generated system-linking mock for zlib")

    # 5. Generate dummy mocks for other overridden unused transitive dependencies
    dummy_modules = [
        "grpc",
        "rules_testing",
        "googletest",
        "with_cfg.bzl",
        "apple_support",
        "abseil-cpp",
        "c-ares",
        "rules_go",
        "rules_kotlin",
        "upb",
        "buildozer"
    ]

    for mod in dummy_modules:
        mod_dir = os.path.join(dest_dir, mod)
        os.makedirs(mod_dir, exist_ok=True)
        with open(os.path.join(mod_dir, "MODULE.bazel"), "w") as f:
            f.write(f'module(name = "{mod}")\n')
        with open(os.path.join(mod_dir, "BUILD"), "w") as f:
            f.write("# Dummy BUILD\n")
    print("Generated dummy mocks for unused transitive dependencies")

    # Defensive self-healing check: ensure mock protoc script is executable!
    protoc_script = os.path.join(protobuf_dir, "protoc.sh")
    if os.path.exists(protoc_script):
        os.chmod(protoc_script, 0o755)

    print("Successfully initialized all offline Bzlmod mocks!")

if __name__ == "__main__":
    main()
