namespace Nexus.Api.Models;

/// <summary>
/// Public, secret-free representation of Nexus' single OpenClaw connection profile.
/// OpenClaw remains the authority for agents, configuration and scheduled jobs.
/// </summary>
public sealed record OpenClawSetupStatusDto(
    string ProfileId,
    string State,
    bool ExperimentalBlocked,
    bool HasProfile,
    string? Endpoint,
    string? DiscoverySource,
    string AdoptionState,
    bool ManagementEnabled,
    string? RequiredVersion,
    string? GatewayVersion,
    int? ProtocolVersion,
    string? DeviceId,
    bool DeviceTokenConfigured,
    bool PairingRequired,
    string? PairingRequestId,
    IReadOnlyList<string> GrantedScopes,
    IReadOnlyList<string> AdvertisedMethods,
    string? CapabilityHash,
    int? Revision,
    DateTimeOffset? LastProbedAt,
    DateTimeOffset? LastVerifiedAt,
    DateTimeOffset? AdoptedAt,
    DateTimeOffset? UpdatedAt,
    string? Message,
    string? Recovery,
    DateTimeOffset CheckedAt);

public sealed record OpenClawDiscoveryRequest(bool IncludeMdns = false);

public sealed record OpenClawDiscoveryCandidateDto(
    string Endpoint,
    string Source,
    bool IsCurrentConnectorEndpoint,
    bool RequiresTlsFingerprint,
    bool IsValid,
    string? Reason);

public sealed record OpenClawDiscoveryDto(
    IReadOnlyList<OpenClawDiscoveryCandidateDto> Candidates,
    string MdnsState,
    string? Message,
    DateTimeOffset CheckedAt);

public sealed record ProbeOpenClawRequest(
    string Endpoint,
    string? TlsCertificateFingerprint = null)
{
    public override string ToString()
        => $"{nameof(ProbeOpenClawRequest)} {{ EndpointSupplied = {!string.IsNullOrWhiteSpace(Endpoint)}, TlsFingerprintSupplied = {!string.IsNullOrWhiteSpace(TlsCertificateFingerprint)} }}";
}

public sealed record OpenClawProbeDto(
    string Endpoint,
    string Source,
    bool IsCurrentConnectorEndpoint,
    bool Connected,
    string? GatewayVersion,
    string? RequiredVersion,
    bool VersionMatches,
    int? ProtocolVersion,
    string? DeviceId,
    bool PairingRequired,
    string? PairingRequestId,
    IReadOnlyList<string> GrantedScopes,
    IReadOnlyList<string> AdvertisedMethods,
    string CapabilityHash,
    bool CanAttach,
    bool LeastPrivilegeSatisfied,
    DateTimeOffset CheckedAt);

/// <summary>
/// Bootstrap credentials are deliberately request-only. The setup profile and all
/// response contracts contain no corresponding field, so they cannot be persisted
/// or returned to the browser.
/// </summary>
public sealed record AttachOpenClawRequest(
    string Endpoint,
    string DiscoverySource,
    string? TlsCertificateFingerprint = null,
    string? BootstrapToken = null,
    string? BootstrapSecretReference = null)
{
    public override string ToString()
        => $"{nameof(AttachOpenClawRequest)} {{ EndpointSupplied = {!string.IsNullOrWhiteSpace(Endpoint)}, DiscoverySourceSupplied = {!string.IsNullOrWhiteSpace(DiscoverySource)}, TlsFingerprintSupplied = {!string.IsNullOrWhiteSpace(TlsCertificateFingerprint)}, BootstrapCredentialSupplied = {!string.IsNullOrWhiteSpace(BootstrapToken) || !string.IsNullOrWhiteSpace(BootstrapSecretReference)} }}";
}

public sealed record VerifyOpenClawRequest(int ExpectedRevision);

public sealed record AdoptOpenClawRequest(int ExpectedRevision);

public sealed record SetOpenClawManagementRequest(
    bool Enabled,
    bool Confirmed,
    int ExpectedRevision);

public sealed record DeleteOpenClawConnectionRequest(
    string Endpoint,
    string? DeviceId,
    int ExpectedRevision);

public sealed record OpenClawAdoptionInventoryDto(
    int? AgentCount,
    int? AgentFileCount,
    int? CronJobCount,
    int? ModelCount,
    int? ChannelCount,
    int? NodeCount,
    IReadOnlyList<string> Diagnostics,
    DateTimeOffset CapturedAt);

public sealed record OpenClawSetupOperationDto<T>(
    bool Ok,
    string State,
    string Message,
    T? Data,
    string? Recovery,
    DateTimeOffset CompletedAt);

public static class OpenClawSetupStates
{
    public const string NotConfigured = "not_configured";
    public const string Discovered = "discovered";
    public const string Probed = "probed";
    public const string Attached = "attached";
    public const string Verified = "verified";
    public const string Adopted = "adopted";
    public const string Disconnected = "disconnected";
    public const string PairingRequired = "pairing_required";
    public const string ScopeUpgradeRequired = "scope_upgrade_required";
    public const string ExcessiveScope = "excessive_scope";
    public const string ExperimentalBlocked = "experimental_blocked";
    public const string DynamicEndpointUnsupported = "dynamic_endpoint_unsupported";
    public const string InvalidEndpoint = "invalid_endpoint";
    public const string InvalidRequest = "invalid_request";
    public const string ConcurrencyConflict = "concurrency_conflict";
    public const string NotFound = "not_found";
    public const string GatewayUnavailable = "gateway_unavailable";
    public const string Removed = "removed";
}

public static class OpenClawAdoptionStates
{
    public const string None = "none";
    public const string Attached = "attached";
    public const string Verified = "verified";
    public const string Adopted = "adopted";
}
