@page "/clusters/{ClusterId}/drivers/new/historianwonderware"
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
@rendermode RenderMode.InteractiveServer
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.EntityFrameworkCore
@using ZB.MOM.WW.OtOpcUa.AdminUI.Clients
@using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers
@using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers.Pickers
@using ZB.MOM.WW.OtOpcUa.Configuration
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
@using ZB.MOM.WW.OtOpcUa.Driver.Historian.Wonderware.Client
@inject IDbContextFactory<OtOpcUaConfigDbContext> DbFactory
@inject NavigationManager Nav

<div class="d-flex justify-content-between align-items-center mb-3">
    <h4 class="mb-0">@(IsNew ? "New Wonderware Historian driver" : "Edit Wonderware Historian driver") &middot; <span class="mono">@ClusterId</span></h4>
    <a href="/clusters/@ClusterId/drivers" class="btn btn-outline-secondary btn-sm">Cancel</a>
</div>
<ClusterNav ClusterId="@ClusterId" ActiveTab="drivers" />

@if (!_loaded)
{
    <p>Loading&hellip;</p>
}
else if (!IsNew && _existing is null)
{
    <section class="panel notice rise" style="animation-delay:.02s">
        Driver instance <span class="mono">@DriverInstanceId</span> was not found in cluster <span class="mono">@ClusterId</span>.
    </section>
}
else
{
    <EditForm Model="_form" OnValidSubmit="SubmitAsync" FormName="historianwonderwareDriverEdit">
        <DataAnnotationsValidator />
        <DriverFormShell IsNew="IsNew" Busy="_busy" Error="_error"
                         CancelHref="@($"/clusters/{ClusterId}/drivers")"
                         OnDelete="@(IsNew ? null : (EventCallback?)EventCallback.Factory.Create(this, DeleteAsync))">

            <DriverIdentitySection Model="_identityModel" Namespaces="_namespaces" IsNew="IsNew" ShowDriverType="false" />

            @if (!IsNew && !string.IsNullOrEmpty(DriverInstanceId))
            {
                <DriverStatusPanel DriverInstanceId="@DriverInstanceId" Enabled="@_identityModel.Enabled" />
            }

            <div class="mt-2 mb-3">
                <DriverTestConnectButton DriverType="@DriverTypeKey"
                                         GetConfigJson="@SerializeCurrentConfig"
                                         TimeoutSeconds="@_form.Historian.ProbeTimeoutSeconds" />
                <button type="button" class="btn btn-sm btn-outline-secondary mt-2"
                        @onclick="@(() => _showPicker = true)">
                    Pick address
                </button>
            </div>

            <DriverTagPicker @bind-Visible="_showPicker"
                             Title="Historian Wonderware address"
                             CurrentAddress="@_pickedAddress"
                             OnPickAddress="@OnAddressPicked">
                <HistorianWonderwareAddressPickerBody CurrentAddress="@_pickedAddress"
                                                      CurrentAddressChanged="@((s) => _pickedAddress = s)" />
            </DriverTagPicker>

            @* Connection *@
            <section class="panel rise mt-3" style="animation-delay:.06s">
                <div class="panel-head">Connection</div>
                <div style="padding:1rem">
                    <div class="row g-3">
                        <div class="col-md-5">
                            <label class="form-label">Named pipe name</label>
                            <InputText @bind-Value="_form.Historian.PipeName" class="form-control form-control-sm mono"
                                       placeholder="otopcua-historian" />
                            <div class="form-text">Must match the sidecar's <code>OTOPCUA_HISTORIAN_PIPE</code> environment variable.</div>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Shared secret</label>
                            <InputText @bind-Value="_form.Historian.SharedSecret" type="password" class="form-control form-control-sm" autocomplete="new-password" />
                            <div class="form-text">Per-process secret verified in the Hello frame — must match the sidecar's configured secret.</div>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Peer name (diagnostic)</label>
                            <InputText @bind-Value="_form.Historian.PeerName" class="form-control form-control-sm"
                                       placeholder="OtOpcUa" />
                            <div class="form-text">Sent in Hello for sidecar logging. Default: OtOpcUa.</div>
                        </div>
                    </div>
                </div>
            </section>

            @* Timing *@
            <section class="panel rise mt-3" style="animation-delay:.08s">
                <div class="panel-head">Timing</div>
                <div style="padding:1rem">
                    <div class="row g-3">
                        <div class="col-md-3">
                            <label class="form-label">Connect timeout (s, blank = default 10 s)</label>
                            <InputNumber @bind-Value="_form.Historian.ConnectTimeoutSeconds" class="form-control form-control-sm" />
                            <div class="form-text">Cap on pipe connect + Hello round-trip. Null = 10 s.</div>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Call timeout (s, blank = default 30 s)</label>
                            <InputNumber @bind-Value="_form.Historian.CallTimeoutSeconds" class="form-control form-control-sm" />
                            <div class="form-text">Cap on a single read/write once connected. Null = 30 s.</div>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Effective connect timeout (s)</label>
                            <input class="form-control form-control-sm" readonly
                                   value="@(_form.Historian.ConnectTimeoutSeconds?.ToString() ?? "10 (default)")" />
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Effective call timeout (s)</label>
                            <input class="form-control form-control-sm" readonly
                                   value="@(_form.Historian.CallTimeoutSeconds?.ToString() ?? "30 (default)")" />
                        </div>
                    </div>
                </div>
            </section>

            @* Diagnostics *@
            <section class="panel rise mt-3" style="animation-delay:.10s">
                <div class="panel-head">Diagnostics</div>
                <div style="padding:1rem">
                    <div class="row g-3">
                        <div class="col-md-3">
                            <label class="form-label">Admin UI probe timeout (seconds)</label>
                            <InputNumber @bind-Value="_form.Historian.ProbeTimeoutSeconds" class="form-control form-control-sm" />
                            <div class="form-text">Max 60. Used by Test Connect. Default 15.</div>
                        </div>
                    </div>
                </div>
            </section>

            <DriverResilienceSection @bind-ResilienceConfig="_form.ResilienceConfig" />
        </DriverFormShell>
    </EditForm>
}

