# HCFS Base Image - Production-ready environment with HCFS integration
FROM ubuntu:22.04

LABEL maintainer="anthony@deepblack.cloud"
LABEL description="HCFS-integrated base image for AI agent development environments"
LABEL version="1.0.0"

# Prevent interactive prompts during package installation
ENV DEBIAN_FRONTEND=noninteractive
ENV TERM=xterm-256color

# Set up standard environment
ENV HCFS_WORKSPACE_ROOT=/workspace
ENV HCFS_MOUNT_POINT=/mnt/hcfs
ENV HCFS_API_URL=http://host.docker.internal:8000
ENV HCFS_ENABLED=true
ENV PYTHONPATH=/usr/local/lib/python3.10/site-packages:$PYTHONPATH

# Create agent user for sandboxed execution
RUN groupadd -r agent && useradd -r -g agent -d /home/agent -s /bin/bash agent

# Install system dependencies
RUN apt-get update && apt-get install -y \
    # Core system tools
    curl \
    wget \
    git \
    make \
    build-essential \
    software-properties-common \
    gnupg2 \
    lsb-release \
    ca-certificates \
    apt-transport-https \
    # Development essentials
    vim \
    nano \
    tree \
    jq \
    zip \
    unzip \
    rsync \
    tmux \
    screen \
    htop \
    # Network tools
    net-tools \
    iputils-ping \
    dnsutils \
    # Python and pip
    python3 \
    python3-pip \
    python3-dev \
    python3-venv \
    # FUSE for HCFS mounting
    fuse3 \
    libfuse3-dev \
    # Additional utilities
    sqlite3 \
    openssh-client \
    && rm -rf /var/lib/apt/lists/*

# Set up Python symlinks
RUN ln -sf /usr/bin/python3 /usr/bin/python && \
    ln -sf /usr/bin/pip3 /usr/bin/pip

# Install HCFS Python SDK and dependencies
RUN pip install --no-cache-dir \
    httpx \
    websockets \
    fastapi \
    uvicorn \
    pydantic \
    python-multipart \
    aiofiles \
    sentence-transformers \
    numpy \
    scipy \
    scikit-learn \
    requests \
    pyyaml \
    toml \
    click

# Create directory structure
RUN mkdir -p \
    /workspace \
    /mnt/hcfs \
    /home/agent \
    /home/agent/work \
    /home/agent/.local \
    /home/agent/.cache \
    /opt/hcfs \
    /etc/hcfs \
    /var/log/hcfs

# Set up HCFS integration scripts
COPY scripts/hcfs-init.sh /opt/hcfs/
COPY scripts/hcfs-mount.sh /opt/hcfs/
COPY scripts/hcfs-workspace.sh /opt/hcfs/
COPY scripts/entrypoint.sh /opt/hcfs/
COPY config/hcfs-agent.yaml /etc/hcfs/

# Make scripts executable
RUN chmod +x /opt/hcfs/*.sh

# Install HCFS client library
COPY hcfs-client /opt/hcfs/client
RUN cd /opt/hcfs/client && pip install -e .

# Set up agent workspace
RUN chown -R agent:agent /home/agent /workspace /mnt/hcfs
RUN chmod 755 /home/agent /workspace

# Configure sudo for agent user (needed for FUSE mounts)
RUN echo "agent ALL=(ALL) NOPASSWD: /bin/mount, /bin/umount, /usr/bin/fusermount3" >> /etc/sudoers

# Set default working directory
WORKDIR /home/agent/work

# Environment for development
ENV HOME=/home/agent
ENV USER=agent
ENV SHELL=/bin/bash

# Expose standard ports for development services
EXPOSE 8080 8000 3000 5000

# Set up entrypoint that initializes HCFS workspace
ENTRYPOINT ["/opt/hcfs/entrypoint.sh"]
CMD ["/bin/bash"]