;; use the SIP090 interface
(impl-trait .nft-trait.nft-trait)

(define-non-fungible-token special-ingredient uint)

;; Constants
(define-constant PREMINT-ADDRESS 'SP3HCNR789SGMN18Y4SYBXBP38NB1BPRFVA9P010M)
(define-constant ERR-ALL-MINTED u101)
(define-constant CONTRACT-OWNER tx-sender)
(define-constant ERR-NOT-AUTHORIZED u403)
(define-constant MINT-LIMIT u400)
(define-constant ERR-ALREADY-CLAIMED u501)
(define-constant OK-CLAIMED u200)

;; Store the last issues token ID
(define-data-var last-id uint u0)
(define-data-var token-uri (string-ascii 256) "")

(define-public (mint)
    (let (
        (next-id (+ u1 (var-get last-id)))  
        (count (var-get last-id))
        (canClaim (unwrap-panic (contract-call? .punk-donuts get-si-claimable)))
        )
        (asserts! (< count MINT-LIMIT) (err ERR-ALL-MINTED))
        (asserts! (is-eq canClaim true) (err ERR-NOT-AUTHORIZED))
        (try! (nft-mint? special-ingredient next-id tx-sender))
        (var-set last-id next-id)
        (unwrap! (contract-call? .punk-donuts set-si-claimed) (ok OK-CLAIMED))
        (ok next-id)
    )
)

;; SIP009: Transfer token to a specified principal
(define-public (transfer (token-id uint) (sender principal) (recipient principal))
  (if (and
        (is-eq tx-sender sender))
      (match (nft-transfer? special-ingredient token-id sender recipient)
        success (ok success)
        error (err error))
      (err u500)))

;; SIP009: Get the owner of the specified token ID
(define-read-only (get-owner (token-id uint))
  (ok (nft-get-owner? special-ingredient token-id)))

;; SIP009: Get the last token ID
(define-read-only (get-last-token-id)
  (ok (var-get last-id)))

;; SIP009: Get the token URI. You can set it to any other URI
(define-read-only (get-token-uri (id uint))
  (ok (some (var-get token-uri)))
)

(define-read-only (stx-balance)
  (stx-get-balance (as-contract tx-sender))
)

(define-read-only (stx-balance-of (address principal))
  (stx-get-balance address)
)

;; Transfers stx from contract to contract owner
(define-public (transfer-stx (address principal) (amount uint))
  (if (is-eq tx-sender PREMINT-ADDRESS)
    (as-contract (stx-transfer? amount (as-contract tx-sender) address))
    (err ERR-NOT-AUTHORIZED)
  )
)

;; Internal - premint
(define-private (pre-mint (new-owner principal))
  (let (
        (next-id (+ u1 (var-get last-id)))  
        (count (var-get last-id))
      )
      (asserts! (< count MINT-LIMIT) (err ERR-ALL-MINTED))
      (asserts! (is-eq tx-sender CONTRACT-OWNER) (err ERR-NOT-AUTHORIZED))
            (try! (nft-mint? special-ingredient next-id new-owner))
            (var-set last-id next-id)
            (ok next-id)
          )
        )

;; initialize
(var-set token-uri "ipfs://Qmcodmv7vVTqLPRRjwvNgrAnkjzwNquN6DdGCgXADFj1we")

;; premint 100
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
(pre-mint PREMINT-ADDRESS)
