# Backlog Aggregator Dockerfile with Gitea Scanner
# Multi-project real-time task aggregation server

FROM oven/bun:1 AS base
WORKDIR /app

# Install dependencies including git and cron for Gitea scanning
RUN apt-get update && apt-get install -y git cron openssh-client && rm -rf /var/lib/apt/lists/*

# Install npm dependencies
FROM base AS install
RUN mkdir -p /temp/dev
COPY package.json bun.lock* bunfig.toml /temp/dev/
RUN cd /temp/dev && bun install --frozen-lockfile --ignore-scripts

# Build stage
FROM base AS release
COPY --from=install /temp/dev/node_modules node_modules
COPY . .

# Build CSS (needed for components)
RUN bun run build:css || true

# Make entrypoint executable
RUN chmod +x /app/entrypoint.sh || true

# Create cron job for Gitea sync (every 6 hours)
RUN echo "0 */6 * * * GIT_SSH_COMMAND='ssh -i /tmp/gitea_ed25519 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' cd /app && bun run src/aggregator/gitea-scanner.ts --verbose >> /var/log/gitea-scanner.log 2>&1" > /etc/cron.d/gitea-scanner \
    && chmod 0644 /etc/cron.d/gitea-scanner \
    && crontab /etc/cron.d/gitea-scanner

# Create log file
RUN touch /var/log/gitea-scanner.log

# Expose port
EXPOSE 6420

# Set environment
ENV NODE_ENV=production
ENV PORT=6420

# Use entrypoint script
ENTRYPOINT ["/app/entrypoint.sh"]
