# Utilizamos Node.js como base
FROM node:22-bookworm

# Configurar entorno global
ENV NODEJS_ORG_MIRROR=https://nodejs.org/dist \
    NODE_ENV=production

# Instalar herramientas necesarias del sistema
RUN apt-get update && \
    apt-get install -y locales tzdata && \
    rm -rf /var/lib/apt/lists/*

RUN npm install -g typescript

# Configurar timezone a "America/Argentina/Buenos_Aires"
ENV TZ=America/Argentina/Buenos_Aires
RUN ln -sf /usr/share/zoneinfo/America/Argentina/Buenos_Aires /etc/localtime && \
    dpkg-reconfigure -f noninteractive tzdata

# Configurar locale en español (Argentina)
RUN sed -i 's/# es_AR.UTF-8 UTF-8/es_AR.UTF-8 UTF-8/' /etc/locale.gen && \
    locale-gen

ENV LANG=es_AR.UTF-8
ENV LC_ALL=es_AR.UTF-8

# Crear carpetas de trabajo separadas para server y client
WORKDIR /app

# Copiar y construir el servidor
WORKDIR /app/server
COPY server/package*.json ./
RUN npm install --omit=dev=false
COPY server ./
RUN npx tsoa spec-and-routes
RUN npm run build

# Copiar y construir el cliente
WORKDIR /app/txclient
COPY txclient/package*.json ./
RUN npm install --omit=dev=false
COPY txclient ./
RUN npm run build

# Configurar puertos expuestos
EXPOSE 3000 3001 3021

# Comando de inicio que lanza tanto el server como el client
CMD [ "sh", "-c", "npm --prefix /app/server start & npm --prefix /app/txclient start" ]
