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
# Will be overridden at runtime with the actual server endpoint
ENV NEXT_PUBLIC_ENTE_ENDPOINT="https://localhost:8080"

# 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 for both Go and Node.js
RUN apt-get update && \
    apt-get install -y curl git nodejs npm golang libsodium23 libsodium-dev pkg-config nginx && \
    npm install -g yarn serve && \
    apt-get clean && apt-get autoremove && \
    rm -rf /var/cache/apt /var/lib/apt/lists

# Set up directory structure
RUN mkdir -p /app/code /app/data/config /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 .

# 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

# Create NGINX directories
RUN mkdir -p /etc/nginx/sites-available /etc/nginx/sites-enabled

# Expose the web port (Cloudron expects port 8080)
EXPOSE 8080

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