namespace ZB.MOM.WW.ScadaBridge.CentralUI.Services;

/// <summary>
/// Outcome of a single move-data-connection command dispatch. Wraps either success
/// or a human-readable guard error, so the move dialog can render an inline result
/// rather than reasoning about transport exceptions or the three management response
/// shapes.
/// </summary>
/// <param name="Success">Whether the move succeeded.</param>
/// <param name="Error">A human-readable error message when <paramref name="Success"/> is <c>false</c>; otherwise <c>null</c>.</param>
public record DataConnectionMoveResult(bool Success, string? Error)
{
    /// <summary>Creates a successful result.</summary>
    /// <returns>A successful <see cref="DataConnectionMoveResult"/>.</returns>
    public static DataConnectionMoveResult Ok() => new(true, null);

    /// <summary>Creates a failed result carrying <paramref name="error"/>.</summary>
    /// <param name="error">The human-readable failure (typically a server guard message).</param>
    /// <returns>A failed <see cref="DataConnectionMoveResult"/>.</returns>
    public static DataConnectionMoveResult Fail(string error) => new(false, error);
}

/// <summary>
/// CentralUI facade over the move-data-connection management command. It
/// dispatches the strongly-typed <c>MoveDataConnectionCommand</c> to the central
/// <c>ManagementActor</c> through the in-process <c>ManagementActorHolder</c> seam —
/// the SAME Ask path the HTTP <c>/management</c> endpoint uses — so the server remains
/// the single enforcer of the Designer role gate and every move guard (target site
/// exists, no name collision at the target, no instance binding references it, no
/// name-based native-alarm-source references). The UI re-implements none of that: a
/// move is a command submitted to the server, and any guard failure comes back as a
/// classified error.
/// </summary>
/// <remarks>
/// The current Blazor principal is projected to an <c>AuthenticatedUser</c> so the
/// server's role gate runs against the real identity. <c>ManagementUnauthorized</c> /
/// <c>ManagementError</c> / transport faults collapse into a typed
/// <see cref="DataConnectionMoveResult"/> so the caller renders an inline outcome
/// rather than throwing. Mirrors <c>ISecuredWriteService</c>.
/// </remarks>
public interface IDataConnectionMoveService
{
    /// <summary>
    /// Moves the connection identified by <paramref name="connectionId"/> to
    /// <paramref name="targetSiteId"/> via the guard-running ManagementActor.
    /// </summary>
    /// <param name="connectionId">Primary key of the connection to move.</param>
    /// <param name="targetSiteId">Primary key of the destination site.</param>
    /// <param name="cancellationToken">Cancellation token.</param>
    /// <returns>A task that resolves to a <see cref="DataConnectionMoveResult"/> — success, or a classified guard / transport failure.</returns>
    Task<DataConnectionMoveResult> MoveAsync(
        int connectionId,
        int targetSiteId,
        CancellationToken cancellationToken = default);
}
