-- Sipariş numarası sırası: FMX-{id}
CREATE TABLE IF NOT EXISTS ecommerce_order_sequences (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY
) ENGINE=InnoDB;

-- Mevcut siparişlerden sonra devam et (idempotent; mevcut seq varsa düşürmez)
SET @order_next = (SELECT COALESCE(MAX(id), 0) + 1 FROM ecommerce_orders);
SET @seq_next = (SELECT COALESCE(MAX(id), 0) + 1 FROM ecommerce_order_sequences);
SET @next_seq = GREATEST(COALESCE(@order_next, 1), COALESCE(@seq_next, 1));
SET @alter_sql = CONCAT('ALTER TABLE ecommerce_order_sequences AUTO_INCREMENT = ', @next_seq);
PREPARE alter_stmt FROM @alter_sql;
EXECUTE alter_stmt;
DEALLOCATE PREPARE alter_stmt;
