#!/usr/bin/env nu

# Environment Validation Script for Rustelo Project
# Validates .env loading, required variables, and development prerequisites

use ../content/lib/env.nu *

def main [
    --strict                     # Fail on warnings (not just errors)
    --path: string = "."        # Path to project root
] {
    print "🔍 Validating development environment..."

    mut issues = []

    # Validate .env file and loading
    let issues = ($issues | append (check_env_file $path))

    # Validate required environment variables
    let issues = ($issues | append (check_required_env_vars))

    # Validate development tools
    let issues = ($issues | append (check_dev_tools))

    # Validate project structure
    let issues = ($issues | append (check_project_structure $path))

    # Display results
    if ($issues | is-empty) {
        print "✅ Environment validation passed!"
        return 0
    } else {
        print "❌ Environment validation issues found:"
        for $issue in $issues {
            print $"  ($issue.severity): ($issue.message)"
            if ($issue.fix?) {
                print $"     Fix: ($issue.fix)"
            }
        }

        let errors = ($issues | where severity == "ERROR")
        if not ($errors | is-empty) {
            return 1
        } else if $strict {
            return 1
        } else {
            return 0
        }
    }
}

# Check .env file exists and can be loaded
def check_env_file [path: string] {
    mut issues = []

    let env_file = $"($path)/.env"

    if not ($env_file | path exists) {
        $issues = ($issues | append {
            severity: "ERROR"
            message: ".env file not found"
            fix: "Create .env file from .env.example"
        })
        return $issues
    }

    # Try to load .env file
    let load_result = try {
        load_env_from_file
        "success"
    } catch {
        "failed"
    }

    if $load_result == "success" {
        print $"(ansi green)✅ .env file loaded successfully(ansi reset)"
    } else {
        $issues = ($issues | append {
            severity: "ERROR"
            message: ".env file exists but cannot be loaded"
            fix: "Check .env file syntax"
        })
    }

    $issues
}

# Check required environment variables
def check_required_env_vars [] {
    mut issues = []

    let required_vars = [
        "SITE_CONTENT_PATH"
        "SITE_SERVER_CONTENT_URL"
    ]

    for $var in $required_vars {
        if not ($var in $env) {
            $issues = ($issues | append {
                severity: "WARNING"
                message: $"Required environment variable ($var) not set"
                fix: $"Set ($var) in .env file"
            })
        } else {
            print $"(ansi green)✅ ($var) = ($env | get $var)(ansi reset)"
        }
    }

    $issues
}

# Check development tools availability
def check_dev_tools [] {
    mut issues = []

    let required_tools = [
        {name: "cargo", command: "cargo", required: true}
        {name: "npm", command: "npm", required: true}
        {name: "nu", command: "nu", required: true}
        {name: "just", command: "just", required: true}
        {name: "rg", command: "rg", required: false}
    ]

    for $tool in $required_tools {
        if (which $tool.command | is-empty) {
            let severity = if $tool.required { "ERROR" } else { "WARNING" }
            $issues = ($issues | append {
                severity: $severity
                message: $"Tool ($tool.name) not found in PATH"
                fix: $"Install ($tool.name)"
            })
        } else {
            print $"(ansi green)✅ ($tool.name) available(ansi reset)"
        }
    }

    $issues
}

# Check project structure
def check_project_structure [path: string] {
    mut issues = []

    let required_dirs = [
        "crates"
        "scripts"
        "just"
        "site/public"
    ]

    for $dir in $required_dirs {
        let dir_path = $"($path)/($dir)"
        if not ($dir_path | path exists) {
            $issues = ($issues | append {
                severity: "ERROR"
                message: $"Required directory ($dir) not found"
                fix: $"Ensure project structure is correct"
            })
        } else {
            print $"(ansi green)✅ Directory ($dir) exists(ansi reset)"
        }
    }

    # Check content path
    let content_path = get_env_var "SITE_CONTENT_PATH" "site/content"
    let full_content_path = $"($path)/($content_path)"

    if not ($full_content_path | path exists) {
        $issues = ($issues | append {
            severity: "WARNING"
            message: $"Content directory ($content_path) not found"
            fix: $"Create content directory or update SITE_CONTENT_PATH"
        })
    } else {
        print $"(ansi green)✅ Content directory ($content_path) exists(ansi reset)"
    }

    $issues
}
