#!/usr/bin/env python3
# This is free software for the public good of a permacomputer hosted at
# permacomputer.com, an always-on computer by the people, for the people.
# One which is durable, easy to repair, & distributed like tap water
# for machine learning intelligence.
#
# The permacomputer is community-owned infrastructure optimized around
# four values:
#
#   TRUTH      First principles, math & science, open source code freely distributed
#   FREEDOM    Voluntary partnerships, freedom from tyranny & corporate control
#   HARMONY    Minimal waste, self-renewing systems with diverse thriving connections
#   LOVE       Be yourself without hurting others, cooperation through natural law
#
# This software contributes to that vision by enabling code execution across 42+ programming languages through a unified interface, accessible to all.
# Code is seeds to sprout on any abandoned technology.

"""
File Operations example for unsandbox Python SDK - Synchronous Version

This example demonstrates reading and writing files in sandboxed environments.
Shows temporary file creation and manipulation within the sandbox.

To run:
    export UNSANDBOX_PUBLIC_KEY="your-public-key"
    export UNSANDBOX_SECRET_KEY="your-secret-key"
    python3 file_operations.py

Expected output:
    File created at: /tmp/example.txt
    File contents:
    Line 1: Hello from the sandbox
    Line 2: This is temporary storage
    Line 3: File operations work!
    Total lines written: 3
"""

import sys
import os

# Add the SDK path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))

try:
    from un import execute_code, CredentialsError
except ImportError as e:
    print(f"Missing dependency: {e}")
    print("Install with: pip install requests")
    sys.exit(0)  # Exit gracefully for CI


def main():
    """Execute file operations code in sandbox."""

    # The code to execute - demonstrates file I/O
    code = """
import os
import tempfile

# Create temporary file
temp_file = "/tmp/example.txt"

try:
    # Write to file
    with open(temp_file, "w") as f:
        f.write("Line 1: Hello from the sandbox\\n")
        f.write("Line 2: This is temporary storage\\n")
        f.write("Line 3: File operations work!\\n")

    print(f"File created at: {temp_file}")

    # Check if file exists
    if os.path.exists(temp_file):
        print(f"File exists: {os.path.isfile(temp_file)}")

        # Get file size
        file_size = os.path.getsize(temp_file)
        print(f"File size: {file_size} bytes")

    # Read from file
    print("File contents:")
    with open(temp_file, "r") as f:
        lines = f.readlines()
        for line in lines:
            print(line.rstrip())

    print(f"Total lines written: {len(lines)}")

except IOError as e:
    print(f"File operation error: {e}")
except Exception as e:
    print(f"Error: {e}")
"""

    try:
        # Resolve credentials from environment
        public_key = os.environ.get("UNSANDBOX_PUBLIC_KEY")
        secret_key = os.environ.get("UNSANDBOX_SECRET_KEY")

        if not public_key or not secret_key:
            print("Skipping: UNSANDBOX_PUBLIC_KEY and UNSANDBOX_SECRET_KEY environment variables required")
            print("To run: export UNSANDBOX_PUBLIC_KEY=your-key UNSANDBOX_SECRET_KEY=your-key")
            sys.exit(0)  # Exit gracefully for CI

        # Execute the code
        print("Executing file operations in sandbox...")
        result = execute_code("python", code, public_key, secret_key)

        # Check for errors
        if result.get("status") == "completed":
            print("\n=== STDOUT ===")
            print(result.get("stdout", ""))
            if result.get("stderr"):
                print("\n=== STDERR ===")
                print(result.get("stderr", ""))
        else:
            print(f"Execution failed with status: {result.get('status')}")
            print(f"Error: {result.get('error', 'Unknown error')}")
            sys.exit(1)

    except CredentialsError as e:
        print(f"Credentials error: {e}")
        sys.exit(1)
    except Exception as e:
        print(f"Error: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)


if __name__ == "__main__":
    main()
