# ============================================
# Stage 1: Build Flutter Web Application
# ============================================
FROM ghcr.io/cirruslabs/flutter:stable AS builder

USER root

WORKDIR /app

# Safe directory configuration for git
RUN git config --global --add safe.directory /app

# Optimize DART VM Memory for build to prevent OOM
ENV DART_VM_OPTIONS="--old_gen_heap_size=4096"

# Enable web support (idempotent)
RUN flutter config --enable-web

# Copy dependency files first for better caching
# Note: pubspec.lock will be generated by flutter pub get if missing
# Cache buster - INCREMENT THIS VALUE TO FORCE REBUILD ON DEPLOY
ARG CACHEBUST=2025-12-13-v7
COPY pubspec.yaml ./

# Get dependencies (generates pubspec.lock automatically)
RUN flutter pub get

# Copy source code
COPY . .

# Re-run pub get after copying source to ensure lockfile consistency
RUN flutter pub get

# Generate code (for Riverpod and Hive)
RUN dart run build_runner build --delete-conflicting-outputs

# Clean build environment to avoid conflicts
RUN flutter clean

# Build web application (CanvasKit renderer is now default)
# Added --no-tree-shake-icons to prevent potential font issues in some environments
RUN flutter build web --release --base-href="/" --no-wasm-dry-run --no-tree-shake-icons --verbose

# ============================================
# Stage 2: Serve with custom Dart server (with API proxy)
# ============================================
# Use NVIDIA CUDA base image for GPU support
FROM nvidia/cuda:12.2.0-runtime-ubuntu22.04

# Install Dart SDK
RUN apt-get update && apt-get install -y \
    apt-transport-https \
    wget \
    gnupg \
    && wget -qO- https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor -o /usr/share/keyrings/dart.gpg \
    && echo 'deb [signed-by=/usr/share/keyrings/dart.gpg arch=amd64] https://storage.googleapis.com/download.dartlang.org/linux/debian stable main' | tee /etc/apt/sources.list.d/dart_stable.list \
    && apt-get update && apt-get install -y dart \
    && rm -rf /var/lib/apt/lists/*

ENV PATH="/usr/lib/dart/bin:$PATH"

WORKDIR /app

# Install SQLite3 library for FFI
RUN apt-get update && apt-get install -y sqlite3 libsqlite3-dev && rm -rf /var/lib/apt/lists/*

# Install FFmpeg with NVIDIA NVENC support (static build from johnvansickle.com)
RUN apt-get update && apt-get install -y xz-utils \
    && wget -q https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz \
    && tar xf ffmpeg-release-amd64-static.tar.xz \
    && mv ffmpeg-*-amd64-static/ffmpeg /usr/local/bin/ \
    && mv ffmpeg-*-amd64-static/ffprobe /usr/local/bin/ \
    && rm -rf ffmpeg-* \
    && rm -rf /var/lib/apt/lists/*

# Create directory for HLS stream output
RUN mkdir -p /tmp/streams && chmod 777 /tmp/streams

# Copy pubspec.yaml (required for dependencies)
COPY pubspec.yaml ./

# Copy lib directory (required for shared models)
COPY lib/ ./lib/

# Copy bin directory
COPY bin/ ./bin/

# Get dependencies
RUN dart pub get --directory=bin

# Copy built web application from builder stage
COPY --from=builder /app/build/web /app/web

# Expose port (internal only, not mapped to host)
EXPOSE 8089

# Serve web application with API proxy
WORKDIR /app
CMD ["dart", "run", "bin/server.dart", "--port", "8089", "--path", "/app/web"]
