<?php

declare(strict_types=1);

namespace XBotControl;

use React\Promise\PromiseInterface;

class InitTables
{

    public static function create():PromiseInterface
    {
        $db = Storage::getInstance()->db;
        return $db->exec("CREATE TABLE IF NOT EXISTS  ip (data TEXT UNIQUE NOT NULL CHECK (data LIKE '%'), CONSTRAINT valid_ip CHECK (data LIKE '%.%' OR data LIKE '%:%')) STRICT ;")
            ->then(function () use ($db) {
                return $db->exec('CREATE  TABLE IF NOT EXISTS  domain (data TEXT UNIQUE NOT NULL)  STRICT ;');
            })->then(function () use ($db) {
                return $db->exec('CREATE TABLE IF NOT EXISTS  path (data TEXT UNIQUE NOT NULL)  STRICT ;');
            })->then(function () use ($db) {
                return $db->exec('CREATE TABLE IF NOT EXISTS  useragent ( data TEXT UNIQUE NOT NULL)  STRICT ;');
            })->then(function () use ($db) {
                return $db->exec('CREATE TABLE IF NOT EXISTS  headers ( data TEXT UNIQUE NOT NULL) STRICT ;');
            })->then(function () use ($db) {
                return $db->exec("CREATE TABLE IF NOT EXISTS  networkwhitelist ( data TEXT UNIQUE NOT NULL CHECK (data LIKE '%/%'), CONSTRAINT valid_network CHECK ( data LIKE '%.%/%' OR data LIKE '%:%/%' ))  STRICT  ;");
            })->then(function () use ($db) {
                return $db->exec('CREATE TABLE IF NOT EXISTS  request ( id_ip INTEGER NOT NULL, id_method INTEGER NOT NULL, id_domain INTEGER NOT NULL, id_path INTEGER NOT NULL, id_useragent INTEGER NOT NULL, id_headers INTEGER NOT NULL, timestamp INTEGER NOT NULL, FOREIGN KEY (id_ip) REFERENCES ip(rowid), FOREIGN KEY (id_domain) REFERENCES domain(rowid), FOREIGN KEY (id_path) REFERENCES path(rowid), FOREIGN KEY (id_useragent) REFERENCES useragent(rowid), FOREIGN KEY (id_headers) REFERENCES headers(rowid) ) STRICT ;');
            })->then(function () use ($db) {
                return $db->exec('CREATE TABLE IF NOT EXISTS  bot (  name TEXT NOT NULL, keyword TEXT NULL ) STRICT  ;');
            })->then(function () use ($db) {
                return $db->exec('PRAGMA journal_mode=WAL;');
            });
    
        return $db->exec("CREATE TABLE IF NOT EXISTS  ip (id_ip INTEGER PRIMARY KEY AUTOINCREMENT, ip TEXT UNIQUE NOT NULL CHECK (ip LIKE '%'), CONSTRAINT valid_ip CHECK (ip LIKE '%.%' OR ip LIKE '%:%')) STRICT ;")
            ->then(function () use ($db) {
                return $db->exec('CREATE  TABLE IF NOT EXISTS  domain (id_domain INTEGER PRIMARY KEY AUTOINCREMENT, domain TEXT UNIQUE NOT NULL)  STRICT ;');
            })->then(function () use ($db) {
                return $db->exec('CREATE TABLE IF NOT EXISTS  path ( id_path INTEGER PRIMARY KEY AUTOINCREMENT, path TEXT UNIQUE NOT NULL)  STRICT ;');
            })->then(function () use ($db) {
                return $db->exec('CREATE TABLE IF NOT EXISTS  useragent ( id_useragent INTEGER PRIMARY KEY AUTOINCREMENT, useragent TEXT UNIQUE NOT NULL)  STRICT ;');
            })->then(function () use ($db) {
                return $db->exec('CREATE TABLE IF NOT EXISTS  headers ( id_headers INTEGER PRIMARY KEY AUTOINCREMENT, headers TEXT UNIQUE NOT NULL) STRICT ;');
            })->then(function () use ($db) {
                return $db->exec("CREATE TABLE IF NOT EXISTS  networkwhitelist ( id_networkwhitelist INTEGER PRIMARY KEY AUTOINCREMENT, network TEXT UNIQUE NOT NULL CHECK (network LIKE '%/%'), CONSTRAINT valid_network CHECK ( network LIKE '%.%/%' OR network LIKE '%:%/%' ))  STRICT WITHOUT ROWID ;");
            })->then(function () use ($db) {
                return $db->exec('CREATE TABLE IF NOT EXISTS  request ( id_request INTEGER PRIMARY KEY AUTOINCREMENT, id_ip INTEGER NOT NULL, id_method INTEGER NOT NULL, id_domain INTEGER NOT NULL, id_path INTEGER NOT NULL, id_useragent INTEGER NOT NULL, id_headers INTEGER NOT NULL, timestamp INTEGER NOT NULL, FOREIGN KEY (id_ip) REFERENCES ip(id_ip), FOREIGN KEY (id_domain) REFERENCES domain(id_domain), FOREIGN KEY (id_path) REFERENCES path(id_path), FOREIGN KEY (id_useragent) REFERENCES useragent(id_useragent), FOREIGN KEY (id_headers) REFERENCES headers(id_headers) ) STRICT WITHOUT ROWID ;');
            })->then(function () use ($db) {
                return $db->exec('CREATE TABLE IF NOT EXISTS  bot ( id_bot INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, keyword TEXT NULL ) STRICT  ;');
            })->then(function () use ($db) {
                return $db->exec('PRAGMA journal_mode=WAL;');
            });
    }
}
