# Dockerfile for building Synor Desktop Wallet
# Multi-stage build: Frontend (Node) + Backend (Rust/Tauri)

# ==============================================================================
# Stage 1: Build Frontend
# ==============================================================================
FROM node:20-bookworm AS frontend-builder

WORKDIR /app

# Install pnpm
RUN npm install -g pnpm

# Copy package files
COPY package.json pnpm-lock.yaml* ./

# Install dependencies
RUN pnpm install --frozen-lockfile || pnpm install

# Copy source files
COPY . .

# Build frontend
RUN pnpm build

# ==============================================================================
# Stage 2: Build Tauri Backend
# ==============================================================================
FROM rust:1.85-bookworm AS backend-builder

# Install Tauri build dependencies
RUN apt-get update && apt-get install -y \
    libwebkit2gtk-4.1-dev \
    build-essential \
    curl \
    wget \
    file \
    libssl-dev \
    libayatana-appindicator3-dev \
    librsvg2-dev \
    cmake \
    clang \
    libclang-dev \
    pkg-config \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Copy Rust workspace files from root
COPY --from=frontend-builder /app/dist ./dist

# Copy Tauri source
COPY src-tauri ./src-tauri

# Copy monorepo crates (needed for local dependencies)
# Note: In CI/CD, this would be handled differently
COPY ../../../crates ./crates 2>/dev/null || true

WORKDIR /app/src-tauri

# Build release binary
RUN cargo build --release

# ==============================================================================
# Stage 3: Development environment
# ==============================================================================
FROM node:20-bookworm AS development

# Install Rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"

# Install Tauri dependencies
RUN apt-get update && apt-get install -y \
    libwebkit2gtk-4.1-dev \
    build-essential \
    curl \
    wget \
    file \
    libssl-dev \
    libayatana-appindicator3-dev \
    librsvg2-dev \
    cmake \
    clang \
    libclang-dev \
    pkg-config \
    && rm -rf /var/lib/apt/lists/*

# Install pnpm and Tauri CLI
RUN npm install -g pnpm

WORKDIR /app

# Copy everything
COPY . .

# Install dependencies
RUN pnpm install

# Install Tauri CLI globally
RUN cargo install tauri-cli --version "^2.0.0"

# Default command for development
CMD ["pnpm", "tauri", "dev"]
