import prologue, prologue/middlewares/staticfile, prologue/middlewares/cors, std/json, sysrandom, asyncdispatch, std/strutils, db_connector/db_sqlite, std/asyncnet

const key_len = 128 
const refer_len = 16
const debug = false

let db = open("real.db", "", "", "")

db.exec(
sql"""CREATE TABLE IF NOT EXISTS keys(
    referred_by VARCHAR(255) NOT NULL,
    referral    VARCHAR(255) NOT NULL,
    key         VARCHAR(255) NOT NULL,
    ip          VARCHAR(255) NOT NULL
)""")

proc index*(ctx: Context) {.async.} = 
    await ctx.staticFileResponse("public/index.html", "")

proc ip_exists*(ip: string): bool = 
    return len(db.getAllRows(sql"SELECT * FROM keys WHERE ip=?", ip)) != 0

proc referral_exists*(refer: string): bool =
    let results = db.getAllRows(sql"SELECT * FROM keys WHERE referral=?", refer)
    return len(results) != 0

proc table_empty*(): bool = 
    return len(db.getAllRows(sql"SELECT * FROM keys")) == 0

proc generate_referral*(key, referral: var string, ip: string, referred_by: string): void = 
    key = "K-" & getRandomString(key_len)
    referral = "R-" & getRandomString(refer_len)
    db.exec(sql"INSERT INTO keys (referral, key, ip, referred_by) VALUES (?, ?, ?, ?)", referral, key, ip, referred_by)

proc gentry*(ctx: Context) {.async.} =
    var key: string = ""
    var referral: string = ""
    var err: bool = true

    echo table_empty()

    if ((not ctx.request.hasHeader("referral") or 
        not referral_exists($ctx.request.getHeader("referral")) or
        ip_exists(ctx.request.hostName)) and not table_empty()) and not debug:
        err = true
    else:
        generate_referral(key,referral, ctx.request.hostName, $ctx.request.getHeader("referral"))
        err = false

    var info = %* 
        {
            "referral": referral,
            "key": key,
            "err": err
        }

    resp jsonResponse(info)

var app = newApp()

app.use(CorsMiddleware(allowOrigins = @["*"], allowHeaders = @["*"], allowMethods = @["*"])) 
app.use(staticFileMiddleware("./public"))
app.addRoute("/", index)
app.addRoute("/entry", gentry)
app.run()
