# 🧲 MAGION Dockerfile
# Magnetospheric Ionization & Galactic Interaction Observational Network

FROM python:3.10-slim AS builder

LABEL maintainer="gitdeeper@gmail.com"
LABEL version="1.0.0"
LABEL description="MAGION - Real-time magnetospheric shield monitoring"

# Install build dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    gcc \
    g++ \
    gfortran \
    libopenblas-dev \
    liblapack-dev \
    libhdf5-dev \
    libnetcdf-dev \
    wget \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Create virtual environment
ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

# Install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

# Final stage
FROM python:3.10-slim

# Install runtime dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    libopenblas0 \
    libhdf5-103-1 \
    libnetcdf19 \
    curl \
    procps \
    && rm -rf /var/lib/apt/lists/*

# Copy virtual environment from builder
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

# Create magion user
RUN useradd -m -u 1000 -s /bin/bash magion && \
    mkdir -p /opt/magion && \
    mkdir -p /etc/magion && \
    mkdir -p /data/solar_wind && \
    mkdir -p /data/neutron && \
    mkdir -p /data/tec && \
    mkdir -p /data/results && \
    mkdir -p /var/log/magion && \
    chown -R magion:magion /opt/magion /etc/magion /data /var/log/magion

# Set working directory
WORKDIR /opt/magion

# Copy application code
COPY --chown=magion:magion . .

# Install MAGION
RUN pip install --no-cache-dir -e .

# Expose ports
EXPOSE 8000 8501 9090

# Switch to non-root user
USER magion

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:8000/health || exit 1

# Default command
CMD ["magion", "serve", "--all"]
