(impl-trait 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.nft-trait.nft-trait)
(define-non-fungible-token my-nft uint)

(define-data-var administrator principal 'ST1N0A4BJYXZE2E4ZWM1MHZ67X3BG53RD51DCCH9D)
(define-data-var mint-price uint u10000000)
(define-data-var mint-counter uint u0)

(define-constant not-allowed (err u10))
(define-constant failed-to-mint-err (err u18))
(define-constant failed-to-stx-transfer (err u22))
(define-constant cant-pay-mint-price (err u25))
(define-constant editions-error (err u26))
(define-constant err-not-token-owner (err u102))

(define-map nft-edition-counter {nft-index: uint} {edition-counter: uint})

(define-read-only (get-last-token-id)
  (ok (var-get mint-counter))
)

(define-read-only (get-owner (token-id uint))
  (ok (nft-get-owner? my-nft token-id))
)

(define-read-only (get-token-uri (token-id uint))
  (ok none)
)

(define-public (transfer (token-id uint) (sender principal) (recipient principal))
  (begin
    (asserts! (is-eq tx-sender sender) err-not-token-owner)
    (nft-transfer? my-nft token-id sender recipient)  
  )
)

;; the contract administrator can change the mint price
(define-public (update-mint-price (new-mint-price uint))
    (begin
        (asserts! (is-eq (var-get administrator) tx-sender) not-allowed)
        (var-set mint-price new-mint-price)
        (ok true)
    )
)

;; max-editions: maximum number of editions allowed for this asset
(define-public (mint-token (maxEditions uint))
  (let (
        (mintCounter (var-get mint-counter))
      ) 
      (asserts! (> maxEditions u0) editions-error)
      (asserts! (> (stx-get-balance tx-sender) (var-get mint-price)) cant-pay-mint-price)
      ;; Note editions are 1 based and <= maxEditions - the one minted here is #1
      (map-insert nft-edition-counter {nft-index: mintCounter} {edition-counter: u2})
      ;; finally - mint the NFT and step the counter
      (if (is-eq tx-sender (var-get administrator))
        ;; true
        (print "mint-token : tx-sender is contract - skipping mint price")
        ;; false
        (begin
          (unwrap! (stx-transfer? (var-get mint-price) tx-sender (var-get administrator)) failed-to-stx-transfer)
          (print "mint-token : tx-sender paid mint price")
        )
      )
      (unwrap! (nft-mint? my-nft mintCounter tx-sender) failed-to-mint-err)
      (print {evt: "mint-token", nftIndex: mintCounter, owner: tx-sender, amount: (var-get mint-price)})
      (var-set mint-counter (+ mintCounter u1))
      (ok mintCounter)))