//! Script templates for generated projects

/// Generate project-specific setup script
#[allow(dead_code)] // Template for future use
pub fn setup_script(project_name: &str) -> String {
    format!(
        r#"#!/usr/bin/env bash
#
# {} Project Setup
#
# Installs all required tools and dependencies for this robot project.
# Checks before installing to avoid overwriting existing installations.
#
# Usage:
#   mecha10 setup                         # Interactive setup (recommended)
#   ./scripts/setup.sh                    # Direct script usage
#   mecha10 setup --non-interactive       # Auto-yes to all prompts
#   mecha10 setup --check-only            # Only check installed tools
#

set -o pipefail

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'

# Options
NON_INTERACTIVE=false
CHECK_ONLY=false

# Parse arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --non-interactive)
            NON_INTERACTIVE=true
            shift
            ;;
        --check-only)
            CHECK_ONLY=true
            shift
            ;;
        *)
            echo "Unknown option: $1"
            echo "Usage: $0 [--non-interactive] [--check-only]"
            exit 1
            ;;
    esac
done

# Logging functions
log_header() {{ echo -e "\n${{CYAN}}===${{NC}} $1 ${{CYAN}}===${{NC}}"; }}
log_info() {{ echo -e "${{BLUE}}ℹ${{NC}} $1"; }}
log_success() {{ echo -e "${{GREEN}}✓${{NC}} $1"; }}
log_warning() {{ echo -e "${{YELLOW}}⚠${{NC}} $1"; }}
log_error() {{ echo -e "${{RED}}✗${{NC}} $1" >&2; }}

# Prompt for confirmation
confirm() {{
    if [ "$NON_INTERACTIVE" = true ] || [ "$CHECK_ONLY" = true ]; then
        return 0
    fi

    local prompt="$1"
    local response

    while true; do
        read -p "$(echo -e "${{YELLOW}}?${{NC}} $prompt (y/n): ")" response
        case "$response" in
            [Yy]* ) return 0;;
            [Nn]* ) return 1;;
            * ) echo "Please answer y or n.";;
        esac
    done
}}

# OS detection
detect_os() {{
    if [[ "$OSTYPE" == "darwin"* ]]; then
        echo "macos"
    elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
        echo "linux"
    else
        echo "unknown"
    fi
}}

OS=$(detect_os)

# Check functions
check_rust() {{
    if command -v rustc >/dev/null 2>&1; then
        local version=$(rustc --version)
        log_success "Rust installed: $version"

        # Check for required components
        if rustup component list | grep -q 'rustfmt.*installed'; then
            log_success "  rustfmt installed"
        else
            log_warning "  rustfmt not installed"
            return 1
        fi

        if rustup component list | grep -q 'clippy.*installed'; then
            log_success "  clippy installed"
        else
            log_warning "  clippy not installed"
            return 1
        fi

        return 0
    else
        log_warning "Rust not installed"
        return 1
    fi
}}

check_mecha10() {{
    if command -v mecha10 >/dev/null 2>&1; then
        local version=$(mecha10 --version 2>&1 || echo "unknown")
        log_success "mecha10 CLI installed: $version"
        return 0
    else
        log_warning "mecha10 CLI not installed"
        return 1
    fi
}}

# Install functions
install_rust() {{
    log_info "Installing Rust..."
    if curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y; then
        # Source cargo env
        if [ -f "$HOME/.cargo/env" ]; then
            source "$HOME/.cargo/env"
        fi

        # Install components
        if rustup component add rustfmt clippy; then
            log_success "Rust installed successfully"
            return 0
        else
            log_error "Failed to install Rust components"
            return 1
        fi
    else
        log_error "Failed to install Rust"
        return 1
    fi
}}

install_mecha10() {{
    log_info "Installing mecha10 CLI..."

    if command -v cargo >/dev/null 2>&1; then
        if cargo install mecha10-cli; then
            log_success "mecha10 CLI installed successfully"
            return 0
        else
            log_error "Failed to install mecha10 CLI"
            return 1
        fi
    else
        log_error "Rust/Cargo required to install mecha10 CLI"
        return 1
    fi
}}

# Main setup flow
main() {{
    log_header "{} Project Setup"

    log_info "Detected OS: $OS"

    # Check all tools
    log_header "Checking Required Tools"

    RUST_OK=false
    MECHA10_OK=false

    check_rust && RUST_OK=true
    check_mecha10 && MECHA10_OK=true

    if [ "$CHECK_ONLY" = true ]; then
        log_header "Check Complete"

        if [ "$RUST_OK" = true ] && [ "$MECHA10_OK" = true ]; then
            log_success "All required tools are installed!"
            exit 0
        else
            log_warning "Some tools are missing. Run without --check-only to install."
            exit 1
        fi
    fi

    # Install missing tools
    log_header "Installing Missing Tools"

    if [ "$RUST_OK" = false ]; then
        if confirm "Install Rust?"; then
            install_rust || log_error "Failed to install Rust"
        fi
    fi

    if [ "$MECHA10_OK" = false ]; then
        if confirm "Install mecha10 CLI?"; then
            install_mecha10 || log_warning "mecha10 CLI installation failed"
        fi
    fi

    # Build project
    log_header "Building Project"

    if [ "$RUST_OK" = true ] || command -v cargo >/dev/null 2>&1; then
        log_info "Building project (this may take a few minutes)..."
        if cargo build; then
            log_success "Project built successfully"
        else
            log_warning "Failed to build project"
        fi
    fi

    # Final verification
    log_header "Setup Complete!"

    echo ""
    log_success "{} project environment is ready!"
    echo ""
    log_info "Next steps:"
    echo "  1. Source your shell config to update PATH:"
    echo "     source ~/.bashrc  # or ~/.zshrc"
    echo ""
    echo "  2. Run code quality checks:"
    echo "     mecha10 lint"
    echo ""
    echo "  3. Start development mode:"
    echo "     mecha10 dev"
    echo ""
}}

# Run main
main
"#,
        project_name, project_name, project_name
    )
}
