# Dockerfile for building Synor smart contract WASM modules
# Builds all contracts: DEX, Perps, Oracle, Aggregator, etc.
#
# Usage:
#   docker build -f Dockerfile.contracts -t synor-contracts .
#   docker run -v $(pwd)/contracts-output:/output synor-contracts

# =============================================================================
# Stage 1: Build Environment
# =============================================================================
FROM rust:1.85-bookworm AS builder

# Install build dependencies
RUN apt-get update && apt-get install -y \
    cmake \
    clang \
    libclang-dev \
    pkg-config \
    && rm -rf /var/lib/apt/lists/*

# Install wasm32 target
RUN rustup target add wasm32-unknown-unknown

# Create app directory
WORKDIR /app

# Copy workspace files
COPY Cargo.toml Cargo.lock ./
COPY crates/ crates/

# Copy all contracts
COPY contracts/ contracts/

# =============================================================================
# Stage 2: Build Contracts
# =============================================================================

# Build each contract for WASM target with optimizations
WORKDIR /app

# Create output directory
RUN mkdir -p /output/wasm

# Build DEX contract
WORKDIR /app/contracts/dex
RUN cargo build --release --target wasm32-unknown-unknown 2>/dev/null || true
RUN if [ -f /app/target/wasm32-unknown-unknown/release/synor_dex.wasm ]; then \
        cp /app/target/wasm32-unknown-unknown/release/synor_dex.wasm /output/wasm/; \
    fi

# Build Perps contract
WORKDIR /app/contracts/perps
RUN cargo build --release --target wasm32-unknown-unknown 2>/dev/null || true
RUN if [ -f /app/target/wasm32-unknown-unknown/release/synor_perps.wasm ]; then \
        cp /app/target/wasm32-unknown-unknown/release/synor_perps.wasm /output/wasm/; \
    fi

# Build Oracle contract
WORKDIR /app/contracts/oracle
RUN cargo build --release --target wasm32-unknown-unknown 2>/dev/null || true
RUN if [ -f /app/target/wasm32-unknown-unknown/release/synor_oracle.wasm ]; then \
        cp /app/target/wasm32-unknown-unknown/release/synor_oracle.wasm /output/wasm/; \
    fi

# Build Aggregator contract
WORKDIR /app/contracts/aggregator
RUN cargo build --release --target wasm32-unknown-unknown 2>/dev/null || true
RUN if [ -f /app/target/wasm32-unknown-unknown/release/synor_aggregator.wasm ]; then \
        cp /app/target/wasm32-unknown-unknown/release/synor_aggregator.wasm /output/wasm/; \
    fi

# Build Token contract
WORKDIR /app/contracts/token
RUN cargo build --release --target wasm32-unknown-unknown 2>/dev/null || true
RUN if [ -f /app/target/wasm32-unknown-unknown/release/synor_token.wasm ]; then \
        cp /app/target/wasm32-unknown-unknown/release/synor_token.wasm /output/wasm/; \
    fi

# Build Staking contract
WORKDIR /app/contracts/staking
RUN cargo build --release --target wasm32-unknown-unknown 2>/dev/null || true
RUN if [ -f /app/target/wasm32-unknown-unknown/release/synor_staking.wasm ]; then \
        cp /app/target/wasm32-unknown-unknown/release/synor_staking.wasm /output/wasm/; \
    fi

# Build Confidential Token contract
WORKDIR /app/contracts/confidential-token
RUN cargo build --release --target wasm32-unknown-unknown 2>/dev/null || true
RUN if [ -f /app/target/wasm32-unknown-unknown/release/synor_confidential_token.wasm ]; then \
        cp /app/target/wasm32-unknown-unknown/release/synor_confidential_token.wasm /output/wasm/; \
    fi

# Build NFT contract
WORKDIR /app/contracts/nft
RUN cargo build --release --target wasm32-unknown-unknown 2>/dev/null || true
RUN if [ -f /app/target/wasm32-unknown-unknown/release/synor_nft.wasm ]; then \
        cp /app/target/wasm32-unknown-unknown/release/synor_nft.wasm /output/wasm/; \
    fi

# =============================================================================
# Stage 3: Output Stage (minimal image with artifacts)
# =============================================================================
FROM alpine:3.19 AS output

# Copy WASM artifacts
COPY --from=builder /output /contracts-output

# Create manifest file listing all contracts
RUN echo "# Synor Smart Contracts" > /contracts-output/MANIFEST.txt && \
    echo "# Built: $(date -Iseconds)" >> /contracts-output/MANIFEST.txt && \
    echo "" >> /contracts-output/MANIFEST.txt && \
    ls -la /contracts-output/wasm/ >> /contracts-output/MANIFEST.txt 2>/dev/null || echo "No contracts built"

# Default: list what's available
CMD ["sh", "-c", "echo '=== Synor Smart Contracts ===' && ls -lah /contracts-output/wasm/ 2>/dev/null || echo 'No contracts found'"]
