;; Hadith Quiz - Islamic knowledge quiz platform
;; Halal - learning hadith
(define-constant CONTRACT-OWNER tx-sender)
(define-constant ERR-AUTH (err u401))
(define-constant ERR-NONE (err u404))
(define-data-var quiz-count uint u0)
(define-map quizzes uint { creator: principal, question: (string-utf8 200), answer-hash: (string-ascii 64), attempts: uint })
(define-map scores principal { correct: uint, total: uint })
(define-public (create-quiz (question (string-utf8 200)) (answer-hash (string-ascii 64)))
(let ((id (+ (var-get quiz-count) u1)))
(asserts! (is-eq tx-sender CONTRACT-OWNER) ERR-AUTH)
(map-set quizzes id { creator: tx-sender, question: question, answer-hash: answer-hash, attempts: u0 })
(var-set quiz-count id) (ok id)))
(define-public (attempt-quiz (id uint) (answer-hash (string-ascii 64)))
(let ((q (unwrap! (map-get? quizzes id) ERR-NONE)) (s (default-to { correct: u0, total: u0 } (map-get? scores tx-sender))))
(map-set quizzes id (merge q { attempts: (+ (get attempts q) u1) }))
(if (is-eq answer-hash (get answer-hash q))
(begin (map-set scores tx-sender { correct: (+ (get correct s) u1), total: (+ (get total s) u1) }) (ok true))
(begin (map-set scores tx-sender (merge s { total: (+ (get total s) u1) })) (ok false)))))
(define-read-only (get-quiz (id uint)) (map-get? quizzes id))
(define-read-only (get-score (who principal)) (map-get? scores who))
(define-read-only (get-quiz-count) (ok (var-get quiz-count)))