#[Copyright 2024 ITwrx.
This file is part of Simple Site Manager.
Simple Site Manager is released under the GNU Affero General Public License 3.0.
See COPYING or <https://www.gnu.org/licenses/> for details.]#

import std/random
#import sqliteral, "../helpers/db"

type
  HumanChecker* = object
    question*: string
    id*, answer*: int
    
var humanCheckers: seq[HumanChecker]
humanCheckers = @[
  HumanChecker(id: 1, question: "26 + four, minus 10", answer: 20),
  HumanChecker(id: 2, question: "10 minus 2, + 14", answer: 22),
  HumanChecker(id: 3, question: "15 + five, minus 3", answer: 17),
  HumanChecker(id: 4, question: "9 + nine, minus 6", answer: 12),
  HumanChecker(id: 5, question: "13 - three, plus 5", answer: 15),
  HumanChecker(id: 6, question: "7 + six, plus one", answer: 14),
  HumanChecker(id: 7, question: "22 + 8, - 2", answer: 28),
  HumanChecker(id: 8, question: "4 - four, + ten", answer: 10),
  HumanChecker(id: 9, question: "16 + four, minus three", answer: 17),
  HumanChecker(id: 10, question: "twelve minus four, plus 8", answer: 16)
  ]

proc getHumanChecker*(): HumanChecker =  
  {.gcsafe.}:
    randomize()    
    return sample(humancheckers)
  
proc getHCById*(id: int): HumanChecker =
  {.gcsafe.}:
    for hc in humanCheckers:
      if hc.id == id:
        return hc

#for some reason we are creating the humanCheckers here and then checking them from the DB, instead of one data location or the other, like we probably should have. 
#the below was probably supposed to be a test, but then it just stayed like that.
#[proc getHumanChecker*(): HumanChecker =  
  {.gcsafe.}:
    randomize()
    var humanCheckers{.threadvar.}: seq[HumanChecker]
    humanCheckers.add(HumanChecker(id: 1, question: "26 + four, minus 10", answer: 20))
    humanCheckers.add(HumanChecker(id: 2, question: "10 minus 2, + 14", answer: 22))
    humanCheckers.add(HumanChecker(id: 3, question: "15 + five, minus 3", answer: 17))
    humanCheckers.add(HumanChecker(id: 4, question: "9 + nine, minus 6", answer: 12))
    humanCheckers.add(HumanChecker(id: 5, question: "13 - three, plus 5", answer: 15))
    humanCheckers.add(HumanChecker(id: 6, question: "7 + six, plus one", answer: 14))
    humanCheckers.add(HumanChecker(id: 7, question: "22 + 8, - 2", answer: 28))
    humanCheckers.add(HumanChecker(id: 8, question: "4 - four, + ten", answer: 10))
    humanCheckers.add(HumanChecker(id: 9, question: "16 + four, minus three", answer: 17))
    humanCheckers.add(HumanChecker(id: 10, question: "twelve minus four, plus 8", answer: 16))
    return sample(humancheckers)
  
proc getHCById*(id: int): HumanChecker =
  {.gcsafe.}:
    var hc {.threadvar.}: HumanChecker
    #prepareDb2SQL()
    for row in db2.rows(SelectHumanCheckerById, id): 
      hc.id = row.getInt(0)
      hc.question = row.getString(1)
      hc.answer = row.getInt(2)
    return hc]#
  
