#!/usr/bin/env nu
# sync-tracking.nu - Synchronize tracking data from all projects to central database
# Follows NuShell 0.108+ guidelines with explicit types and Rule 17 string interpolation

def main [
    --projects-dir: string = "/Users/Akasha"  # No bool type annotation (Rule!)
    --verbose = false
    --dry-run = false
]: void {
    if $verbose {
        print "🔄 Starting tracking sync..."
        print $"📁 Scanning projects in: [$projects_dir]"
    }

    # Rule 3: Early validation
    if not ($projects_dir | path exists) {
        print $"❌ Error: Projects directory [$projects_dir] not found"
        return
    }

    # Find all .coder directories
    let coder_projects = (
        ls $projects_dir --all --recursive
        | where type == "dir"
        | where name == ".coder"
        | get parent_path
    )

    if ($coder_projects | length) == 0 {
        print "⚠️  No .coder directories found"
        return
    }

    print $"✅ Found (($coder_projects | length)) projects to sync"

    # Rule 1: Single purpose - process each project
    let total_synced = (
        $coder_projects
        | each { |project_path|
            sync-project $project_path $verbose $dry_run
        }
        | math sum
    )

    print $"
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✅ Sync Complete
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📊 Total entries synced: ($total_synced)
⏱️  Timestamp: (date now | format date '%Y-%m-%d %H:%M:%S UTC')
"
}

# Rule 1: Single purpose - only syncs one project
def sync-project [project-path: string, verbose: bool, dry-run: bool]: int {
    # Rule 3: Early return validation
    if not ($project-path | path exists) {
        if $verbose {
            print $"⚠️  Skipping [$project-path] - not found"
        }
        return 0
    }

    # Rule 17: ($expr) for expressions, [$var] for variables
    let changes-file = $"[$project-path]/.coder/changes.md"
    let todo-file = $"[$project-path]/.coder/todo.md"

    let mut synced = 0

    # Process changes.md
    if ($changes-file | path exists) {
        if $verbose {
            print $"📝 Syncing changes from [$changes-file]"
        }

        if not $dry_run {
            let result = (
                curl --silent --request POST "http://localhost:3000/api/v1/tracking/sync" \
                  --header "Content-Type: application/json" \
                  --data {
                      project: $project-path
                      type: "changes"
                      file: $changes-file
                  } | to json
            )

            if ($result | has "error") {
                print $"❌ Error syncing changes: ($result.error)"
            } else {
                $synced = ($synced + 1)
            }
        }
    }

    # Process todo.md
    if ($todo-file | path exists) {
        if $verbose {
            print $"📝 Syncing todos from [$todo-file]"
        }

        if not $dry_run {
            let result = (
                curl --silent --request POST "http://localhost:3000/api/v1/tracking/sync" \
                  --header "Content-Type: application/json" \
                  --data {
                      project: $project-path
                      type: "todos"
                      file: $todo-file
                  } | to json
            )

            if ($result | has "error") {
                print $"❌ Error syncing todos: ($result.error)"
            } else {
                $synced = ($synced + 1)
            }
        }
    }

    if $verbose {
        print $"✅ Synced [$project-path]"
    }

    $synced
}
