using System;
using System.Net.Http;
using System.Text.Json;
using CarpaNet.Identity;
using CarpaNet.OAuth.Crypto;
using CarpaNet.OAuth.Storage;
using Microsoft.Extensions.Logging;
namespace CarpaNet.OAuth;
///
/// Configuration for the ATProto OAuth client.
///
public sealed class OAuthClientConfig
{
///
/// The client ID. For web apps, this is the URL where client metadata is hosted.
/// For loopback clients, use .
///
public string ClientId { get; set; } = string.Empty;
///
/// The redirect URI to use for OAuth callbacks.
///
public string RedirectUri { get; set; } = string.Empty;
///
/// The scope to request (default: "atproto").
///
public string Scope { get; set; } = "atproto";
///
/// The HttpClient to use for requests. If not provided, a new one will be created.
///
public HttpClient? HttpClient { get; set; }
///
/// The state store for OAuth authorization state. If not provided, an in-memory store will be used.
///
public IOAuthStateStore? StateStore { get; set; }
///
/// The session store for OAuth sessions. If not provided, an in-memory store will be used.
///
public IOAuthSessionStore? SessionStore { get; set; }
///
/// The private key for client authentication (if using private_key_jwt).
///
public DPoPKeyPair? ClientKey { get; set; }
///
/// The state expiration time (default: 10 minutes).
///
public TimeSpan StateExpiration { get; set; } = TimeSpan.FromMinutes(10);
///
/// The token refresh buffer (default: 30 seconds).
/// Tokens will be refreshed this amount of time before they expire.
///
public TimeSpan RefreshBuffer { get; set; } = TimeSpan.FromSeconds(30);
///
/// The JSON serializer options to use for request/response serialization.
/// Should include a source-generated IJsonTypeInfoResolver for AOT compatibility.
/// If not provided, a reflection-based fallback will be used.
///
public JsonSerializerOptions? JsonOptions { get; set; }
///
/// Gets or sets the list of labeler DIDs whose labels should be included in responses.
/// When set, the atproto-accept-labelers header is added to requests.
///
public IReadOnlyList? LabelerDids { get; set; }
///
/// Gets or sets the logger factory for diagnostic logging.
/// When null, logging is disabled (NullLoggerFactory is used internally).
///
public ILoggerFactory? LoggerFactory { get; set; }
///
/// Gets or sets the identity resolver for handle/DID resolution.
/// If null, a new resolver will be created if needed.
///
public IdentityResolver? IdentityResolver { get; set; }
///
/// Creates a loopback client ID for native/desktop applications.
///
/// The local port for the callback server.
/// Optional state parameter.
/// A loopback client ID.
public static string CreateLoopbackClientId(int port, string? state = null)
{
var stateParam = string.IsNullOrEmpty(state) ? "" : $"&state={Uri.EscapeDataString(state)}";
return $"http://127.0.0.1:{port}/?{stateParam}";
}
///
/// Creates a loopback redirect URI.
///
/// The local port for the callback server.
/// A loopback redirect URI.
public static string CreateLoopbackRedirectUri(int port)
{
return $"http://127.0.0.1:{port}/callback";
}
}