# OpenAccounting Server - Just recipes
# https://github.com/casey/just

# Default recipe
default:
    @just --list

# Variables
image_name := "openaccounting-server"
tag := "latest"

# Build the Go application
build:
    @echo "Building OpenAccounting Server..."
    go build -o server ./core/

# Run the server locally
run: build
    @echo "Starting server locally..."
    ./server

# Run with custom environment
run-dev: build
    @echo "Starting server in development mode..."
    OA_DATABASE_DRIVER=sqlite OA_DATABASE_FILE=./dev.db OA_PORT=8080 ./server

# Run tests
test:
    @echo "Running tests..."
    go test ./...

# Clean build artifacts
clean:
    @echo "Cleaning up..."
    rm -f server
    rm -f *.db

# Docker recipes

# Build Docker image
docker-build:
    @echo "Building Docker image: {{image_name}}:{{tag}}"
    docker build -t {{image_name}}:{{tag}} .

# Run container with SQLite (development)
docker-run: docker-build
    @echo "Running container with SQLite..."
    docker run --rm -p 8080:8080 \
        -e OA_DATABASE_DRIVER=sqlite \
        -e OA_DATABASE_FILE=/app/data/openaccounting.db \
        -v $(pwd)/data:/app/data \
        {{image_name}}:{{tag}}

# Run container with MySQL (production example)
docker-run-mysql: docker-build
    @echo "Running container with MySQL (requires external MySQL)..."
    docker run --rm -p 8080:8080 \
        -e OA_DATABASE_DRIVER=mysql \
        -e OA_DATABASE_ADDRESS=mysql:3306 \
        -e OA_DATABASE=openaccounting \
        -e OA_USER=openaccounting \
        -e OA_PASSWORD=secret \
        {{image_name}}:{{tag}}

# Run with docker-compose (if you create one)
docker-compose-up:
    @echo "Starting with docker-compose..."
    docker-compose up -d

docker-compose-down:
    @echo "Stopping docker-compose..."
    docker-compose down

# Development utilities

# Format code
fmt:
    @echo "Formatting code..."
    go fmt ./...

# Lint code (requires golangci-lint)
lint:
    @echo "Linting code..."
    golangci-lint run

# Install development dependencies
install-deps:
    @echo "Installing development dependencies..."
    go mod download
    go mod vendor

# Update dependencies
update-deps:
    @echo "Updating dependencies..."
    go get -u ./...
    go mod tidy
    go mod vendor

# Database utilities

# Create SQLite database directory
init-db:
    @echo "Creating database directory..."
    mkdir -p data

# Reset SQLite database
reset-db:
    @echo "Resetting SQLite database..."
    rm -f *.db data/*.db

# Migration recipes

# Run database migrations manually (if needed)
migrate:
    @echo "Running database migrations..."
    go run ./core/ --migrate-only || echo "Migration command not implemented yet"

# Production utilities

# Build for production
build-prod:
    @echo "Building for production..."
    CGO_ENABLED=1 GOOS=linux go build -a -installsuffix cgo -ldflags="-w -s" -o server ./core/

# Create release tarball
release: build-prod
    @echo "Creating release package..."
    tar -czf openaccounting-server-$(date +%Y%m%d).tar.gz server config.json.sample README.md

# Security scan (requires trivy)
security-scan:
    @echo "Scanning Docker image for vulnerabilities..."
    trivy image {{image_name}}:{{tag}}

# Show configuration help
config-help:
    @echo "OpenAccounting Server Configuration:"
    @echo ""
    @echo "Environment Variables (prefix with OA_):"
    @echo "  OA_ADDRESS                Server address (default: localhost)"
    @echo "  OA_PORT                   Server port (default: 8080)"
    @echo "  OA_API_PREFIX             API prefix (default: /api/v1)"
    @echo "  OA_DATABASE_DRIVER        Database driver: sqlite or mysql (default: sqlite)"
    @echo "  OA_DATABASE_FILE          SQLite database file (default: ./openaccounting.db)"
    @echo "  OA_DATABASE_ADDRESS       MySQL address (e.g., localhost:3306)"
    @echo "  OA_DATABASE               MySQL database name"
    @echo "  OA_USER                   Database username"
    @echo "  OA_PASSWORD               Database password (recommended for security)"
    @echo "  OA_MAILGUN_DOMAIN         Mailgun domain"
    @echo "  OA_MAILGUN_KEY            Mailgun API key (recommended for security)"
    @echo "  OA_MAILGUN_EMAIL          Mailgun email"
    @echo "  OA_MAILGUN_SENDER         Mailgun sender name"
    @echo ""
    @echo "Examples:"
    @echo "  Development: OA_DATABASE_DRIVER=sqlite OA_PORT=8080 ./server"
    @echo "  Production:  OA_DATABASE_DRIVER=mysql OA_PASSWORD=secret ./server"

# All-in-one development setup
dev-setup: install-deps init-db build
    @echo "Development setup complete!"
    @echo "Run 'just run-dev' to start the server"

# All-in-one production build
prod-build: clean build-prod docker-build
    @echo "Production build complete!"
    @echo "Run 'just docker-run' to test the container"