function [M, nameExceptionSample, nameExceptionModel] = CompareRoiName(studyOverview, Model)
%COMPAREROINAME Compares the RoiName of a given Sample table and from the
% loaded Model.
%   The output map contains pairs between Sample and Model ROI Names that
%   are closest to each other. The keys of the map are the Sample ROI Names
%   and the values are the Model ROI Names.

uniqueRoiNamesSample = unique(fieldnames(studyOverview.ROIInformation));
uniqueRoiNamesModel = unique(fieldnames(Model));

nameExceptionSample = setdiff(uniqueRoiNamesSample, uniqueRoiNamesModel);
nameExceptionModel = setdiff(uniqueRoiNamesModel, uniqueRoiNamesSample);

if ~isempty(nameExceptionModel) && ~isempty(nameExceptionSample)
  % Normalize strings: convert to lowercase, remove punctuation, underscores, asterisks, and minus signs
  nameExceptionSample_norm = cellfun(@(s) lower(regexprep(s, '[^\w]|_|[*]|[-]', '')), nameExceptionSample, 'UniformOutput', false);
  nameExceptionModel_norm = cellfun(@(s) lower(regexprep(s, '[^\w]|_|[*]|[-]', '')), nameExceptionModel, 'UniformOutput', false);
  
  % Initialize a matrix to store similarity scores
  similarityScores = zeros(length(nameExceptionSample_norm), length(nameExceptionModel_norm));
  
  % Calculate similarity scores using Damerau-Levenshtein distance
  for i = 1:length(nameExceptionSample_norm)
      for j = 1:length(nameExceptionModel_norm)
          % the rows are the names of the sample ROIs
          % the colums are the names of the model ROIs
          similarityScores(i, j) = editDistance(nameExceptionSample_norm{i}, nameExceptionModel_norm{j}, 'SwapCost', 1);
      end
  end
  
  % Find the indices of the two pairs with the highest similarity scores
  [~, sortedScore] = min(similarityScores,[],2);
  % [row,col] = find(similarityScores == sortedScore, length(nameExceptionModel));
  
  % Display the most similar pairs
  keys = {}; % keys stores the ROI names from the sample
  vals = {}; % vals stores the ROI names of the model. Thus key values will be renamed to the according val value.
  
  for iRow = 1:length(sortedScore)
    if iRow > length(nameExceptionSample)
      % triggers in case there are less ROIs defined in the sample than
      % there are available in the model
      fprintf('Less ROIs defined in Sample than in Model. Did not find a fitting ROI in sample for %s', nameExceptionModel{sortedScore(iRow)})
      break
    end

    keys{iRow} = nameExceptionSample{iRow};
    vals{iRow} = nameExceptionModel{sortedScore(iRow)};

    if similarityScores(iRow, sortedScore(iRow)) > 5
      promptText = fprintf('Unexpected high Damerau-Levenshtein distance between pair (%s -> %s). Skipping... \n', nameExceptionSample{iRow}, nameExceptionModel{sortedScore(iRow)});
      % if exist('app', 'var') == 2
      %     % Running in App Designer, use uiconfirm for a pop-up
      %     uiconfirm(promptText, 'Prompt', 'modal');
      % else
      %     % Running in command window, use disp for a command line prompt
      %     disp(promptText);
      %     promptRes = input('Y/N: ');
      %     if isempty(promptRes)
      %       promptRes = 'Y';
      %     end
      % 
      %     if strcmp(promptRes, 'N')
      %       ME = MException('PCAOutlierModel:RoiMismatch', ...
      %     'All Rois in Model need to match the sampled Data',str);
      %       throw(ME)
      %     end
      % end
      vals{iRow} = NaN;
    end
    
    
  end
  
  M = containers.Map(keys, vals);
else
  M = containers.Map();
end
end

