# Multi-stage build РґР»СЏ РµРґРёРЅРѕРіРѕ РѕР±СЂР°Р·Р° frontend + backend

# Stage 1: Build Frontend
FROM node:20-alpine AS frontend-builder
WORKDIR /app/frontend
COPY play-life-web/package*.json ./
RUN npm ci
# РљРѕРїРёСЂСѓРµРј РёСЃС…РѕРґРЅРёРєРё (node_modules РёСЃРєР»СЋС‡РµРЅС‹ С‡РµСЂРµР· .dockerignore)
COPY play-life-web/ .
RUN npm run build

# Stage 2: Build Backend
FROM golang:1.24-alpine AS backend-builder
WORKDIR /app/backend
# Устанавливаем GOPROXY для более надежной загрузки модулей
ENV GOPROXY=https://proxy.golang.org,direct
ENV GOSUMDB=sum.golang.org
COPY play-life-backend/go.mod play-life-backend/go.sum ./
RUN go mod download
COPY play-life-backend/ .
RUN go mod tidy
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

# Stage 3: Final image
FROM alpine:latest

# РЈСЃС‚Р°РЅР°РІР»РёРІР°РµРј РЅРµРѕР±С…РѕРґРёРјС‹Рµ РїР°РєРµС‚С‹
RUN apk --no-cache add \
    ca-certificates \
    nginx \
    supervisor \
    curl \
    tzdata \
    chromium \
    chromium-chromedriver \
    udev \
    ttf-freefont \
    font-noto-emoji

# РЎРѕР·РґР°РµРј РґРёСЂРµРєС‚РѕСЂРёРё
WORKDIR /app

# РљРѕРїРёСЂСѓРµРј СЃРѕР±СЂР°РЅРЅС‹Р№ frontend
COPY --from=frontend-builder /app/frontend/dist /usr/share/nginx/html

# РљРѕРїРёСЂСѓРµРј СЃРѕР±СЂР°РЅРЅС‹Р№ backend
COPY --from=backend-builder /app/backend/main /app/backend/main
COPY play-life-backend/admin.html /app/backend/admin.html

# РљРѕРїРёСЂСѓРµРј РєРѕРЅС„РёРіСѓСЂР°С†РёСЋ nginx
COPY nginx.conf /etc/nginx/nginx.conf
COPY nginx-unified.conf /etc/nginx/conf.d/default.conf

# РљРѕРїРёСЂСѓРµРј РєРѕРЅС„РёРіСѓСЂР°С†РёСЋ supervisor РґР»СЏ Р·Р°РїСѓСЃРєР° backend
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# РЎРѕР·РґР°РµРј РґРёСЂРµРєС‚РѕСЂРёРё РґР»СЏ Р»РѕРіРѕРІ
RUN mkdir -p /var/log/supervisor && \
    mkdir -p /var/log/nginx && \
    mkdir -p /var/run

# РћС‚РєСЂС‹РІР°РµРј РїРѕСЂС‚ 80
EXPOSE 80

# Р—Р°РїСѓСЃРєР°РµРј supervisor, РєРѕС‚РѕСЂС‹Р№ Р·Р°РїСѓСЃС‚РёС‚ nginx Рё backend
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]