@code {
    [Parameter] public string ClusterId { get; set; } = "";
    [Parameter] public string? DriverInstanceId { get; set; }

    private const string DriverTypeKey = "Historian.Wonderware";

    private bool IsNew => string.IsNullOrEmpty(DriverInstanceId);

    private static readonly System.Text.Json.JsonSerializerOptions _jsonOpts = new()
    {
        PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase,
        UnmappedMemberHandling = System.Text.Json.Serialization.JsonUnmappedMemberHandling.Skip,
        WriteIndented = false,
    };

    private FormModel _form = new();
    private DriverIdentitySection.DriverIdentityModel _identityModel = new() { DriverType = DriverTypeKey };
    private DriverInstance? _existing;
    private List<Namespace> _namespaces = new();
    private bool _loaded;
    private bool _busy;
    private string? _error;

    // Address picker state
    private bool _showPicker;
    private string _pickedAddress = "";

    private void OnAddressPicked(string address) => _pickedAddress = address;

    protected override async Task OnInitializedAsync()
    {
        await using var db = await DbFactory.CreateDbContextAsync();
        _namespaces = await db.Namespaces.AsNoTracking()
            .Where(n => n.ClusterId == ClusterId)
            .OrderBy(n => n.NamespaceId)
            .ToListAsync();

        if (IsNew)
        {
            _identityModel = new()
            {
                DriverInstanceId = "",
                Name = "",
                DriverType = DriverTypeKey,
                NamespaceId = _namespaces.FirstOrDefault()?.NamespaceId ?? "",
                Enabled = true,
            };
            _form = new FormModel();
        }
        else
        {
            _existing = await db.DriverInstances.AsNoTracking()
                .FirstOrDefaultAsync(d => d.ClusterId == ClusterId && d.DriverInstanceId == DriverInstanceId);
            if (_existing is not null)
            {
                _identityModel = new()
                {
                    DriverInstanceId = _existing.DriverInstanceId,
                    Name = _existing.Name,
                    DriverType = _existing.DriverType,
                    NamespaceId = _existing.NamespaceId,
                    Enabled = _existing.Enabled,
                };
                var opts = TryDeserialize(_existing.DriverConfig) ?? CreateDefaultOptions();
                _form = new FormModel();
                _form.Historian = WonderwareHistorianClientFormModel.FromRecord(opts);
                _form.ResilienceConfig = _existing.ResilienceConfig;
                _form.RowVersion = _existing.RowVersion;
            }
        }
        _loaded = true;
    }

    private static WonderwareHistorianClientOptions CreateDefaultOptions() =>
        new(PipeName: "otopcua-historian", SharedSecret: "");

    private async Task SubmitAsync()
    {
        _busy = true; _error = null;
        try
        {
            var opts = _form.Historian.ToRecord();
            var configJson = System.Text.Json.JsonSerializer.Serialize(opts, _jsonOpts);
            await using var db = await DbFactory.CreateDbContextAsync();
            if (IsNew)
            {
                if (await db.DriverInstances.AnyAsync(d => d.DriverInstanceId == _identityModel.DriverInstanceId))
                {
                    _error = $"Driver instance '{_identityModel.DriverInstanceId}' already exists."; return;
                }
                db.DriverInstances.Add(new DriverInstance
                {
                    DriverInstanceId = _identityModel.DriverInstanceId,
                    ClusterId = ClusterId,
                    NamespaceId = _identityModel.NamespaceId,
                    Name = _identityModel.Name,
                    DriverType = DriverTypeKey,
                    Enabled = _identityModel.Enabled,
                    DriverConfig = configJson,
                    ResilienceConfig = string.IsNullOrWhiteSpace(_form.ResilienceConfig) ? null : _form.ResilienceConfig,
                });
            }
            else
            {
                var entity = await db.DriverInstances.FirstOrDefaultAsync(
                    d => d.ClusterId == ClusterId && d.DriverInstanceId == DriverInstanceId);
                if (entity is null) { _error = "Row no longer exists."; return; }
                db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion;
                entity.NamespaceId = _identityModel.NamespaceId;
                entity.Name = _identityModel.Name;
                entity.Enabled = _identityModel.Enabled;
                entity.DriverConfig = configJson;
                entity.ResilienceConfig = string.IsNullOrWhiteSpace(_form.ResilienceConfig) ? null : _form.ResilienceConfig;
            }
            await db.SaveChangesAsync();
            Nav.NavigateTo($"/clusters/{ClusterId}/drivers");
        }
        catch (DbUpdateConcurrencyException)
        {
            _error = "Another user changed this driver instance while you were editing. Reload to see the latest values, then re-apply your changes.";
        }
        catch (Exception ex) { _error = ex.Message; }
        finally { _busy = false; }
    }

    private async Task DeleteAsync()
    {
        if (IsNew) return;
        _busy = true; _error = null;
        try
        {
            await using var db = await DbFactory.CreateDbContextAsync();
            var entity = await db.DriverInstances.FirstOrDefaultAsync(
                d => d.ClusterId == ClusterId && d.DriverInstanceId == DriverInstanceId);
            if (entity is null) { Nav.NavigateTo($"/clusters/{ClusterId}/drivers"); return; }
            db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion;
            db.DriverInstances.Remove(entity);
            await db.SaveChangesAsync();
            Nav.NavigateTo($"/clusters/{ClusterId}/drivers");
        }
        catch (DbUpdateConcurrencyException)
        {
            _error = "Another user changed this driver instance while you were viewing it. Reload before deleting.";
        }
        catch (Exception ex)
        {
            _error = $"Delete failed: {ex.Message}. (Likely because equipment/tags still reference this driver — remove them first.)";
        }
        finally { _busy = false; }
    }

    private string SerializeCurrentConfig()
        => System.Text.Json.JsonSerializer.Serialize(_form.Historian.ToRecord(), _jsonOpts);

    private static WonderwareHistorianClientOptions? TryDeserialize(string json)
    {
        try { return System.Text.Json.JsonSerializer.Deserialize<WonderwareHistorianClientOptions>(json, _jsonOpts); }
        catch { return null; }
    }

    public sealed class FormModel
    {
        public WonderwareHistorianClientFormModel Historian { get; set; } = new();
        public string? ResilienceConfig { get; set; }
        public byte[] RowVersion { get; set; } = [];
    }

    /// <summary>
    ///     Mutable mirror of <see cref="WonderwareHistorianClientOptions"/> (positional record).
    ///     <c>ConnectTimeoutSeconds</c> and <c>CallTimeoutSeconds</c> are nullable int — null
    ///     round-trips to a null TimeSpan?, which the record resolves to its compiled default.
    /// </summary>
    public sealed class WonderwareHistorianClientFormModel
    {
        public string PipeName { get; set; } = "otopcua-historian";
        public string SharedSecret { get; set; } = "";
        public string PeerName { get; set; } = "OtOpcUa";
        public int? ConnectTimeoutSeconds { get; set; }
        public int? CallTimeoutSeconds { get; set; }
        public int ProbeTimeoutSeconds { get; set; } = 15;

        public static WonderwareHistorianClientFormModel FromRecord(WonderwareHistorianClientOptions r) => new()
        {
            PipeName = r.PipeName,
            SharedSecret = r.SharedSecret,
            PeerName = r.PeerName,
            ConnectTimeoutSeconds = r.ConnectTimeout.HasValue ? (int)r.ConnectTimeout.Value.TotalSeconds : null,
            CallTimeoutSeconds = r.CallTimeout.HasValue ? (int)r.CallTimeout.Value.TotalSeconds : null,
            ProbeTimeoutSeconds = r.ProbeTimeoutSeconds,
        };

        public WonderwareHistorianClientOptions ToRecord() => new(
            PipeName: PipeName,
            SharedSecret: SharedSecret,
            PeerName: PeerName,
            ConnectTimeout: ConnectTimeoutSeconds.HasValue ? TimeSpan.FromSeconds(ConnectTimeoutSeconds.Value) : null,
            CallTimeout: CallTimeoutSeconds.HasValue ? TimeSpan.FromSeconds(CallTimeoutSeconds.Value) : null)
        {
            ProbeTimeoutSeconds = ProbeTimeoutSeconds,
        };
    }
}
