(define-constant E1 (err u940))
(define-constant E2 (err u941))
(define-data-var ac uint u0)
(define-map alerts uint
{user: principal, mid: uint, alert-type: uint, threshold: uint, active: bool})
(define-map user-alert-count principal uint)
(define-public (create-alert (mid uint) (alert-type uint) (threshold uint))
(let ((id (var-get ac))
(uc (default-to u0 (map-get? user-alert-count tx-sender))))
(asserts! (< uc u10) E1)
(var-set ac (+ id u1))
(map-set user-alert-count tx-sender (+ uc u1))
(ok (map-set alerts id
{user: tx-sender, mid: mid, alert-type: alert-type,
threshold: threshold, active: true}))))
(define-public (cancel-alert (aid uint))
(let ((a (unwrap! (map-get? alerts aid) E2)))
(asserts! (is-eq tx-sender (get user a)) E1)
(ok (map-set alerts aid (merge a {active: false})))))
(define-read-only (get-alert (aid uint)) (map-get? alerts aid))
(define-read-only (get-user-alerts (u principal))
(default-to u0 (map-get? user-alert-count u)))
(define-read-only (get-alert-count) (var-get ac))