FROM eclipse-temurin:21-jdk

ARG ANDROID_API_VERSION=36
ARG ANDROID_BUILD_TOOLS_VERSION=36.0.0
ARG FLUTTER_CHANNEL=stable

ENV ANDROID_SDK_ROOT=/opt/android-sdk \
    ANDROID_HOME=/opt/android-sdk \
    FLUTTER_HOME=/opt/flutter \
    DEBIAN_FRONTEND=noninteractive \
    HOME=/root \
    PUB_CACHE=/root/.pub-cache \
    GRADLE_USER_HOME=/root/.gradle

ENV PATH=${FLUTTER_HOME}/bin:${FLUTTER_HOME}/bin/cache/dart-sdk/bin:${ANDROID_SDK_ROOT}/cmdline-tools/latest/bin:${ANDROID_SDK_ROOT}/platform-tools:${PATH}

RUN apt-get update && apt-get install -y --no-install-recommends \
    bash \
    ca-certificates \
    curl \
    git \
    libglu1-mesa \
    openssh-client \
    unzip \
    wget \
    xz-utils \
    zip \
    && rm -rf /var/lib/apt/lists/*

RUN mkdir -p ${ANDROID_SDK_ROOT}/cmdline-tools \
    && curl -fsSL -o /tmp/commandlinetools.zip https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip \
    && unzip -q /tmp/commandlinetools.zip -d ${ANDROID_SDK_ROOT}/cmdline-tools \
    && mv ${ANDROID_SDK_ROOT}/cmdline-tools/cmdline-tools ${ANDROID_SDK_ROOT}/cmdline-tools/latest \
    && rm /tmp/commandlinetools.zip

RUN yes | sdkmanager --licenses >/dev/null \
    && sdkmanager \
        "platform-tools" \
        "platforms;android-${ANDROID_API_VERSION}" \
        "build-tools;${ANDROID_BUILD_TOOLS_VERSION}" \
        "cmdline-tools;latest"

RUN git clone --depth 1 --branch ${FLUTTER_CHANNEL} https://github.com/flutter/flutter.git ${FLUTTER_HOME} \
    && git config --global --add safe.directory ${FLUTTER_HOME}

RUN flutter config --no-analytics \
    && flutter precache --android \
    && flutter doctor -v || true

WORKDIR /workspace

CMD ["bash"]
