<?php

namespace WPIS;

require_once __DIR__ . '/Shortcodes/VueEstateListingShortcode.php';
require_once __DIR__ . '/Shortcodes/EstateDetailsListShortcode.php';
require_once __DIR__ . '/Shortcodes/EstateMetaLabelShortcode.php';
require_once __DIR__ . '/Shortcodes/EstateMetaValueShortcode.php';
require_once __DIR__ . '/Shortcodes/EstateMetaGroupShortcode.php';
require_once __DIR__ . '/Shortcodes/FormContactShortcode.php';
require_once __DIR__ . '/Shortcodes/FormEvaluationShortcode.php';
require_once __DIR__ . '/Shortcodes/FormEstateShortcode.php';

class DatabaseManager {

    static public function install() {
        self::createContactTable();
        self::createConnectionTable();
        self::createSettingsTable();
    }

    static public function uninstall() {
        self::removeTable(WPIS::WP_TABLE_CONNEXION);
        self::removeTable(WPIS::WP_TABLE_SETTINGS);
        self::removeTable(WPIS::WP_TABLE_CONTACTS);
    }

    static public function createContactTable() {

        $charset = self::db()->get_charset_collate();
        $table = self::db()->prefix . WPIS::WP_TABLE_CONTACTS;

        $query = "CREATE TABLE IF NOT EXISTS $table (
          id INT AUTO_INCREMENT PRIMARY KEY,
          source tinytext NOT NULL,
          firstname tinytext NOT NULL,
          lastname tinytext NOT NULL,
          email tinytext NOT NULL,
          form_data TEXT NOT NULL,
          email_recipient BOOLEAN NOT NULL,
          email_notification BOOLEAN NOT NULL,
          software_contact_id tinytext NULL,
          created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        ) $charset;";

        self::db()->query( $query );
    }

    static public function createConnectionTable() {

        $charset = self::db()->get_charset_collate();
        $table = self::db()->prefix . WPIS::WP_TABLE_CONNEXION;

        $query = "CREATE TABLE IF NOT EXISTS $table (
            access_id tinytext NOT NULL,
            name tinytext NOT NULL,
            account tinytext NOT NULL,
            token TEXT NOT NULL,
            activated_at TIMESTAMP NOT NULL,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
          ) $charset;";

        self::db()->query( $query );
    }

    static public function createSettingsTable() {

        $charset = self::db()->get_charset_collate();
        $table = self::db()->prefix . WPIS::WP_TABLE_SETTINGS;

        $query = "CREATE TABLE IF NOT EXISTS $table (
            name VARCHAR(50) UNIQUE NOT NULL,
            value VARCHAR(50),
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
          ) $charset;";

        self::db()->query( $query );
    }

    static private function removeTable($table){
        $query = 'DROP TABLE IF EXISTS ' . self::db()->prefix . $table . ';';
        self::db()->query($query);
    }

    static public function db(){
        return $GLOBALS['wpdb'];
    }

}