<?php
defined('ABSPATH') || exit;

use SmartPay\Models\Coupon;

class Smartpay_CreateSmartpayCouponsTable
{
    public static function up()
    {
        global $wpdb;

        $table = esc_sql( $wpdb->prefix . 'smartpay_coupons' );

        $defaultStatus = Coupon::PUBLISH;

        $charsetCollate = $wpdb->get_charset_collate();

		// coupons Table Creation, caching not applicable, safe prepare.
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
        if ($wpdb->get_var("SHOW TABLES LIKE '$table'") != $table) {
			// Schema creation with dbDelta() on plugin activation.
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange
            $sql = "CREATE TABLE $table (
                `id` BIGINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
                `title` VARCHAR(255) NOT NULL,
                `description` TEXT NULL,
                `discount_type` VARCHAR(50) NOT NULL,
                `discount_amount` FLOAT DEFAULT 0,
                `status` VARCHAR(45) DEFAULT '$defaultStatus',
                `expiry_date` TIMESTAMP NULL DEFAULT NULL,
                `created_by` BIGINT UNSIGNED DEFAULT 0,
                `extra` LONGTEXT DEFAULT NULL,
                `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            ) $charsetCollate";

            dbDelta($sql);
        }
    }
}
