;; Math Tutor - Free math tutoring program
;; Halal - spreading knowledge
(define-constant CONTRACT-OWNER tx-sender)
(define-constant ERR-AUTH (err u401))
(define-constant ERR-NONE (err u404))
(define-data-var tutor-count uint u0)
(define-data-var session-count uint u0)
(define-map tutors principal { name: (string-utf8 100), level: (string-ascii 20), sessions: uint })
(define-map sessions uint { tutor: principal, student: principal, topic: (string-utf8 100), completed: bool, block: uint })
(define-public (register-tutor (name (string-utf8 100)) (level (string-ascii 20)))
(begin (map-set tutors tx-sender { name: name, level: level, sessions: u0 })
(var-set tutor-count (+ (var-get tutor-count) u1)) (ok true)))
(define-public (book-session (tutor principal) (topic (string-utf8 100)))
(let ((id (+ (var-get session-count) u1)))
(asserts! (is-some (map-get? tutors tutor)) ERR-NONE)
(map-set sessions id { tutor: tutor, student: tx-sender, topic: topic, completed: false, block: stacks-block-height })
(var-set session-count id) (ok id)))
(define-public (complete-session (id uint))
(let ((s (unwrap! (map-get? sessions id) ERR-NONE)) (t (unwrap! (map-get? tutors (get tutor s)) ERR-NONE)))
(asserts! (is-eq tx-sender (get tutor s)) ERR-AUTH)
(map-set sessions id (merge s { completed: true }))
(map-set tutors tx-sender (merge t { sessions: (+ (get sessions t) u1) })) (ok true)))
(define-read-only (get-tutor (who principal)) (map-get? tutors who))
(define-read-only (get-session (id uint)) (map-get? sessions id))
(define-read-only (get-stats) (ok { tutors: (var-get tutor-count), sessions: (var-get session-count) }))