# ============================================================
# Troubleshoot AI Assistant — Docker 镜像
# ============================================================
# 架构：
#   nginx:80 (对外) → Vue SPA / API 反代
#   Flask:8088 (容器内) → 后端 API
#   supervisord → 管理 nginx + Flask 双进程
#
# 前端构建：本地执行 npm run build，产物 frontend/dist/ 打包进镜像
# 数据持久化：通过 volume 挂载，不写入镜像层
# ============================================================

# ===== 运行时镜像 =====
FROM python:3.11-slim AS base

# 系统依赖：bash + nginx + supervisor + curl（健康检查）
RUN apt-get update && apt-get install -y --no-install-recommends \
    bash \
    nginx \
    supervisor \
    curl \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# ---------- Python 依赖层（利用 Docker 缓存） ----------
COPY skill/code/requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir -r /app/requirements.txt

# ---------- 代码层 ----------
COPY skill/code/web/ /app/web/

# 复制知识库 SKILL.md（问题排查助手 prompt 模板）
COPY skill/SKILL.md /app/SKILL.md

# ---------- Vue 前端构建产物（本地 npm run build 后直接复制） ----------
COPY frontend/dist/ /data/dist/

# ---------- 运行时数据目录（volume 挂载点） ----------
RUN mkdir -p /app/data \
    && mkdir -p /app/web/service_monitor/data/reports \
    && mkdir -p /app/web/cache \
    && mkdir -p /app/web/logs \
    && mkdir -p /var/log/supervisor

# 修复 Windows 开发环境产生的 CRLF 行尾（bash 脚本在 Linux 必须是 LF）
RUN find /app/web/service_monitor/assets -name '*.sh' -o -name '*.template' \
    | xargs -r sed -i 's/\r$//'

# ---------- 配置文件 ----------
# nginx 配置
COPY nginx/nginx.conf /etc/nginx/nginx.conf

# supervisord 配置
COPY config/supervisord.conf /etc/supervisor/conf.d/troubleshoot.conf

# ---------- 环境变量 ----------
ENV FLASK_DEBUG=0 \
    PYTHONIOENCODING=utf-8 \
    TROUBLESHOOT_ROOT=/app \
    LANG=C.UTF-8

# nginx 对外端口
EXPOSE 80

WORKDIR /app/web

# supervisord 启动（管理 nginx + Flask 双进程）
CMD ["supervisord", "-c", "/etc/supervisor/conf.d/troubleshoot.conf"]
