<?php
namespace Elementor_Pay_Addons\Database;

use Elementor_Pay_Addons\Shared\Logger;

defined('ABSPATH') || exit;

class Database_Updater {
    private $db_version = '1.0'; // Increment this when you need DB changes
    private $db_version_key = 'epa_db_version';

    public function __construct() {
        $this->check_db_version();
    }

    public function check_db_version() {
        // update_option($this->db_version_key, '');
        $installed_version = get_option($this->db_version_key);
        if ($installed_version != $this->db_version) {
            $this->upgrade_database($installed_version);
            update_option($this->db_version_key, $this->db_version);
        }
    }

    private function upgrade_database($installed_version) {
        global $wpdb;
        // Example upgrade paths
        if (version_compare($installed_version, '1.0', '<') || !$installed_version) {
            $this->create_initial_table();
        }
        // Add future upgrade steps here, e.g.:
        // if (version_compare($installed_version, '1.1', '<')) {
        //     $this->add_new_columns();
        // }
    }

    private function create_initial_table() {
        Transaction_Query::remove_table();
        Transaction_Query::create_table();
    }

    // Example for future column addition
    // private function add_new_columns() {
    //     global $wpdb;
    //     $table_name = $wpdb->prefix . Transaction_Query::TABLE;
    //     $column_exists = $wpdb->get_results($wpdb->prepare(
    //         "SHOW COLUMNS FROM $table_name LIKE %s",
    //         'new_column'
    //     ));
    //     if (empty($column_exists)) {
    //         $wpdb->query("ALTER TABLE $table_name ADD new_column varchar(255)");
    //     }
    // }
}

new Database_Updater();
