# Use Node.js 18 with Alpine Linux as the base image
FROM node:18-alpine
RUN apk add --no-cache python3 make g++ && ln -sf python3 /usr/bin/python

# Create a new user and group for better security
RUN addgroup app && adduser -SG app app \
    && mkdir -p /app/frontend /app/backend /app/shared \
    && chown -R app:app /app

# Switch to the new user
USER app

# Set up the frontend environment
WORKDIR /app/frontend
COPY --chown=app:app ./frontend/package*.json ./
RUN ln -s /app/frontend/src/types /app/shared/types \
    && npm install --legacy-peer-deps --unsafe-perm

# Set up the backend environment
WORKDIR /app/backend
COPY --chown=app:app ./backend/package*.json ./
RUN npm install --legacy-peer-deps --unsafe-perm

# Copy the remaining application files
WORKDIR /app
# After creating directories
RUN mkdir -p /app/backend/assets/upload /app/backend/logs && \
    chown -R app:app /app/backend/assets /app/backend/logs && \
    chmod -R 755 /app/backend/assets /app/backend/logs 
RUN chown -R app:app /app/backend/assets/upload
COPY --chown=app:app . .
RUN chown -R app:app /app

# Expose ports for frontend and backend applications
EXPOSE 3000
EXPOSE 3001

# Start the application
CMD ["sh", "./bin/start-dev.sh"]
