#!/usr/bin/env herodo
//! Buildah Basic Builder Pattern Examples
//! Demonstrates fundamental builder pattern operations

// Example 1: Simple Alpine image with curl installed
let alpine_builder = bah("alpine-curl", "alpine:latest")
    .run("apk update")
    .run("apk add --no-cache curl wget")
    .write("Welcome to Alpine", "/tmp/welcome.txt");

alpine_builder.remove();

// Example 2: Ubuntu-based builder with git
let ubuntu_builder = bah("ubuntu-git", "ubuntu:22.04")
    .run("apt-get update")
    .run("apt-get install -y git curl");

ubuntu_builder.remove();

// Example 3: Node.js application builder
let app_code = `
const http = require('http');
const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello from Node.js\\n');
});
server.listen(3000);
`;

let node_builder = bah("node-app", "node:18-alpine")
    .run("npm install -g pm2")
    .run("mkdir -p /app")
    .write(app_code, "/app/server.js")
    .config(#{ "workingdir": "/app" })
    .entrypoint("node")
    .cmd("/app/server.js");

node_builder.remove();

// Example 4: Python Flask application
let flask_app = `
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello from Flask!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=False)
`;

let requirements = `
Flask==2.3.0
gunicorn==21.2.0
`;

let python_builder = bah("python-flask", "python:3.11-slim")
    .run("pip install --no-cache-dir Flask gunicorn")
    .run("mkdir -p /app")
    .write(flask_app, "/app/app.py")
    .write(requirements, "/app/requirements.txt")
    .config(#{
        "workingdir": "/app",
        "env": "FLASK_APP=app.py"
    })
    .entrypoint("gunicorn")
    .cmd("-w 4 -b 0.0.0.0:5000 app:app");

python_builder.remove();

// Example 5: Nginx web server with custom config
let nginx_conf = `
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    
    server_name _;
    root /usr/share/nginx/html;
    
    location / {
        try_files $uri $uri/ =404;
    }
    
    location /health {
        access_log off;
        return 200 "healthy\n";
        add_header Content-Type text/plain;
    }
}
`;

let index_html = `
<!DOCTYPE html>
<html>
<head>
    <title>Welcome</title>
    <style>
        body { font-family: Arial; margin: 40px; }
        h1 { color: #333; }
    </style>
</head>
<body>
    <h1>Welcome to Nginx</h1>
    <p>This is a custom Nginx instance.</p>
</body>
</html>
`;

let nginx_builder = bah("nginx-custom", "nginx:alpine")
    .write(nginx_conf, "/etc/nginx/conf.d/default.conf")
    .write(index_html, "/usr/share/nginx/html/index.html")
    .config(#{
        "label": "app=web-server",
        "label": "version=1.0"
    })
    .entrypoint("nginx")
    .cmd("-g 'daemon off;'");

nginx_builder.remove();

// Example 6: Reading content from container
let reader = bah("content-reader", "alpine:latest")
    .run("echo 'Configuration content' > /app/config.txt")
    .run("echo 'Database: localhost' >> /app/config.txt");

let config_content = reader.read("/app/config.txt");

reader.remove();
