;; SatGuard Risk History
(define-constant err-owner (err u340))
(define-constant err-nf (err u341))
(define-data-var admin principal tx-sender)
(define-map snapshots {pid: uint,idx: uint} {score: uint,tier: uint,tf: uint,bh: uint})
(define-map snap-counts {pid: uint} {count: uint})
(define-read-only (get-snapshot (pid uint) (idx uint)) (map-get? snapshots {pid: pid,idx: idx}))
(define-read-only (get-snapshot-count (pid uint)) (default-to {count: u0} (map-get? snap-counts {pid: pid})))
(define-public (record-snapshot (pid uint))
(let ((pool (unwrap! (contract-call? .insurance-pool get-pool pid) err-nf))
(rs (unwrap! (contract-call? .sg-risk-calculator calc-risk-score pid) err-nf))
(sc (get count (get-snapshot-count pid)))
(nid (+ sc u1)))
(map-set snapshots {pid: pid,idx: nid} {score: (get score rs),tier: (get level rs),tf: (get tf pool),bh: block-height})
(map-set snap-counts {pid: pid} {count: nid})
(print {e: "snapshot-recorded",pid: pid,idx: nid})
(ok nid)))
(define-read-only (get-latest-snapshot (pid uint))
(let ((sc (get count (get-snapshot-count pid))))
(if (> sc u0) (map-get? snapshots {pid: pid,idx: sc}) none)))