FROM debian:bookworm-slim

# Avoid prompts from apt
ENV DEBIAN_FRONTEND=noninteractive

# Install base packages for GIS tools
RUN apt-get update && apt-get install -y \
    bash \
    curl \
    wget \
    git \
    python3 \
    python3-pip \
    build-essential \
    sudo \
    && rm -rf /var/lib/apt/lists/*

# Create symbolic link for python
RUN ln -s /usr/bin/python3 /usr/bin/python

# Install GIS tools
RUN apt-get update && apt-get install -y \
    gdal-bin \
    libgdal-dev \
    proj-bin \
    proj-data \
    libproj-dev \
    postgis \
    postgresql-client \
    && rm -rf /var/lib/apt/lists/*

# Install DuckDB with spatial extensions
RUN curl -L https://github.com/duckdb/duckdb/releases/latest/download/duckdb_cli-linux-amd64.zip \
    -o /tmp/duckdb.zip && \
    cd /tmp && \
    unzip duckdb.zip && \
    cp duckdb_*_linux_amd64 /usr/local/bin/duckdb && \
    chmod +x /usr/local/bin/duckdb && \
    rm -rf /tmp/duckdb*

# Install Python GIS libraries
RUN pip3 install --break-system-packages \
    geopandas \
    shapely \
    rasterio \
    folium \
    plotly \
    xarray \
    cfgrib \
    netcdf4 \
    matplotlib \
    seaborn \
    duckdb \
    dask

# Install R with spatial packages (if needed)
RUN apt-get update && apt-get install -y \
    r-base \
    r-base-dev \
    && rm -rf /var/lib/apt/lists/*

# Install additional tools for weather data processing
RUN apt-get update && apt-get install -y \
    ftp \
    && rm -rf /var/lib/apt/lists/*

# Install R spatial packages
RUN R --slave -e "install.packages(c('raster', 'rgdal', 'ncdf4', 'rasterVis', 'leaflet'), repos='https://cran.rstudio.com/', dependencies=TRUE)"

# Add entrypoint script
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# Create a working directory
WORKDIR /workspace

# Use the entrypoint script to handle user creation
ENTRYPOINT ["/entrypoint.sh"]