<?php


namespace SGC\eGiftCard\Database\Migrations;

class GiftEventsMigrator
{
    protected static $tableName = 'gift_events';

    public static function migrate()
    {
        global $wpdb;

        // Get charset and collation for the table
        $charsetCollate = $wpdb->get_charset_collate();

        // Escape the table name properly
        $table = $wpdb->prefix . esc_sql(static::$tableName);

        // Check if the table already exists
        if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table)) != $table) {// phpcs:ignore WordPress.DB.DirectDatabaseQuery

            // @codingStandardsIgnoreStart
            // SQL query to create the table if it doesn't exist
            $sql = "CREATE TABLE $table (
                `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
                `event_name` VARCHAR(255) NOT NULL,
                `event_file` VARCHAR(255) NOT NULL,
                `event_file_type` ENUM('static', 'audio', 'video') NOT NULL,
                `design_name` VARCHAR(255) DEFAULT NULL,
                `place_holder` VARCHAR(255) DEFAULT NULL,
                `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
                PRIMARY KEY (`id`)
            ) $charsetCollate;";
            // @codingStandardsIgnoreEnd
            
            // Use dbDelta to create or update the table
            require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
            dbDelta($sql);
        }
    }
}
