import os
from os.path import expanduser
from pathlib import Path, PurePosixPath, PureWindowsPath

# AICAGE PATHS (for Python program on host)

_AGENT_DEFINITION_FILENAME: str = "agent.yaml"
EXTENDED_IMAGE_DEFINITION_FILENAME: str = "image-extended.yaml"

_CONFIG_BASE_DIR: Path = Path(expanduser("~/.aicage"))
PROJECTS_DIR: Path = _CONFIG_BASE_DIR / "projects"

BASE_IMAGE_BUILD_STATE_DIR: Path = _CONFIG_BASE_DIR / "state/base-image/build"
IMAGE_BUILD_STATE_DIR: Path = _CONFIG_BASE_DIR / "state/image/build"
AGENT_VERSION_CHECK_STATE_DIR: Path = _CONFIG_BASE_DIR / "state/agent/version-check/state"
IMAGE_EXTENDED_STATE_DIR: Path = _CONFIG_BASE_DIR / "state/image-extended/state"
IMAGE_EXTENDED_BUILD_STATE_DIR: Path =  _CONFIG_BASE_DIR / "state/image-extended/build"

_LOG_DIR: Path = _CONFIG_BASE_DIR / "logs"
GLOBAL_LOG_PATH: Path = _LOG_DIR / "aicage.log"
IMAGE_PULL_LOG_DIR: Path = _LOG_DIR / "image/pull"
BASE_IMAGE_BUILD_LOG_DIR: Path = _LOG_DIR / "base-image/build"
IMAGE_BUILD_LOG_DIR: Path = _LOG_DIR / "image/build"
IMAGE_EXTENDED_BUILD_LOG_DIR: Path = _LOG_DIR / "image-extended/build"

# Only user-generated custom files outside ~/.aicage.
_CUSTOM_ROOT_DIR: Path = Path(expanduser("~/.aicage-custom"))

CUSTOM_BASES_DIR: Path = _CUSTOM_ROOT_DIR / "base-images"
CUSTOM_BASE_DEFINITION_FILES: tuple[str, str] = (
    "base.yaml",
    "base.yml",
)

CUSTOM_AGENTS_DIR: Path = _CUSTOM_ROOT_DIR / "agents"
CUSTOM_AGENT_DEFINITION_FILES: tuple[str, str] = (
    _AGENT_DEFINITION_FILENAME,
    "agent.yml",
)

CUSTOM_EXTENSIONS_DIR: Path = _CUSTOM_ROOT_DIR / "extensions"
CUSTOM_EXTENSION_DEFINITION_FILES: tuple[str, str] = (
    "extension.yaml",
    "extension.yml",
)

# OTHER HOST PATHS (for mounts to container)

HOST_SSH_DIR: Path = Path.home() / ".ssh"
HOST_DOCKER_SOCKET_PATH: Path = Path("/run/docker.sock")

# CONTAINER PATHS (for mounts to container)

CONTAINER_WORKSPACE_DIR: PurePosixPath = PurePosixPath("/workspace")
_WSL_MOUNT_ROOT: PurePosixPath = PurePosixPath("/mnt")
CONTAINER_AGENT_CONFIG_DIR: PurePosixPath = PurePosixPath("/aicage/agent-config")
CONTAINER_GITCONFIG_PATH: PurePosixPath = PurePosixPath("/aicage/host/gitconfig")
CONTAINER_SSH_DIR: PurePosixPath = PurePosixPath("/aicage/host/ssh")
CONTAINER_GNUPG_DIR: PurePosixPath = PurePosixPath("/aicage/host/gnupg")
CONTAINER_ENTRYPOINT_PATH: PurePosixPath = PurePosixPath("/usr/local/bin/entrypoint.sh")
CONTAINER_DOCKER_SOCKET_PATH: PurePosixPath = PurePosixPath("/run/docker.sock")


def container_project_path(host_path: Path) -> PurePosixPath:
    if os.name != "nt":
        return PurePosixPath(host_path.as_posix())
    win_path = PureWindowsPath(host_path)
    drive = win_path.drive
    if drive.startswith("\\\\?\\"):
        drive = drive[4:]
    drive_letter = drive.rstrip(":").lower()
    if not drive_letter:
        return PurePosixPath(host_path.as_posix())
    parts = [part for part in win_path.parts if part != win_path.anchor]
    return _WSL_MOUNT_ROOT / drive_letter / PurePosixPath(*parts)
