
(define-constant ERR-NOT-AUTHORIZED u401)
(define-constant ERR-NOT-ACTIVE u501)
(define-constant ERR-BID-NOT-HIGH-ENOUGH u100)
(define-constant ERR-ITEM-NOT-FOR-SALE u101)
(define-constant ERR-ITEM-PRICE-TOO-LOW u102)
(define-constant CONTRACT-OWNER tx-sender)

(define-data-var commission uint u250)
(define-data-var active bool true)

(define-map bids { namespace: (buff 20), name: (buff 48) } { buyer: principal, offer: uint })

(define-public (bid (namespace (buff 20))
                            (domain (buff 48))
                            (amount uint))
    (match (stx-transfer? amount tx-sender (as-contract tx-sender))
      success (begin
      (asserts! (is-eq (var-get active) true) (err ERR-NOT-ACTIVE))
     (map-set bids { namespace: namespace, name: domain } { buyer: tx-sender, offer: amount })
     (ok true)
    )
    error (err error)
    )
)

(define-public (get-bid (namespace (buff 20))
                            (domain (buff 48)))
     (ok (map-get? bids { namespace: namespace, name: domain } ))
)

(define-public (withdraw-bid (namespace (buff 20))
                            (domain (buff 48)))
    (let (
        (name (map-get? bids { namespace: namespace, name: domain } ))
        (offer (unwrap-panic (get offer name)))
        (buyer (unwrap-panic (get buyer name)))
    )
    (begin
    (asserts! (is-eq tx-sender buyer) (err ERR-NOT-AUTHORIZED))
    (try! (as-contract (stx-transfer? offer (as-contract tx-sender) buyer)))
     (map-delete bids { namespace: namespace, name: domain } )
     (ok true)
    )
)
)

(define-public (accept-bid (namespace (buff 20))
                            (domain (buff 48))
                            (zonefile-hash (optional (buff 20))))
    (let (
        (name (map-get? bids { namespace: namespace, name: domain } ))
        (offer (unwrap-panic (get offer name)))
        (commiss (/ (* offer (var-get commission)) u10000))
        (buyer (unwrap-panic (get buyer name)))
        ;; (owner (contract-call? .bns name-resolve namespace name))
        (owner (get owner {lease-ending-at: (some u280129), lease-started-at: u17329, owner: 'SP3BZ5HP83M87YG0FPX5YZRH6MYWHTFGP4AAYV1ZY, zonefile-hash: 0xb472a266d0bd89c13706a4132ccfb16f7c3b9fcb}))
    )
    (begin
    (print commiss)
    (print name)
    (print buyer)
    (asserts! (is-eq (var-get active) true) (err ERR-NOT-ACTIVE))
    (asserts! (is-eq owner tx-sender) (err ERR-NOT-AUTHORIZED))
    (try! (as-contract (stx-transfer? commiss (as-contract tx-sender) CONTRACT-OWNER)))
    (try! (as-contract (stx-transfer? (- offer commiss) (as-contract tx-sender) owner)))
    (try! (contract-call? 'SP000000000000000000002Q6VF78.bns name-transfer namespace domain buyer none))
     (map-delete bids { namespace: namespace, name: domain } )
    )
    (ok true)
)
)

(define-public (admin-unbid (namespace (buff 20))
                            (domain (buff 48)))
    (let (
        (name (map-get? bids { namespace: namespace, name: domain } ))
        (offer (unwrap-panic (get offer name)))
        (buyer (unwrap-panic (get buyer name)))
    )
    (begin
    (asserts! (is-eq tx-sender CONTRACT-OWNER) (err ERR-NOT-AUTHORIZED))
    (try! (as-contract (stx-transfer? offer (as-contract tx-sender) buyer)))
     (map-delete bids { namespace: namespace, name: domain } )
     (ok true)
    )
)
)

(define-public (set-commisssion (value uint))
  (if (is-eq tx-sender CONTRACT-OWNER)
    (ok (var-set commission value))
    (err ERR-NOT-AUTHORIZED)
  )
)

(define-public (set-active (value bool))
  (if (is-eq tx-sender CONTRACT-OWNER)
    (ok (var-set active value))
    (err ERR-NOT-AUTHORIZED)
  )
)