<?php

namespace AC_SYNC\Includes\Classes\Sql\Version {
    if (!defined('ABSPATH'))
        exit;

    class Action_Wear_Sql_Version_1_1_1 extends Action_Wear_Sql_Version_Abstract implements Action_Wear_Sql_Version_Interface
    {
        public static function versioningScript()
        {
            global $wpdb;
            $table = $wpdb->prefix . 'actionwear_addresses';

            // Ensure table exists (may have been skipped if DB was already >= 1.0.7)
            $table_exists = $wpdb->get_var("SHOW TABLES LIKE '$table'");
            if (!$table_exists) {
                $charset = $wpdb->get_charset_collate();
                $wpdb->query("CREATE TABLE $table (
                    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                    label VARCHAR(255) NOT NULL,
                    ragione_sociale VARCHAR(255) NOT NULL,
                    indirizzo VARCHAR(255) NOT NULL,
                    localita VARCHAR(255) NOT NULL,
                    cap VARCHAR(10) NOT NULL,
                    provincia VARCHAR(10) NOT NULL,
                    telefono VARCHAR(50) DEFAULT '',
                    nazione VARCHAR(5) DEFAULT 'IT',
                    fermo_deposito VARCHAR(255) DEFAULT '',
                    codice_destinazione VARCHAR(64) NULL DEFAULT NULL,
                    type ENUM('merchant', 'customer') DEFAULT 'merchant',
                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
                ) $charset;");
            } else {
                // Table exists but may lack codice_destinazione column
                $column_exists = $wpdb->get_results("SHOW COLUMNS FROM $table LIKE 'codice_destinazione'");
                if (empty($column_exists)) {
                    $wpdb->query("ALTER TABLE $table ADD COLUMN codice_destinazione VARCHAR(64) NULL DEFAULT NULL AFTER fermo_deposito");
                }
            }
        }
    }
}
