#!/usr/bin/env nu

use lib_provisioning/cmd/env.nu *
use lib_provisioning/cmd/lib.nu *

def main [] {
    let defs = load_defs
    let work_path = $env.PROVISIONING_WK_ENV_PATH

    _create_directories $work_path
    _setup_agent_config $work_path
}

# Create required directory structure for Woodpecker
def _create_directories [work_path: string] {
    let dirs = [
        ($work_path + "/.woodpecker"),
        ($work_path + "/data"),
        ($work_path + "/config"),
        ($work_path + "/log"),
        ($work_path + "/cache"),
        ($work_path + "/secrets"),
        ($work_path + "/ssh"),
        ($work_path + "/certs"),
    ]

    for dir in $dirs {
        if not ($dir | path exists) {
            mkdir -p $dir
            print $"Created directory: ($dir)"
        }
    }
}

# Setup agent configuration if agent is enabled
def _setup_agent_config [work_path: string] {
    let agent_config = ($work_path + "/config/agent.env")

    if not ($agent_config | path exists) {
        let config_content = @"
# Woodpecker Agent Configuration
# Uncomment and configure when using distributed agents

# WOODPECKER_SERVER=http://woodpecker:9000
# WOODPECKER_AGENT_SECRET=your-agent-secret-here
# WOODPECKER_MAX_PROCS=2
# WOODPECKER_BACKEND=docker
# WOODPECKER_FILTER_LABELS=agent:default
"@

        $config_content | save -f $agent_config
        print $"Created agent configuration: ($agent_config)"
    }
}
