-- ICYLAB OS v8.9.1 - Asignación centralizada de centros financieros
-- Ejecutar una sola vez desde phpMyAdmin, después de las migraciones v8.8 y v8.9.

SET NAMES utf8mb4;

CREATE TABLE IF NOT EXISTS finance_center_assignment_settings (
  id TINYINT UNSIGNED NOT NULL,
  assignment_priority ENUM('supplier_category_global','category_supplier_global') NOT NULL DEFAULT 'supplier_category_global',
  global_cost_center_id INT UNSIGNED DEFAULT NULL,
  auto_apply_manual_costs TINYINT(1) NOT NULL DEFAULT 1,
  auto_apply_dte TINYINT(1) NOT NULL DEFAULT 1,
  auto_apply_recurring TINYINT(1) NOT NULL DEFAULT 1,
  updated_by INT DEFAULT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_center_assignment_global (global_cost_center_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO finance_center_assignment_settings
  (id,assignment_priority,global_cost_center_id,auto_apply_manual_costs,auto_apply_dte,auto_apply_recurring)
VALUES (1,'supplier_category_global',NULL,1,1,1)
ON DUPLICATE KEY UPDATE id=VALUES(id);

SELECT 'ICYLAB_OS_FINANCE_CENTER_ASSIGNMENT_V8_9_1_OK' AS migration_status,
       (SELECT COUNT(*) FROM finance_center_assignment_settings WHERE id=1) AS settings_ready;
