# VulnManager Dockerfile
FROM python:3.11-slim

# Security: Run as non-root user
RUN useradd -m -u 1000 vulnmanager

WORKDIR /app

# Install system dependencies.
# - postgresql-client: alembic / psql tooling
# - dos2unix: entrypoint line-endings
# - libxml2-dev, libxmlsec1-dev, libxmlsec1-openssl, pkg-config:
#     required by python3-saml (XML signature validation)
# - libsasl2-dev, libldap2-dev, libssl-dev:
#     required by python-ldap if used (ldap3 is pure-python so optional,
#     kept here for future flexibility)
RUN apt-get update && apt-get install -y --no-install-recommends \
    postgresql-client \
    dos2unix \
    libxml2-dev \
    libxmlsec1-dev \
    libxmlsec1-openssl \
    pkg-config \
    libsasl2-dev \
    libldap2-dev \
    libssl-dev \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Copy requirements and install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code (and entrypoint)
COPY . .

# Set permissions for entrypoint
RUN dos2unix /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh
RUN chown -R vulnmanager:vulnmanager /app

# Switch to non-root user
USER vulnmanager

# Env vars defaults
ENV POSTGRES_HOST=postgres
ENV POSTGRES_USER=vulnmanager
ENV POSTGRES_DB=vulnmanager

# Expose port
EXPOSE 8000

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')"

# Entrypoint
ENTRYPOINT ["/app/entrypoint.sh"]

# Start application
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
