@page "/design/api-methods/create"
@page "/design/api-methods/{Id:int}/edit"
@using ScadaLink.Security
@using ScadaLink.Commons.Entities.InboundApi
@using ScadaLink.Commons.Interfaces.Repositories
@attribute [Authorize(Policy = AuthorizationPolicies.RequireDesign)]
@inject IInboundApiRepository InboundApiRepository
@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 API Method" : "Add API Method")</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" disabled="@Id.HasValue" />
                </div>
                <div class="mb-3">
                    <label class="form-label">Timeout (seconds)</label>
                    <input type="number" class="form-control" @bind="_timeoutSeconds" min="1" />
                </div>
                <div class="mb-3">
                    <label class="form-label">Params (JSON)</label>
                    <input type="text" class="form-control" @bind="_params" />
                </div>
                <div class="mb-3">
                    <label class="form-label">Returns (JSON)</label>
                    <input type="text" class="form-control" @bind="_returns" />
                </div>
                <div class="mb-3">
                    <label class="form-label">Script</label>
                    <textarea class="form-control font-monospace" rows="5" @bind="_script" style="font-size: 0.85rem;"></textarea>
                </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 = "", _script = "";
    private int _timeoutSeconds = 30;
    private string? _params, _returns;
    private string? _formError;

    private ApiMethod? _existing;

    protected override async Task OnInitializedAsync()
    {
        if (Id.HasValue)
        {
            try
            {
                _existing = await InboundApiRepository.GetApiMethodByIdAsync(Id.Value);
                if (_existing != null)
                {
                    _name = _existing.Name;
                    _script = _existing.Script;
                    _timeoutSeconds = _existing.TimeoutSeconds;
                    _params = _existing.ParameterDefinitions;
                    _returns = _existing.ReturnDefinition;
                }
            }
            catch (Exception ex) { _formError = ex.Message; }
        }
        _loading = false;
    }

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

        try
        {
            if (_existing != null)
            {
                _existing.Script = _script;
                _existing.TimeoutSeconds = _timeoutSeconds;
                _existing.ParameterDefinitions = _params?.Trim();
                _existing.ReturnDefinition = _returns?.Trim();
                await InboundApiRepository.UpdateApiMethodAsync(_existing);
            }
            else
            {
                var m = new ApiMethod(_name.Trim(), _script)
                {
                    TimeoutSeconds = _timeoutSeconds,
                    ParameterDefinitions = _params?.Trim(),
                    ReturnDefinition = _returns?.Trim()
                };
                await InboundApiRepository.AddApiMethodAsync(m);
            }
            await InboundApiRepository.SaveChangesAsync();
            NavigationManager.NavigateTo("/design/external-systems");
        }
        catch (Exception ex) { _formError = ex.Message; }
    }

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