@page "/admin/ldap-mappings/create"
@page "/admin/ldap-mappings/{Id:int}/edit"
@using ZB.MOM.WW.ScadaBridge.Commons.Entities.Security
@using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites
@using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories
@using ZB.MOM.WW.ScadaBridge.Security
@attribute [Authorize(Policy = AuthorizationPolicies.RequireAdmin)]
@inject ISecurityRepository SecurityRepository
@inject ISiteRepository SiteRepository
@inject NavigationManager NavigationManager
@inject IDialogService Dialog

<div class="container-fluid mt-3">
    <div class="mb-3">
        <button class="btn btn-outline-secondary btn-sm"
                aria-label="Back to LDAP mappings"
                @onclick="GoBack">
            &larr; Back
        </button>
    </div>

    <div class="card mb-3">
        <div class="card-body">
            <h5 class="card-title">Mapping</h5>
            <div class="mb-2">
                <label class="form-label small">LDAP Group Name</label>
                <input type="text" class="form-control form-control-sm" @bind="_formGroupName" />
            </div>
            <div class="mb-2">
                <label class="form-label small">Role</label>
                <select class="form-select form-select-sm" @bind="_formRole">
                    <option value="">Select role...</option>
                    <option value="@Roles.Administrator">Administrator</option>
                    <option value="@Roles.Designer">Designer</option>
                    <option value="@Roles.Deployer">Deployer</option>
                    <option value="@Roles.Viewer">Viewer</option>
                </select>
                <div class="form-text">Deployer role: configure site scope below after saving.</div>
            </div>
            @if (_formError != null)
            {
                <div class="text-danger small mt-2">@_formError</div>
            }
            <div class="mt-3">
                <button class="btn btn-success btn-sm me-1" @onclick="SaveMapping">Save</button>
                <button class="btn btn-outline-secondary btn-sm" @onclick="GoBack">Cancel</button>
            </div>
        </div>
    </div>

    <div class="card mb-3">
        <div class="card-body">
            <h5 class="card-title">Site Scope Rules</h5>

            @if (!IsEditMode)
            {
                <p class="text-muted small mb-0">Save the mapping first to configure site scope.</p>
            }
            else
            {
                @if (_scopeRules.Count > 0)
                {
                    <div class="d-flex flex-wrap gap-2 mb-3">
                        @foreach (var rule in _scopeRules)
                        {
                            var siteName = _siteLookup.GetValueOrDefault(rule.SiteId)?.Name ?? $"Site {rule.SiteId}";
                            <span class="badge bg-info text-dark d-inline-flex align-items-center">
                                @siteName
                                <button type="button"
                                        class="btn-close btn-close-white ms-2"
                                        style="font-size: 0.6rem;"
                                        aria-label="@($"Remove scope rule for {siteName}")"
                                        @onclick="() => DeleteScopeRule(rule)"></button>
                            </span>
                        }
                    </div>
                }
                else
                {
                    <p class="text-muted small mb-3">All sites (no restrictions)</p>
                }

                <div class="row g-2 align-items-end">
                    <div class="col-auto">
                        <label class="form-label small">Site</label>
                        <select class="form-select form-select-sm" @bind="_scopeRuleSiteId">
                            <option value="0">Select site...</option>
                            @foreach (var site in _sites)
                            {
                                <option value="@site.Id">@site.Name</option>
                            }
                        </select>
                    </div>
                    <div class="col-auto">
                        <button class="btn btn-success btn-sm" @onclick="AddScopeRule">Add scope rule</button>
                    </div>
                </div>
                @if (_scopeRuleError != null)
                {
                    <div class="text-danger small mt-2">@_scopeRuleError</div>
                }
            }
        </div>
    </div>
</div>

@code {
    [Parameter] public int? Id { get; set; }

    private bool IsEditMode => Id.HasValue;

    private LdapGroupMapping? _editingMapping;
    private string _formGroupName = string.Empty;
    private string _formRole = string.Empty;
    private string? _formError;

    private List<SiteScopeRule> _scopeRules = new();
    private List<Site> _sites = new();
    private Dictionary<int, Site> _siteLookup = new();
    private int _scopeRuleSiteId;
    private string? _scopeRuleError;

    protected override async Task OnInitializedAsync()
    {
        _sites = (await SiteRepository.GetAllSitesAsync()).ToList();
        _siteLookup = _sites.ToDictionary(s => s.Id);

        if (Id.HasValue)
        {
            _editingMapping = await SecurityRepository.GetMappingByIdAsync(Id.Value);
            if (_editingMapping != null)
            {
                _formGroupName = _editingMapping.LdapGroupName;
                _formRole = _editingMapping.Role;
                _scopeRules = (await SecurityRepository.GetScopeRulesForMappingAsync(Id.Value)).ToList();
            }
        }
    }

    private void GoBack()
    {
        NavigationManager.NavigateTo("/admin/ldap-mappings");
    }

    private async Task SaveMapping()
    {
        _formError = null;

        if (string.IsNullOrWhiteSpace(_formGroupName))
        {
            _formError = "LDAP Group Name is required.";
            return;
        }
        if (string.IsNullOrWhiteSpace(_formRole))
        {
            _formError = "Role is required.";
            return;
        }

        try
        {
            if (_editingMapping != null)
            {
                _editingMapping.LdapGroupName = _formGroupName.Trim();
                _editingMapping.Role = _formRole;
                await SecurityRepository.UpdateMappingAsync(_editingMapping);
            }
            else
            {
                var mapping = new LdapGroupMapping(_formGroupName.Trim(), _formRole);
                await SecurityRepository.AddMappingAsync(mapping);
            }

            await SecurityRepository.SaveChangesAsync();
            NavigationManager.NavigateTo("/admin/ldap-mappings");
        }
        catch (Exception ex)
        {
            _formError = $"Save failed: {ex.Message}";
        }
    }

    private async Task AddScopeRule()
    {
        _scopeRuleError = null;

        if (_scopeRuleSiteId <= 0)
        {
            _scopeRuleError = "Select a site to add a scope rule.";
            return;
        }

        try
        {
            var rule = new SiteScopeRule { LdapGroupMappingId = Id!.Value, SiteId = _scopeRuleSiteId };
            await SecurityRepository.AddScopeRuleAsync(rule);
            await SecurityRepository.SaveChangesAsync();
            _scopeRules = (await SecurityRepository.GetScopeRulesForMappingAsync(Id.Value)).ToList();
            _scopeRuleSiteId = 0;
        }
        catch (Exception ex)
        {
            _scopeRuleError = $"Save failed: {ex.Message}";
        }
    }

    private async Task DeleteScopeRule(SiteScopeRule rule)
    {
        var siteName = _siteLookup.GetValueOrDefault(rule.SiteId)?.Name ?? $"Site {rule.SiteId}";
        var confirmed = await Dialog.ConfirmAsync(
            "Remove Scope Rule",
            $"Remove scope rule for '{siteName}'?",
            danger: true);
        if (!confirmed) return;

        try
        {
            await SecurityRepository.DeleteScopeRuleAsync(rule.Id);
            await SecurityRepository.SaveChangesAsync();
            _scopeRules = (await SecurityRepository.GetScopeRulesForMappingAsync(Id!.Value)).ToList();
        }
        catch (Exception ex)
        {
            _scopeRuleError = $"Delete failed: {ex.Message}";
        }
    }
}
