<?php

namespace Digtective\Repository;

class TableManagerRepository{
    public static function createTable($table_ddl){
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

        dbDelta($table_ddl);
    }

    public static function dropTable($table_name){
        global $wpdb;

        $wpdb->query("DROP TABLE IF EXISTS `$table_name`"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.SchemaChange
    }

    public static function tableExists(string $table_name): bool{
        global $wpdb;

        $result = $wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table_name)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.SchemaChange

        return $result == $table_name;
    }

    public static function getCharset(): string{
        global $wpdb;

        return $wpdb->get_charset_collate();
    }

    public static function getPrefix(): string{
        global $wpdb;

        return $wpdb->prefix;
    }
}
