@using ZB.MOM.WW.OtOpcUa.Admin.Services
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
@inject DriverInstanceService DriverSvc
@inject NamespaceService NsSvc

<div class="d-flex justify-content-between mb-3">
    <h4>DriverInstances</h4>
    <button class="btn btn-sm btn-primary" @onclick="() => _showForm = true">Add driver</button>
</div>

@if (_drivers is null) { <p>Loading…</p> }
else if (_drivers.Count == 0) { <p class="text-muted">No drivers configured in this draft.</p> }
else
{
    <table class="table table-sm">
        <thead><tr><th>DriverInstanceId</th><th>Name</th><th>Type</th><th>Namespace</th></tr></thead>
        <tbody>
        @foreach (var d in _drivers)
        {
            <tr><td><code>@d.DriverInstanceId</code></td><td>@d.Name</td><td>@d.DriverType</td><td><code>@d.NamespaceId</code></td></tr>
        }
        </tbody>
    </table>
}

@if (_showForm && _namespaces is not null)
{
    <div class="card">
        <div class="card-body">
            <div class="row g-3">
                <div class="col-md-3">
                    <label class="form-label">Name</label>
                    <input class="form-control" @bind="_name"/>
                </div>
                <div class="col-md-3">
                    <label class="form-label">DriverType</label>
                    <select class="form-select" @bind="_type">
                        <option>Galaxy</option>
                        <option>ModbusTcp</option>
                        <option>AbCip</option>
                        <option>AbLegacy</option>
                        <option>S7</option>
                        <option>Focas</option>
                        <option>OpcUaClient</option>
                    </select>
                </div>
                <div class="col-md-6">
                    <label class="form-label">Namespace</label>
                    <select class="form-select" @bind="_nsId">
                        @foreach (var n in _namespaces) { <option value="@n.NamespaceId">@n.Kind — @n.NamespaceUri</option> }
                    </select>
                </div>
                <div class="col-12">
                    <label class="form-label">DriverConfig JSON (schemaless per driver type)</label>
                    <textarea class="form-control font-monospace" rows="6" @bind="_config"></textarea>
                    <div class="form-text">Phase 1: generic JSON editor — per-driver schema validation arrives in each driver's phase (decision #94).</div>
                </div>
            </div>
            @if (_error is not null) { <div class="alert alert-danger mt-3">@_error</div> }
            <div class="mt-3">
                <button class="btn btn-sm btn-primary" @onclick="SaveAsync">Save</button>
                <button class="btn btn-sm btn-secondary ms-2" @onclick="() => _showForm = false">Cancel</button>
            </div>
        </div>
    </div>
}

@code {
    [Parameter] public long GenerationId { get; set; }
    [Parameter] public string ClusterId { get; set; } = string.Empty;

    private List<DriverInstance>? _drivers;
    private List<Namespace>? _namespaces;
    private bool _showForm;
    private string _name = string.Empty;
    private string _type = "ModbusTcp";
    private string _nsId = string.Empty;
    private string _config = "{}";
    private string? _error;

    protected override async Task OnParametersSetAsync() => await ReloadAsync();

    private async Task ReloadAsync()
    {
        _drivers = await DriverSvc.ListAsync(GenerationId, CancellationToken.None);
        _namespaces = await NsSvc.ListAsync(GenerationId, CancellationToken.None);
        _nsId = _namespaces.FirstOrDefault()?.NamespaceId ?? string.Empty;
    }

    private async Task SaveAsync()
    {
        _error = null;
        if (string.IsNullOrWhiteSpace(_name) || string.IsNullOrWhiteSpace(_nsId))
        {
            _error = "Name and Namespace are required";
            return;
        }
        try
        {
            await DriverSvc.AddAsync(GenerationId, ClusterId, _nsId, _name, _type, _config, CancellationToken.None);
            _name = string.Empty; _config = "{}";
            _showForm = false;
            await ReloadAsync();
        }
        catch (Exception ex) { _error = ex.Message; }
    }
}
