;; calligraphy-market -- Islamic calligraphy art marketplace
(define-constant CONTRACT-OWNER tx-sender)
(define-constant ERR-AUTH (err u401))
(define-constant ERR-NONE (err u404))
(define-data-var piece-count uint u0)
(define-map artworks uint { artist: principal, title: (string-utf8 100), style: (string-ascii 20), price: uint, sold: bool })
(define-map artist-earnings principal uint)
(define-public (list-artwork (title (string-utf8 100)) (style (string-ascii 20)) (price uint))
(let ((id (+ (var-get piece-count) u1)))
(var-set piece-count id)
(map-set artworks id { artist: tx-sender, title: title, style: style, price: price, sold: false })
(ok id)))
(define-public (purchase (id uint))
(let ((a (unwrap! (map-get? artworks id) ERR-NONE)))
(asserts! (not (get sold a)) ERR-AUTH)
(map-set artworks id (merge a { sold: true }))
(let ((prev (default-to u0 (map-get? artist-earnings (get artist a)))))
(map-set artist-earnings (get artist a) (+ prev (get price a)))
(ok true))))
(define-read-only (get-artwork (id uint)) (map-get? artworks id))
(define-read-only (get-piece-count) (var-get piece-count))