# Image watch — periodic freshness monitor for role image snapshots.

use ./state.nu *
use ./create.nu *

# Poll all role image states every N minutes and report stale snapshots.
export def image-watch [
    --interval: int = 60
    --auto-build
    --notify-only
    --provider: string = ""
    --infra: string = ""
] {
    print $"Image watch started (interval: ($interval)m, auto-build: ($auto_build))"
    print "Press Ctrl-C to stop."
    print ""

    loop {
        let states = (image-state-list --provider $provider)
        let now_str = ((date now) | format date "%Y-%m-%dT%H:%M:%SZ")
        print $"[($now_str)] Checking ($states | length) role image(s)..."

        for state in $states {
            let fresh = (do {
                image-state-is-fresh $state.provider $state.role
            } catch { false })

            if $state.snapshot_id == "SNAPSHOT_PENDING" {
                print $"[PENDING]  ($state.provider)/($state.role) — no snapshot built"
            } else if not $fresh {
                let built = ($state.built_at? | default "unknown")
                print $"[STALE]    ($state.provider)/($state.role) — last built: ($built)  snapshot: ($state.snapshot_id)"
                if $auto_build and not $notify_only {
                    print $"  → auto-building ($state.role)..."
                    do {
                        image-create $state.role --infra $infra
                    } catch { |e|
                        print $"  ✗ build failed: ($e.msg)"
                    }
                }
            } else {
                print $"[OK]       ($state.provider)/($state.role) — snapshot: ($state.snapshot_id)"
            }
        }

        print ""
        sleep ($interval * 60sec)
    }
}
