########################################
# Global build arguments (must be re-declared
# in every stage that needs them)
########################################
ARG GITEA_USER
ARG GITEA_PASS
ARG GITEA_URL=157.66.191.31:3000
ARG GITEA_ORG
ARG REPO_NAME
ARG GIT_BRANCH=main

ARG JAR_NAME=app-1.0.0.jar

########################################
# Stage 1 - Clone Repository + Auto-discover modules
########################################

FROM alpine/git:latest AS source

ARG GITEA_USER
ARG GITEA_PASS
ARG GITEA_URL
ARG GITEA_ORG
ARG REPO_NAME
ARG GIT_BRANCH

WORKDIR /src

RUN git clone --branch ${GIT_BRANCH} --single-branch \
    http://${GITEA_USER}:${GITEA_PASS}@${GITEA_URL}/${GITEA_ORG}/${REPO_NAME}.git

########################################
# Auto-discover backend (*-b), frontend (*-f), and
# db (*-d) top-level folders, regardless of the service
########################################

RUN set -e; \
    BACKEND_DIR=$(find /src/${REPO_NAME} -maxdepth 1 -type d -name "*-b" | head -n1); \
    FRONTEND_DIR=$(find /src/${REPO_NAME} -maxdepth 1 -type d -name "*-f" | head -n1); \
    DB_DIR=$(find /src/${REPO_NAME} -maxdepth 1 -type d -name "*-d" | head -n1); \
    if [ -z "$BACKEND_DIR" ]; then \
        echo "ERROR: No backend module folder (ending in -b) found under /src/${REPO_NAME}"; \
        ls -la /src/${REPO_NAME}; \
        exit 1; \
    fi; \
    if [ -z "$FRONTEND_DIR" ]; then \
        echo "ERROR: No frontend module folder (ending in -f) found under /src/${REPO_NAME}"; \
        ls -la /src/${REPO_NAME}; \
        exit 1; \
    fi; \
    if [ -z "$DB_DIR" ]; then \
        echo "WARNING: No db module folder (ending in -d) found under /src/${REPO_NAME}"; \
    fi; \
    echo "Backend module found : $BACKEND_DIR"; \
    echo "Frontend module found : $FRONTEND_DIR"; \
    echo "DB module found : $DB_DIR"; \
    ln -s "$BACKEND_DIR" /src/backend-module; \
    ln -s "$FRONTEND_DIR" /src/frontend-module; \
    if [ -n "$DB_DIR" ]; then ln -s "$DB_DIR" /src/db-module; fi


########################################
# Stage 2 - Backend Build
########################################

FROM maven:3.9.3-eclipse-temurin-8 AS backend-build

WORKDIR /workspace/backend

COPY --from=source \
    /src/backend-module/authsec_springboot/backend/ \
    ./

RUN mvn clean package -DskipTests


########################################
# Stage 3 - Frontend Build
########################################

FROM node:16.20.2 AS frontend-build

WORKDIR /workspace/frontend

COPY --from=source \
    /src/frontend-module/authsec_angular/frontend/angular-clarity-master/ \
    ./

ENV NODE_OPTIONS=--max_old_space_size=4096

RUN npm install

RUN npm run build


########################################
# Stage 4 - Runtime
########################################

FROM ubuntu:22.04

ARG REPO_NAME
ARG JAR_NAME

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
    apt-get install -y \
        openjdk-17-jre \
        mysql-server \
        nginx \
        supervisor \
        curl \
        wget \
        git \
        nano \
        net-tools \
        procps \
        ca-certificates && \
    rm -f /etc/nginx/conf.d/default.conf && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*


########################################
# Create Directories
########################################

RUN mkdir -p \
    /opt/backend \
    /opt/database \
    /opt/frontend \
    /var/log/supervisor


########################################
# Backend
########################################

COPY --from=backend-build \
    /workspace/backend/target/${JAR_NAME} \
    /opt/backend/app.jar


########################################
# SQL Files
########################################

COPY --from=source \
    /src/backend-module/authsec_springboot/backend/src/main/resources/schema.sql \
    /opt/database/schema.sql

COPY --from=source \
    /src/backend-module/authsec_springboot/backend/src/main/resources/data.sql \
    /opt/database/data.sql


########################################
# Frontend
########################################

COPY --from=frontend-build /workspace/frontend/dist/ /usr/share/nginx/html/

########################################
# Configuration Files
########################################

COPY --from=source \
    /src/${REPO_NAME}/sureops/entrypoint.sh \
    /usr/local/bin/entrypoint.sh

COPY --from=source \
    /src/${REPO_NAME}/sureops/init-db.sh \
    /usr/local/bin/init-db.sh

COPY --from=source \
    /src/${REPO_NAME}/sureops/supervisord.conf \
    /etc/supervisor/conf.d/supervisord.conf

COPY --from=source \
    /src/${REPO_NAME}/sureops/nginx.conf \
    /etc/nginx/nginx.conf


########################################
# Permissions
########################################

RUN chmod +x /usr/local/bin/entrypoint.sh && \
    chmod +x /usr/local/bin/init-db.sh


########################################
# Ports
########################################

EXPOSE 80
EXPOSE 9292


########################################
# Start
########################################

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]