using System.Windows.Controls;
using SharepointToolbox.Core.Models;
using SharepointToolbox.Services;
using SharepointToolbox.ViewModels.Tabs;

namespace SharepointToolbox.Views.Tabs;

public partial class UserAccessAuditView : UserControl
{
    public UserAccessAuditView(UserAccessAuditViewModel viewModel)
    {
        InitializeComponent();
        DataContext = viewModel;
    }

    private void SearchResultsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (sender is ListBox listBox && listBox.SelectedItem is GraphUserResult user)
        {
            var vm = (UserAccessAuditViewModel)DataContext;
            if (vm.AddUserCommand.CanExecute(user))
                vm.AddUserCommand.Execute(user);

            // Clear selection so the same item can be re-selected if needed
            listBox.SelectedItem = null;
        }
    }

    private void DirectoryDataGrid_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        if (sender is DataGrid grid && grid.SelectedItem is GraphDirectoryUser user)
        {
            var vm = (UserAccessAuditViewModel)DataContext;
            if (vm.SelectDirectoryUserCommand.CanExecute(user))
                vm.SelectDirectoryUserCommand.Execute(user);
        }
    }
}
