<?php

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

    class Action_Wear_Sql_Version_1_0_7 extends Action_Wear_Sql_Version_Abstract implements Action_Wear_Sql_Version_Interface
    {

        public static function versioningScript()
        {
            global $wpdb;
            $charset = $wpdb->get_charset_collate();

            // Create actionwear_addresses table
            $addresses_table = $wpdb->prefix . 'actionwear_addresses';
            $sql = "CREATE TABLE IF NOT EXISTS $addresses_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 '',
                type ENUM('merchant', 'customer') DEFAULT 'merchant',
                created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
            ) $charset;";

            require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
            dbDelta($sql);

            // Add destination_address_id column to actionwear_orders
            $orders_table = $wpdb->prefix . 'actionwear_orders';
            $column_exists = $wpdb->get_results("SHOW COLUMNS FROM $orders_table LIKE 'destination_address_id'");
            if (empty($column_exists)) {
                $wpdb->query("ALTER TABLE $orders_table ADD COLUMN destination_address_id BIGINT UNSIGNED NULL DEFAULT NULL");
            }
        }
    }
}
