<?php

use Dolphy\Plugin\Helpers\DB;
use Dolphy\Plugin\Options\Options;
use Dolphy\Plugin\Security\DPLC;
use Dolphy\Plugin\Security\DPLCVerified;
use Dolphy\Plugin\Security\Login;
use Dolphy\Plugin\Security\TwoFA;
use Dolphy\Plugin\Security\TwoFABackup;

defined('DOLPHY_PLUGIN') or die('Access Denied!');

require_once ABSPATH . 'wp-admin/includes/upgrade.php';

add_action('dolphy_activate', function () {
  global $wpdb;
  $charset_collate = $wpdb->get_charset_collate();

  $table = Options::table();

  $sql = "CREATE TABLE $table (
    id bigint UNSIGNED NOT NULL AUTO_INCREMENT,
    name varchar(200) NOT NULL,
    value LONGTEXT NOT NULL,
    createdAt timestamp NULL DEFAULT CURRENT_TIMESTAMP,
    updatedAt timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY  (id),
    UNIQUE KEY name (name),
    KEY createdAt (createdAt),
    KEY updatedAt (updatedAt)
  ) $charset_collate;";

  dbDelta($sql);

  DB::create_triggers($table);
});


add_action('dolphy_activate', function () {
  global $wpdb;
  $charset_collate = $wpdb->get_charset_collate();

  $table = DPLC::table();

  $sql = "CREATE TABLE $table (
    id bigint UNSIGNED NOT NULL AUTO_INCREMENT,
    userId bigint UNSIGNED NOT NULL,
    code varchar(500) NOT NULL,
    remember boolean NOT NULL DEFAULT 0,
    secure boolean NOT NULL DEFAULT 0,
    createdAt timestamp NULL DEFAULT CURRENT_TIMESTAMP,
    updatedAt timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY  (id),
    KEY userId (userId),
    UNIQUE KEY code (code(200)),
    KEY remember (remember),
    KEY secure (secure),
    KEY createdAt (createdAt),
    KEY updatedAt (updatedAt)
  ) $charset_collate;";

  dbDelta($sql);

  DB::create_triggers($table);

  DB::create_foreign_keys($table, array(
    $wpdb->users => array('userId', 'ID')
  ));
});

add_action('dolphy_activate', function () {
  global $wpdb;
  $charset_collate = $wpdb->get_charset_collate();

  $table = DPLCVerified::table();

  $sql = "CREATE TABLE $table (
    id bigint UNSIGNED NOT NULL AUTO_INCREMENT,
    action varchar(50) NOT NULL,
    code varchar(500) NOT NULL,
    createdAt timestamp NULL DEFAULT CURRENT_TIMESTAMP,
    updatedAt timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY  (id),
    KEY action (action),
    KEY code (code(200)),
    KEY createdAt (createdAt),
    KEY updatedAt (updatedAt)
  ) $charset_collate;";

  dbDelta($sql);

  DB::create_triggers($table);

  DB::create_foreign_keys($table, array(
    DPLC::table() => array('code', 'code')
  ));
});


add_action('dolphy_activate', function () {
  global $wpdb;
  $charset_collate = $wpdb->get_charset_collate();

  $table = TwoFA::table();

  $sql = "CREATE TABLE $table (
    id bigint UNSIGNED NOT NULL AUTO_INCREMENT,
    userId bigint UNSIGNED NOT NULL,
    secret varchar(500) NOT NULL,
    createdAt timestamp NULL DEFAULT CURRENT_TIMESTAMP,
    updatedAt timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY  (id),
    UNIQUE KEY userId (userId),
    KEY secret (secret),
    KEY createdAt (createdAt),
    KEY updatedAt (updatedAt)
  ) $charset_collate;";

  dbDelta($sql);

  DB::create_triggers($table);

  DB::create_foreign_keys($table, array(
    $wpdb->users => array('userId', 'ID')
  ));
});


add_action('dolphy_activate', function () {
  global $wpdb;
  $charset_collate = $wpdb->get_charset_collate();

  $table = TwoFABackup::table();

  $sql = "CREATE TABLE $table (
    id bigint UNSIGNED NOT NULL AUTO_INCREMENT,
    userId bigint UNSIGNED NOT NULL,
    code varchar(20) NOT NULL,
    createdAt timestamp NULL DEFAULT CURRENT_TIMESTAMP,
    updatedAt timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY  (id),
    KEY userId (userId),
    KEY code (code),
    KEY createdAt (createdAt),
    KEY updatedAt (updatedAt)
  ) $charset_collate;";

  dbDelta($sql);

  DB::create_triggers($table);

  DB::create_foreign_keys($table, array(
    $wpdb->users => array('userId', 'ID')
  ));
});
