FROM node:20-bookworm-slim as web-builder

WORKDIR /ente

# Clone the repository for web app building
RUN apt-get update && apt-get install -y git && \
    git clone --depth=1 https://github.com/ente-io/ente.git . && \
    apt-get clean && apt-get autoremove && \
    rm -rf /var/cache/apt /var/lib/apt/lists

# Will help default to yarn version 1.22.22
RUN corepack enable

# Set environment variables for web app build
# Use "/api" as the endpoint which will be replaced at runtime with the full URL
ENV NEXT_PUBLIC_ENTE_ENDPOINT="/api"
# Add a note for clarity
RUN echo "Building with NEXT_PUBLIC_ENTE_ENDPOINT=/api, will be replaced at runtime with full URL"

# Debugging the repository structure
RUN find . -type d -maxdepth 3 | sort

# Check if web directory exists with apps subdirectory
RUN mkdir -p /build/web/photos /build/web/accounts /build/web/auth /build/web/cast && \
    if [ -d "web" ] && [ -d "web/apps" ]; then \
        echo "Found web/apps directory, building web apps"; \
        cd web && \
        yarn cache clean && \
        yarn install --network-timeout 1000000000 && \
        yarn build:photos && \
        yarn build:accounts && \
        yarn build:auth && \
        yarn build:cast && \
        if [ -d "apps/photos/out" ]; then \
            cp -r apps/photos/out/* /build/web/photos/; \
        fi && \
        if [ -d "apps/accounts/out" ]; then \
            cp -r apps/accounts/out/* /build/web/accounts/; \
        fi && \
        if [ -d "apps/auth/out" ]; then \
            cp -r apps/auth/out/* /build/web/auth/; \
        fi && \
        if [ -d "apps/cast/out" ]; then \
            cp -r apps/cast/out/* /build/web/cast/; \
        fi; \
    elif [ -d "web" ]; then \
        echo "Found web directory, looking for alternative structure"; \
        find web -type d | grep -v node_modules | sort; \
        if [ -d "web/photos" ]; then \
            echo "Building photos app"; \
            cd web/photos && yarn install && yarn build && \
            if [ -d "out" ]; then cp -r out/* /build/web/photos/; fi; \
        fi; \
        if [ -d "web/accounts" ]; then \
            echo "Building accounts app"; \
            cd web/accounts && yarn install && yarn build && \
            if [ -d "out" ]; then cp -r out/* /build/web/accounts/; fi; \
        fi; \
        if [ -d "web/auth" ]; then \
            echo "Building auth app"; \
            cd web/auth && yarn install && yarn build && \
            if [ -d "out" ]; then cp -r out/* /build/web/auth/; fi; \
        fi; \
        if [ -d "web/cast" ]; then \
            echo "Building cast app"; \
            cd web/cast && yarn install && yarn build && \
            if [ -d "out" ]; then cp -r out/* /build/web/cast/; fi; \
        fi; \
    else \
        echo "Web directory not found, creating placeholder web pages"; \
        # Create placeholder HTML files for each app \
        mkdir -p /build/web/photos /build/web/accounts /build/web/auth /build/web/cast; \
        echo "<html><body><h1>Ente Photos</h1><p>Web app not available. Please check the build logs.</p></body></html>" > /build/web/photos/index.html; \
        echo "<html><body><h1>Ente Accounts</h1><p>Web app not available. Please check the build logs.</p></body></html>" > /build/web/accounts/index.html; \
        echo "<html><body><h1>Ente Auth</h1><p>Web app not available. Please check the build logs.</p></body></html>" > /build/web/auth/index.html; \
        echo "<html><body><h1>Ente Cast</h1><p>Web app not available. Please check the build logs.</p></body></html>" > /build/web/cast/index.html; \
    fi

FROM cloudron/base:5.0.0@sha256:04fd70dbd8ad6149c19de39e35718e024417c3e01dc9c6637eaf4a41ec4e596c

# Install necessary packages and Caddy webserver
RUN apt-get update && \
    apt-get install -y curl git nodejs npm libsodium23 libsodium-dev pkg-config postgresql-client && \
    npm install -g yarn serve && \
    # Install Caddy for web server
    apt-get install -y debian-keyring debian-archive-keyring apt-transport-https && \
    curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg && \
    curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list && \
    apt-get update && \
    apt-get install -y caddy && \
    apt-get clean && apt-get autoremove && \
    rm -rf /var/cache/apt /var/lib/apt/lists

# Install Go 1.24.1
RUN curl -L https://go.dev/dl/go1.24.1.linux-amd64.tar.gz -o go.tar.gz && \
    rm -rf /usr/local/go && \
    tar -C /usr/local -xzf go.tar.gz && \
    rm go.tar.gz && \
    ln -sf /usr/local/go/bin/go /usr/local/bin/go && \
    ln -sf /usr/local/go/bin/gofmt /usr/local/bin/gofmt

# Set up directory structure
RUN mkdir -p /app/code /app/data/config /app/data/caddy /app/web

WORKDIR /app/code

# Clone the ente repository during build (for the Museum server)
RUN git clone --depth=1 https://github.com/ente-io/ente.git . && \
    sed -i 's/go 1.23/go 1.24.1/' server/go.mod && \
    mkdir -p /app/data/go && \
    cp -r server/go.mod server/go.sum /app/data/go/ && \
    chmod 777 /app/data/go/go.mod /app/data/go/go.sum

# Pre-download Go dependencies
RUN cd server && \
    export GOMODCACHE="/app/data/go/pkg/mod" && \
    export GOFLAGS="-modfile=/app/data/go/go.mod -mod=mod" && \
    export GOTOOLCHAIN=local && \
    export GO111MODULE=on && \
    export GOSUMDB=off && \
    mkdir -p /app/data/go/pkg/mod && \
    chmod -R 777 /app/data/go && \
    go mod download

# Set Go environment variables
ENV GOTOOLCHAIN=local
ENV GO111MODULE=on
ENV GOFLAGS="-modfile=/app/data/go/go.mod -mod=mod"
ENV PATH="/usr/local/go/bin:${PATH}"
ENV GOSUMDB=off
ENV GOMODCACHE="/app/data/go/pkg/mod"

# Copy the web app built files from the first stage
COPY --from=web-builder /build/web/photos /app/web/photos
COPY --from=web-builder /build/web/accounts /app/web/accounts
COPY --from=web-builder /build/web/auth /app/web/auth
COPY --from=web-builder /build/web/cast /app/web/cast

# Copy configuration and startup scripts
ADD start.sh /app/pkg/
ADD config.template.yaml /app/pkg/

# Set proper permissions
RUN chmod +x /app/pkg/start.sh

# Expose the web port (Cloudron expects port 3080)
EXPOSE 3080
# Also expose API port
EXPOSE 8080

# Start the application
CMD ["/app/pkg/start.sh"]