import chalkcompute as cc

from workspace_helpers import (
    HOST_POOL_ID,
    create_network_policy,
    developer_image,
    install_dev_tools,
    provisioner_image,
    validate_workspace,
)


@cc.function(
    name="create-dev-workspace",
    image=provisioner_image(),
    chalk_identity=True,
)
def create_dev_workspace(
    name_suffix: str = "investigation",
    lifetime_minutes: int = 720,
    shared_volume_name: str = "team-dev-context",
    github_secret_name: str = "",
) -> cc.Sandbox:
    validate_workspace(name_suffix, lifetime_minutes, shared_volume_name)
    cc.Volume(shared_volume_name).reload()
    sandbox = cc.Sandbox(
        name=f"dev-{name_suffix}",
        image=install_dev_tools(developer_image()),  # Installs Codex & other tools
        cpu="8",
        memory="16Gi",
        lifetime=f"{lifetime_minutes * 60}s",
        compute_class=cc.ComputeClass.HOST,
        host_pool_id=HOST_POOL_ID,
        chalk_identity=True,
        network_policy=create_network_policy(),
        resource_detectors=[cc.ResourceDetector.GITHUB_PULL_REQUESTS],
        entrypoint=["sleep", "infinity"],
    ).mount_volume(shared_volume_name, "/workspace/shared")

    if github_secret_name:
        sandbox.add_credentials(
            "api.github.com",
            cc.Secret.from_chalk_env(github_secret_name, alias="GH_TOKEN"),
        )
    return sandbox.run()
