
;; astro-nft
;; <add a description here>

;; constants
;;

;; data maps and vars
;;

;; private functions
;;

;; public functions
;;

;; use the SIP090 interface (testnet)
(impl-trait 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.nft-trait.nft-trait)
;; (impl-trait 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.nft-trait.nft-trait)


;; define a new NFT. Make sure to replace astro-nft
(define-non-fungible-token astro-nft uint)

;; Store the last issues token ID
(define-data-var last-id uint u0)
(define-data-var mint-price uint u0)
(define-data-var meta-base-url (string-ascii 40) "https://stan.rik.ai/api/meta/astro/")


;; Claim 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))
  (if (and
        (is-eq tx-sender sender))
        (match (nft-transfer? astro-nft 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? astro-nft 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://stan.rik.ai/api/trees/1")))
(define-read-only (get-token-uri (token-id uint))
  (ok (some (concat (var-get meta-base-url) (uint-to-string token-id) ) ))
)

(define-read-only (get-meta-base (token-id uint))
  (ok (some (var-get meta-base-url) )))


(define-read-only (echo-int (token-id uint))
  (ok token-id))


;; Internal - Mint new NFT
(define-private (mint (new-owner principal))
    (let ((next-id (+ u1 (var-get last-id))))
      ;; Make sure to replace astro-nft
      (match (nft-mint? astro-nft next-id new-owner)
        success
          (begin
            (var-set last-id next-id)
            (print "minting")
            (ok true))
        error (err error))))



(define-read-only (get-caller)
  (begin
    (print contract-caller))
  )

(define-read-only (get-sender)
  (begin
    (print tx-sender)
  )
)

(define-read-only (get-mint-price)
  (begin
    (print (var-get mint-price))
    (ok (var-get mint-price))
  )
)

;; this is cos clarity doesn't have a concat string/int function
(define-constant LIST_40 (list
  true true true true true true true true true true
  true true true true true true true true true true
  true true true true true true true true true true
  true true true true true true true true true true
))

(define-read-only (uint-to-string (value uint))
  (get return (fold uint-to-string-clojure LIST_40 {value: value, return: ""}))
)

(define-read-only (uint-to-string-clojure (i bool) (data {value: uint, return: (string-ascii 40)}))
  (if (> (get value data) u0)
    {
      value: (/ (get value data) u10),
      return: (unwrap-panic (as-max-len? (concat (unwrap-panic (element-at "0123456789" (mod (get value data) u10))) (get return data)) u40))
    }
    data
  )
)


(define-read-only (get-token-string (tid uint))
  (ok
    (uint-to-string tid)
  )
)


(define-read-only (mint-check)
  (print {evt: "mint-check", last: (var-get last-id), owner: tx-sender, amount: (var-get mint-price)})
)


;; (define-read-only (get-info (token-id uint))
;;   (ok (
;;     (tuple (last-id key-type-0) (key-name-1 key-type-1) ...) - a typed tuple with named fields.

;;   )))


;; initializers
(var-set mint-price u10)
;; (var-set meta-base-uri "https://stan.rik.ai/api/meta/astro/")


