using System.ComponentModel.DataAnnotations;
using ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.PlcFamilies;

namespace ZB.MOM.WW.OtOpcUa.Driver.AbLegacy;

/// <summary>
///     AB Legacy (PCCC) driver configuration. One instance supports N devices (SLC 500 /
///     MicroLogix / PLC-5 / LogixPccc). Per plan decision #41 AbLegacy ships separately from
///     AbCip because PCCC's file-based addressing (<c>N7:0</c>) and Logix's symbolic addressing
///     (<c>Motor1.Speed</c>) pull the abstraction in different directions.
/// </summary>
public sealed class AbLegacyDriverOptions
{
    /// <summary>Gets or sets the list of PCCC devices to connect to.</summary>
    public IReadOnlyList<AbLegacyDeviceOptions> Devices { get; init; } = [];

    /// <summary>Gets or sets the list of PCCC tag definitions.</summary>
    public IReadOnlyList<AbLegacyTagDefinition> Tags { get; init; } = [];

    /// <summary>Gets or sets the probe (connectivity check) options.</summary>
    public AbLegacyProbeOptions Probe { get; init; } = new();

    /// <summary>Gets or sets the default timeout for read/write operations.</summary>
    public TimeSpan Timeout { get; init; } = TimeSpan.FromSeconds(2);

    /// <summary>
    ///     Timeout for the AdminUI Test Connect probe, in seconds. The AdminUI clamps to a
    ///     60s server-side maximum; this default is what the form pre-fills for new instances.
    /// </summary>
    [Display(Name = "Probe timeout (seconds)", Description = "Connection test timeout. Default 5s.", GroupName = "Diagnostics")]
    [Range(1, 60)]
    public int ProbeTimeoutSeconds { get; init; } = 5;
}

public sealed record AbLegacyDeviceOptions(
    string HostAddress,
    AbLegacyPlcFamily PlcFamily = AbLegacyPlcFamily.Slc500,
    string? DeviceName = null);

/// <summary>
///     One PCCC-backed OPC UA variable. <paramref name="Address"/> is the canonical PCCC
///     file-address string that parses via <c>AbLegacyAddress.TryParse</c>.
/// </summary>
public sealed record AbLegacyTagDefinition(
    string Name,
    string DeviceHostAddress,
    string Address,
    AbLegacyDataType DataType,
    bool Writable = true,
    bool WriteIdempotent = false);

public sealed class AbLegacyProbeOptions
{
    /// <summary>Gets or sets a value indicating whether connectivity probing is enabled.</summary>
    public bool Enabled { get; init; } = true;

    /// <summary>Gets or sets the interval between probe attempts.</summary>
    public TimeSpan Interval { get; init; } = TimeSpan.FromSeconds(5);

    /// <summary>Gets or sets the timeout for each probe operation.</summary>
    public TimeSpan Timeout { get; init; } = TimeSpan.FromSeconds(2);

    /// <summary>Gets or sets the probe address (defaults to <c>S:0</c> — status file, first word).</summary>
    public string? ProbeAddress { get; init; } = "S:0";
}
