;; using the SIP009 interface (testnet)
;; trait configured and deployed from ./settings/Devnet.toml
(impl-trait 'SP1VG2RCJ5E8R79SG5G7GPZGM30CZQRD8QDJNAZTS.nft-trait.nft-trait)

;; declare a new NFT
(define-non-fungible-token SoCoStacksNFT_Groceries_Usr_USSC007 uint)

;; store the last issued token ID
(define-data-var last-id uint u0)

;; mint a new NFT
(define-public (claim)
  (mint tx-sender))

;; SIP009: Transfer token to a specified principal
(define-public (transfer (token-id uint) (sender principal) (recipient principal))
  (begin
     (asserts! (is-eq tx-sender sender) (err u403))
     ;; Make sure to replace SoCoStacksNFT_Groceries_Usr_USSC007
     (nft-transfer? SoCoStacksNFT_Groceries_Usr_USSC007 token-id sender recipient)))

(define-public (transfer-memo (token-id uint) (sender principal) (recipient principal) (memo (buff 34)))
  (begin 
    (try! (transfer token-id sender recipient))
    (print memo)
    (ok true)))

;; SIP009: Get the owner of the specified token ID
(define-read-only (get-owner (token-id uint))
  ;; Make sure to replace NFT-NAME
  (ok (nft-get-owner? SoCoStacksNFT_Groceries_Usr_USSC007 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 (token-id uint))
  (ok (some "https://ipfs.io/ipfs/bafybeicvnaqeyxqkqf7vuih3222hk6pcd5poo2qew7gsni52curv7ccpce/soco1.json")))

;; Internal - Mint new NFT
(define-private (mint (new-owner principal))
    (let ((next-id (+ u1 (var-get last-id))))
      (var-set last-id next-id)
      ;; You can replace NFT-FACTORY with another name if you'd like
      (nft-mint? SoCoStacksNFT_Groceries_Usr_USSC007 next-id new-owner)))