@page "/clusters/{ClusterId}/equipment/new"
@page "/clusters/{ClusterId}/equipment/{EquipmentId}"
@* Equipment CRUD. EquipmentId is system-generated (decision #125) — operator picks Name +
   MachineCode + UnsLine + Driver; the EquipmentId is derived from the EquipmentUuid on create.
   OPC 40010 identification fields (Manufacturer, Model, etc.) are all optional. *@
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
@rendermode RenderMode.InteractiveServer
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.EntityFrameworkCore
@using System.ComponentModel.DataAnnotations
@using ZB.MOM.WW.OtOpcUa.Configuration
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
@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 equipment" : "Edit equipment") &middot; <span class="mono">@ClusterId</span></h4>
    <a href="/clusters/@ClusterId/equipment" class="btn btn-outline-secondary btn-sm">Cancel</a>
</div>

<ClusterNav ClusterId="@ClusterId" ActiveTab="equipment" />

@if (!_loaded)
{
    <p>Loading…</p>
}
else if (!IsNew && _existing is null)
{
    <section class="panel notice rise" style="animation-delay:.02s">
        Equipment <span class="mono">@EquipmentId</span> was not found.
    </section>
}
else
{
    <EditForm Model="_form" OnValidSubmit="SubmitAsync" FormName="equipmentEdit">
        <DataAnnotationsValidator />
        <section class="panel rise" style="animation-delay:.02s">
            <div class="panel-head">Identity</div>
            <div style="padding:1rem">
                @if (!IsNew)
                {
                    <div class="mb-3">
                        <label class="form-label">EquipmentId</label>
                        <input class="form-control form-control-sm mono" value="@EquipmentId" disabled />
                        <div class="form-text">System-generated; never operator-edited.</div>
                    </div>
                }
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label" for="name">Name</label>
                        <InputText id="name" @bind-Value="_form.Name" class="form-control form-control-sm mono"
                                   placeholder="machine-01" />
                        <div class="form-text">UNS level 5 segment; lowercase letters, digits, dashes, up to 32 chars.</div>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label" for="machinecode">MachineCode</label>
                        <InputText id="machinecode" @bind-Value="_form.MachineCode" class="form-control form-control-sm mono"
                                   placeholder="machine_001" />
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label" for="line">UNS line</label>
                        <InputSelect id="line" @bind-Value="_form.UnsLineId" class="form-select form-select-sm">
                            <option value="">— pick a line —</option>
                            @foreach (var l in _lines)
                            {
                                <option value="@l.UnsLineId">@l.UnsAreaId / @l.UnsLineId &mdash; @l.Name</option>
                            }
                        </InputSelect>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label" for="driver">Driver instance</label>
                        <InputSelect id="driver" @bind-Value="_form.DriverInstanceId" class="form-select form-select-sm">
                            <option value="">— pick a driver —</option>
                            @foreach (var d in _drivers)
                            {
                                <option value="@d.DriverInstanceId">@d.DriverInstanceId &mdash; @d.Name (@d.DriverType)</option>
                            }
                        </InputSelect>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label" for="ztag">ZTag (ERP)</label>
                        <InputText id="ztag" @bind-Value="_form.ZTag" class="form-control form-control-sm" />
                        <div class="form-text">Unique fleet-wide via ExternalIdReservation.</div>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label" for="sap">SAPID</label>
                        <InputText id="sap" @bind-Value="_form.SAPID" class="form-control form-control-sm" />
                    </div>
                </div>
                <div class="mb-3">
                    <label class="form-label">Enabled</label>
                    <div class="form-check form-switch">
                        <InputCheckbox @bind-Value="_form.Enabled" class="form-check-input" />
                        <label class="form-check-label">Surface in deployments</label>
                    </div>
                </div>
            </div>
        </section>

        <section class="panel rise mt-3" style="animation-delay:.08s">
            <div class="panel-head">OPC 40010 identification (optional)</div>
            <div style="padding:1rem">
                <div class="row">
                    <div class="col-md-4 mb-3"><label class="form-label">Manufacturer</label><InputText @bind-Value="_form.Manufacturer" class="form-control form-control-sm" /></div>
                    <div class="col-md-4 mb-3"><label class="form-label">Model</label><InputText @bind-Value="_form.Model" class="form-control form-control-sm" /></div>
                    <div class="col-md-4 mb-3"><label class="form-label">SerialNumber</label><InputText @bind-Value="_form.SerialNumber" class="form-control form-control-sm" /></div>
                </div>
                <div class="row">
                    <div class="col-md-3 mb-3"><label class="form-label">HardwareRevision</label><InputText @bind-Value="_form.HardwareRevision" class="form-control form-control-sm" /></div>
                    <div class="col-md-3 mb-3"><label class="form-label">SoftwareRevision</label><InputText @bind-Value="_form.SoftwareRevision" class="form-control form-control-sm" /></div>
                    <div class="col-md-3 mb-3"><label class="form-label">Year of construction</label><InputNumber @bind-Value="_form.YearOfConstruction" class="form-control form-control-sm" /></div>
                    <div class="col-md-3 mb-3"><label class="form-label">AssetLocation</label><InputText @bind-Value="_form.AssetLocation" class="form-control form-control-sm" /></div>
                </div>
                <div class="row">
                    <div class="col-md-6 mb-3"><label class="form-label">ManufacturerUri</label><InputText @bind-Value="_form.ManufacturerUri" class="form-control form-control-sm mono" /></div>
                    <div class="col-md-6 mb-3"><label class="form-label">DeviceManualUri</label><InputText @bind-Value="_form.DeviceManualUri" class="form-control form-control-sm mono" /></div>
                </div>
            </div>
        </section>

        @if (!string.IsNullOrWhiteSpace(_error))
        {
            <div class="panel notice mt-3" style="border-color:var(--alert)">@_error</div>
        }

        <div class="mt-3 d-flex gap-2">
            <button type="submit" class="btn btn-primary" disabled="@_busy">
                @if (_busy) { <span class="spinner-border spinner-border-sm me-1"></span> }
                @(IsNew ? "Create" : "Save changes")
            </button>
            <a href="/clusters/@ClusterId/equipment" class="btn btn-outline-secondary">Cancel</a>
            @if (!IsNew)
            {
                <button type="button" class="btn btn-outline-danger ms-auto" @onclick="DeleteAsync" disabled="@_busy">Delete</button>
            }
        </div>
    </EditForm>
}

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

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

    private FormModel _form = new();
    private Equipment? _existing;
    private List<UnsLine> _lines = new();
    private List<DriverInstance> _drivers = new();
    private bool _loaded;
    private bool _busy;
    private string? _error;

    protected override async Task OnInitializedAsync()
    {
        await using var db = await DbFactory.CreateDbContextAsync();
        var areaIds = await db.UnsAreas.AsNoTracking()
            .Where(a => a.ClusterId == ClusterId).Select(a => a.UnsAreaId).ToListAsync();
        _lines = await db.UnsLines.AsNoTracking()
            .Where(l => areaIds.Contains(l.UnsAreaId))
            .OrderBy(l => l.UnsAreaId).ThenBy(l => l.UnsLineId)
            .ToListAsync();
        _drivers = await db.DriverInstances.AsNoTracking()
            .Where(d => d.ClusterId == ClusterId)
            .OrderBy(d => d.DriverInstanceId)
            .ToListAsync();

        if (!IsNew)
        {
            _existing = await db.Equipment.AsNoTracking()
                .FirstOrDefaultAsync(e => e.EquipmentId == EquipmentId);
            if (_existing is not null)
            {
                _form = new FormModel
                {
                    Name = _existing.Name,
                    MachineCode = _existing.MachineCode,
                    UnsLineId = _existing.UnsLineId,
                    DriverInstanceId = _existing.DriverInstanceId,
                    ZTag = _existing.ZTag,
                    SAPID = _existing.SAPID,
                    Manufacturer = _existing.Manufacturer,
                    Model = _existing.Model,
                    SerialNumber = _existing.SerialNumber,
                    HardwareRevision = _existing.HardwareRevision,
                    SoftwareRevision = _existing.SoftwareRevision,
                    YearOfConstruction = _existing.YearOfConstruction,
                    AssetLocation = _existing.AssetLocation,
                    ManufacturerUri = _existing.ManufacturerUri,
                    DeviceManualUri = _existing.DeviceManualUri,
                    Enabled = _existing.Enabled,
                    RowVersion = _existing.RowVersion,
                };
            }
        }
        _loaded = true;
    }

    private async Task SubmitAsync()
    {
        _busy = true; _error = null;
        try
        {
            if (string.IsNullOrEmpty(_form.UnsLineId)) { _error = "Pick a UNS line."; return; }
            if (string.IsNullOrEmpty(_form.DriverInstanceId)) { _error = "Pick a driver instance."; return; }

            await using var db = await DbFactory.CreateDbContextAsync();
            if (IsNew)
            {
                var uuid = Guid.NewGuid();
                var equipmentId = $"EQ-{uuid.ToString("N")[..12]}";
                if (await db.Equipment.AnyAsync(e => e.MachineCode == _form.MachineCode))
                { _error = $"MachineCode '{_form.MachineCode}' already exists in this fleet."; return; }
                db.Equipment.Add(new Equipment
                {
                    EquipmentId = equipmentId,
                    EquipmentUuid = uuid,
                    DriverInstanceId = _form.DriverInstanceId,
                    UnsLineId = _form.UnsLineId,
                    Name = _form.Name,
                    MachineCode = _form.MachineCode,
                    ZTag = string.IsNullOrWhiteSpace(_form.ZTag) ? null : _form.ZTag,
                    SAPID = string.IsNullOrWhiteSpace(_form.SAPID) ? null : _form.SAPID,
                    Manufacturer = _form.Manufacturer,
                    Model = _form.Model,
                    SerialNumber = _form.SerialNumber,
                    HardwareRevision = _form.HardwareRevision,
                    SoftwareRevision = _form.SoftwareRevision,
                    YearOfConstruction = _form.YearOfConstruction,
                    AssetLocation = _form.AssetLocation,
                    ManufacturerUri = _form.ManufacturerUri,
                    DeviceManualUri = _form.DeviceManualUri,
                    Enabled = _form.Enabled,
                });
            }
            else
            {
                var entity = await db.Equipment.FirstOrDefaultAsync(e => e.EquipmentId == EquipmentId);
                if (entity is null) { _error = "Row no longer exists."; return; }
                db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion;
                entity.DriverInstanceId = _form.DriverInstanceId;
                entity.UnsLineId = _form.UnsLineId;
                entity.Name = _form.Name;
                entity.MachineCode = _form.MachineCode;
                entity.ZTag = string.IsNullOrWhiteSpace(_form.ZTag) ? null : _form.ZTag;
                entity.SAPID = string.IsNullOrWhiteSpace(_form.SAPID) ? null : _form.SAPID;
                entity.Manufacturer = _form.Manufacturer;
                entity.Model = _form.Model;
                entity.SerialNumber = _form.SerialNumber;
                entity.HardwareRevision = _form.HardwareRevision;
                entity.SoftwareRevision = _form.SoftwareRevision;
                entity.YearOfConstruction = _form.YearOfConstruction;
                entity.AssetLocation = _form.AssetLocation;
                entity.ManufacturerUri = _form.ManufacturerUri;
                entity.DeviceManualUri = _form.DeviceManualUri;
                entity.Enabled = _form.Enabled;
            }
            await db.SaveChangesAsync();
            Nav.NavigateTo($"/clusters/{ClusterId}/equipment");
        }
        catch (DbUpdateConcurrencyException) { _error = "Another user changed this equipment while you were editing."; }
        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.Equipment.FirstOrDefaultAsync(e => e.EquipmentId == EquipmentId);
            if (entity is null) { Nav.NavigateTo($"/clusters/{ClusterId}/equipment"); return; }
            db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion;
            db.Equipment.Remove(entity);
            await db.SaveChangesAsync();
            Nav.NavigateTo($"/clusters/{ClusterId}/equipment");
        }
        catch (DbUpdateConcurrencyException) { _error = "Another user changed this equipment while you were viewing it."; }
        catch (Exception ex) { _error = $"Delete failed: {ex.Message}. Likely because tags or virtual tags reference this equipment — remove them first."; }
        finally { _busy = false; }
    }

    private sealed class FormModel
    {
        [Required, RegularExpression("^[a-z0-9-]{1,32}$", ErrorMessage = "Lowercase letters, digits, dashes only; max 32 chars.")]
        public string Name { get; set; } = "";
        [Required] public string MachineCode { get; set; } = "";
        [Required] public string UnsLineId { get; set; } = "";
        [Required] public string DriverInstanceId { get; set; } = "";
        public string? ZTag { get; set; }
        public string? SAPID { get; set; }
        public string? Manufacturer { get; set; }
        public string? Model { get; set; }
        public string? SerialNumber { get; set; }
        public string? HardwareRevision { get; set; }
        public string? SoftwareRevision { get; set; }
        public short? YearOfConstruction { get; set; }
        public string? AssetLocation { get; set; }
        public string? ManufacturerUri { get; set; }
        public string? DeviceManualUri { get; set; }
        public bool Enabled { get; set; } = true;
        public byte[] RowVersion { get; set; } = [];
    }
}
