#!/bin/bash

# Docker-based build script for Dew Finance Contracts
# Uses the same Docker image that works for shade-agent-js compilation

DOCKER_IMAGE="pivortex/near-builder@sha256:cdffded38c6cff93a046171269268f99d517237fac800f58e5ad1bcd8d6e2418"
DOCKER_RUN="docker run --rm -v \"$(pwd)\":/workspace $DOCKER_IMAGE"

case "$1" in
    "build"|"")
        echo "Building contract with Docker..."
        eval $DOCKER_RUN cargo near build non-reproducible-wasm
        ;;
    "build-repro")
        echo "Building reproducible contract with Docker..."
        eval $DOCKER_RUN cargo near build reproducible-wasm
        ;;
    "test")
        echo "Running tests with Docker..."
        eval $DOCKER_RUN cargo test
        ;;
    "test-unit")
        echo "Running unit tests..."
        eval cargo test --lib
        ;;
    "test-integration")
        echo "Running integration tests..."
        eval cargo test --test '*'
        ;;
    "check")
        echo "Type checking with Docker..."
        eval $DOCKER_RUN cargo check --target wasm32-unknown-unknown
        ;;
    "fmt")
        echo "Formatting code..."
        eval cargo fmt
        ;;
    "clippy")
        echo "Running clippy with Docker..."
        eval $DOCKER_RUN cargo clippy --target wasm32-unknown-unknown
        ;;
    "clean")
        echo "Cleaning build artifacts..."
        eval cargo clean
        ;;
    "help")
        echo "Usage: $0 [command]"
        echo ""
        echo "Commands:"
        echo "  build, \"\" (default)   Build non-reproducible WASM"
        echo "  build-repro           Build reproducible WASM"
        echo "  test                  Run all tests"
        echo "  test-unit             Run unit tests only"
        echo "  test-integration      Run integration tests only" 
        echo "  check                 Type check for WASM target"
        echo "  fmt                   Format code"
        echo "  clippy                Run clippy lints"
        echo "  clean                 Clean build artifacts"
        echo "  help                  Show this help message"
        ;;
    *)
        echo "Unknown command: $1"
        echo "Run '$0 help' for available commands"
        exit 1
        ;;
esac