@page "/design/db-connections/create"
@page "/design/db-connections/{Id:int}/edit"
@using ScadaLink.Security
@using ScadaLink.Commons.Entities.ExternalSystems
@using ScadaLink.Commons.Interfaces.Repositories
@attribute [Authorize(Policy = AuthorizationPolicies.RequireDesign)]
@inject IExternalSystemRepository ExternalSystemRepository
@inject NavigationManager NavigationManager

<div class="container-fluid mt-3">
    <button class="btn btn-link text-decoration-none ps-0 mb-2" @onclick="GoBack">&larr; Back</button>

    <h4 class="mb-3">@(Id.HasValue ? "Edit Database Connection" : "Add Database Connection")</h4>

    @if (_loading)
    {
        <LoadingSpinner IsLoading="true" />
    }
    else
    {
        <div class="card">
            <div class="card-body">
                <div class="mb-3">
                    <label class="form-label">Name</label>
                    <input type="text" class="form-control" @bind="_name" />
                </div>
                <div class="mb-3">
                    <label class="form-label">Connection String</label>
                    <input type="text" class="form-control" @bind="_connectionString" />
                    <div class="form-text">Treat as sensitive — visible to admins only.</div>
                </div>
                <div class="mb-3">
                    <label class="form-label">Max Retries</label>
                    <input type="number" class="form-control" @bind="_maxRetries" min="0" />
                </div>
                <div class="mb-3">
                    <label class="form-label">Retry Delay (seconds)</label>
                    <input type="number" class="form-control" @bind="_retryDelaySeconds" min="0" />
                </div>

                @if (_formError != null)
                {
                    <div class="text-danger small mb-2">@_formError</div>
                }

                <div class="d-flex gap-2">
                    <button class="btn btn-success" @onclick="Save">Save</button>
                    <button class="btn btn-outline-secondary" @onclick="GoBack">Cancel</button>
                </div>
            </div>
        </div>
    }
</div>

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

    private bool _loading = true;
    private string _name = "", _connectionString = "";
    private int _maxRetries = 3;
    private int _retryDelaySeconds = 5;
    private string? _formError;

    private DatabaseConnectionDefinition? _existing;

    protected override async Task OnInitializedAsync()
    {
        if (Id.HasValue)
        {
            try
            {
                _existing = await ExternalSystemRepository.GetDatabaseConnectionByIdAsync(Id.Value);
                if (_existing != null)
                {
                    _name = _existing.Name;
                    _connectionString = _existing.ConnectionString;
                    _maxRetries = _existing.MaxRetries;
                    _retryDelaySeconds = (int)_existing.RetryDelay.TotalSeconds;
                }
            }
            catch (Exception ex) { _formError = ex.Message; }
        }
        _loading = false;
    }

    private async Task Save()
    {
        _formError = null;
        if (string.IsNullOrWhiteSpace(_name) || string.IsNullOrWhiteSpace(_connectionString))
        {
            _formError = "Name and connection string required.";
            return;
        }

        try
        {
            if (_existing != null)
            {
                _existing.Name = _name.Trim();
                _existing.ConnectionString = _connectionString.Trim();
                _existing.MaxRetries = _maxRetries;
                _existing.RetryDelay = TimeSpan.FromSeconds(_retryDelaySeconds);
                await ExternalSystemRepository.UpdateDatabaseConnectionAsync(_existing);
            }
            else
            {
                var dc = new DatabaseConnectionDefinition(_name.Trim(), _connectionString.Trim())
                {
                    MaxRetries = _maxRetries,
                    RetryDelay = TimeSpan.FromSeconds(_retryDelaySeconds)
                };
                await ExternalSystemRepository.AddDatabaseConnectionAsync(dc);
            }
            await ExternalSystemRepository.SaveChangesAsync();
            NavigationManager.NavigateTo("/design/external-systems");
        }
        catch (Exception ex) { _formError = ex.Message; }
    }

    private void GoBack() => NavigationManager.NavigateTo("/design/external-systems");
}
