# Taskserv Dependency Validator
# Validates taskserv dependencies, conflicts, and requirements

# REMOVED: use lib_provisioning * - causes circular import
use utils.nu *
use ../lib_provisioning/config/accessor.nu *
use ../lib_provisioning/utils/nickel_processor.nu [ncl-eval]

# Validate taskserv dependencies from Nickel definition
export def validate-dependencies [
    taskserv_name: string
    settings: record
    --verbose (-v)
] {
    let taskservs_path = (get-taskservs-path)
    let taskserv_schema_path = ($taskservs_path | path join $taskserv_name "nickel")

    # Check if taskserv has dependencies.ncl
    let deps_file = ($taskserv_schema_path | path join "dependencies.ncl")

    if not ($deps_file | path exists) {
        return {
            valid: true
            taskserv: $taskserv_name
            has_dependencies: false
            warnings: []
            errors: []
        }
    }

    if $verbose {
        _print $"Validating dependencies for (_ansi yellow_bold)($taskserv_name)(_ansi reset)..."
    }

    # Run Nickel to extract dependency information
    let result = (try {
        ncl-eval $deps_file []
    } catch {
        return {
            valid: false
            taskserv: $taskserv_name
            has_dependencies: true
            warnings: []
            errors: ["Failed to parse dependencies.ncl"]
        }
    })

    # Extract dependency information
    let deps = ($result | get -o _dependencies)
    if ($deps | is-empty) {
        return {
            valid: true
            taskserv: $taskserv_name
            has_dependencies: false
            warnings: ["dependencies.ncl exists but no _dependencies defined"]
            errors: []
        }
    }

    let requires = ($deps | get -o requires | default [])
    let optional = ($deps | get -o optional | default [])
    let conflicts = ($deps | get -o conflicts | default [])

    mut warnings = []
    mut errors = []

    # Validate required dependencies
    for req in $requires {
        let req_path = ($taskservs_path | path join $req)
        if not ($req_path | path exists) {
            $errors = ($errors | append $"Required dependency not found: ($req)")
        } else if $verbose {
            _print $"  ✓ Required: ($req)"
        }
    }

    # Check optional dependencies
    for opt in $optional {
        let opt_path = ($taskservs_path | path join $opt)
        if not ($opt_path | path exists) {
            $warnings = ($warnings | append $"Optional dependency not available: ($opt)")
        } else if $verbose {
            _print $"  ℹ Optional: ($opt)"
        }
    }

    # Validate conflicts
    for conf in $conflicts {
        let conf_path = ($taskservs_path | path join $conf)
        if ($conf_path | path exists) {
            $warnings = ($warnings | append $"Conflicting taskserv installed: ($conf)")
        } else if $verbose {
            _print $"  ✓ No conflict: ($conf)"
        }
    }

    # Validate resource requirements
    let resource_req = ($deps | get -o resource_requirements)
    if ($resource_req | is-not-empty) {
        let min_memory = ($resource_req | get -o min_memory | default 0)
        let min_cores = ($resource_req | get -o min_cores | default 0)
        let min_disk = ($resource_req | get -o min_disk | default 0)

        if $verbose {
            _print $"  Resources: CPU($min_cores) MEM($min_memory)GB DISK($min_disk)GB"
        }
    }

    # Check health check configuration
    let health_check = ($deps | get -o health_check)
    if ($health_check | is-not-empty) {
        let endpoint = ($health_check | get -o endpoint | default "")
        let timeout = ($health_check | get -o timeout | default 30)
        let interval = ($health_check | get -o interval | default 10)

        if $verbose {
            let health_msg = $"  Health: ($endpoint) (timeout=($timeout|into string) interval=($interval|into string))"
            _print $health_msg
        }
    }

    {
        valid: ($errors | is-empty)
        taskserv: $taskserv_name
        has_dependencies: true
        warnings: $warnings
        errors: $errors
        requires: $requires
        optional: $optional
        conflicts: $conflicts
    }
}
