# Étape 1: Build de l'admin Tina
FROM node:24-alpine AS tina-builder

WORKDIR /app

# Copie des fichiers nécessaires pour Tina
COPY package*.json ./
COPY tina ./tina
COPY public ./public
COPY tsconfig.json ./

# Installation des dépendances
RUN npm install

# Build de l'admin Tina
RUN npx tinacms build

# Étape 2: Build du backend Fastify
FROM node:20-alpine AS backend-builder

WORKDIR /app

COPY tina-backend/package*.json ./
RUN npm install

COPY tina-backend/tsconfig.json ./
COPY tina-backend/src ./src
COPY tina-backend/drizzle.config.ts ./

RUN npm run build

# Étape 3: Image finale
FROM node:20-alpine AS runner

WORKDIR /app

# Copie du backend buildé
COPY --from=backend-builder /app/dist ./dist
COPY --from=backend-builder /app/node_modules ./node_modules
COPY --from=backend-builder /app/package*.json ./

# Copie de l'admin Tina buildé
COPY --from=tina-builder /app/admin ./admin

EXPOSE 3000

CMD ["node", "dist/index.js"]
