FROM maven:3-eclipse-temurin-21 AS builder

# 配置 Maven 阿里云镜像
RUN mkdir -p /root/.m2 && \
    echo '<?xml version="1.0" encoding="UTF-8"?>\n\
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"\n\
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n\
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">\n\
  <mirrors>\n\
    <mirror>\n\
      <id>aliyunmaven</id>\n\
      <mirrorOf>*</mirrorOf>\n\
      <name>阿里云公共仓库</name>\n\
      <url>https://maven.aliyun.com/repository/public</url>\n\
    </mirror>\n\
  </mirrors>\n\
</settings>' > /root/.m2/settings.xml

WORKDIR /opt/backend

# 先复制所有 pom.xml 文件，利用 Docker 层缓存
# 父 pom
COPY backend/pom.xml ./

# services 聚合模块
COPY backend/services/pom.xml ./services/

# 共享模块
COPY backend/shared/domain-common/pom.xml ./shared/domain-common/
COPY backend/shared/security-common/pom.xml ./shared/security-common/

# 各个服务模块
COPY backend/services/data-annotation-service/pom.xml ./services/data-annotation-service/
COPY backend/services/data-cleaning-service/pom.xml ./services/data-cleaning-service/
COPY backend/services/data-evaluation-service/pom.xml ./services/data-evaluation-service/
COPY backend/services/data-management-service/pom.xml ./services/data-management-service/
COPY backend/services/data-synthesis-service/pom.xml ./services/data-synthesis-service/
COPY backend/services/execution-engine-service/pom.xml ./services/execution-engine-service/
COPY backend/services/main-application/pom.xml ./services/main-application/
COPY backend/services/operator-market-service/pom.xml ./services/operator-market-service/
COPY backend/services/pipeline-orchestration-service/pom.xml ./services/pipeline-orchestration-service/
COPY backend/services/rag-indexer-service/pom.xml ./services/rag-indexer-service/
COPY backend/services/rag-query-service/pom.xml ./services/rag-query-service/

# 使用缓存卷下载依赖（不复制源代码时即可缓存依赖层）
RUN --mount=type=cache,target=/root/.m2/repository \
    cd /opt/backend/services && \
    mvn dependency:go-offline -Dmaven.test.skip=true || true

# 复制所有源代码
COPY backend/ /opt/backend

# 编译打包（使用缓存的依赖）
RUN --mount=type=cache,target=/root/.m2/repository \
    cd /opt/backend/services && \
    mvn clean package -Dmaven.test.skip=true


FROM eclipse-temurin:21-jdk

# 配置 apt 阿里云镜像源
RUN if [ -f /etc/apt/sources.list.d/debian.sources ]; then \
        sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources; \
    elif [ -f /etc/apt/sources.list.d/ubuntu.sources ]; then \
        sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g; s/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list.d/ubuntu.sources; \
    elif [ -f /etc/apt/sources.list ]; then \
        sed -i 's/deb.debian.org/mirrors.aliyun.com/g; s/archive.ubuntu.com/mirrors.aliyun.com/g; s/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list; \
    fi && \
    apt-get update && \
    apt-get install -y vim wget curl rsync python3 python3-pip python-is-python3 dos2unix && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

COPY --from=builder /opt/backend/services/main-application/target/datamate.jar /opt/backend/datamate.jar

COPY scripts/images/backend/start.sh /opt/backend/start.sh

COPY runtime/ops/examples/test_operator/test_operator.tar /opt/backend/test_operator.tar

RUN dos2unix /opt/backend/start.sh \
    && chmod +x /opt/backend/start.sh \
    && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

EXPOSE 8080

ENTRYPOINT ["/opt/backend/start.sh"]

CMD ["java", "-Duser.timezone=Asia/Shanghai", "-jar", "/opt/backend/datamate.jar"]
