using System.Collections.Frozen;
using Febe.Core.Collection.Privacy;
namespace Febe.Core.Collection.Requests;
///
/// Names of the values of the case that a may carry (spec §6.0: «variabili con i soli valori
/// del caso»): who or what is asked for, and where a list goes on. A closed list: everything else a request carries
/// comes from the catalog () or from the saved session, never from the caller.
///
public static class CaseValues
{
/// Numeric identifier of an account on the site.
public const string AccountId = "account_id";
/// User name of an account.
public const string Username = "username";
/// Cursor of the next page, as the previous answer gave it.
public const string Cursor = "cursor";
/// Text typed in the search box.
public const string SearchText = "search_text";
/// Numeric identifier of a content (post, reel, story).
public const string MediaId = "media_id";
/// Short code of a post, as in its address.
public const string Code = "code";
/// Identifier of a highlight (collection of stories).
public const string HighlightId = "highlight_id";
/// Every name of the list.
public static IReadOnlySet All { get; } = new[] { AccountId, Username, Cursor, SearchText, MediaId, Code, HighlightId }
.ToFrozenSet(StringComparer.Ordinal);
/// The shape a value takes in a logged path (): never the value itself.
public static string ShapeOf(string name) => name is AccountId or MediaId ? PathPatterns.Number : PathPatterns.Text;
///
/// True when has the form of the value : digits for identifiers,
/// the characters of a user name, a short code or a highlight; for a cursor or a searched text, no control
/// character. So a value can never carry a path, a query or a step up into the request it fills.
///
public static bool HasForm(string name, string value)
{
ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(value);
return name switch
{
AccountId or MediaId => value.Length <= 32 && value.Length > 0 && value.All(char.IsAsciiDigit),
Username => value.Length <= 64 && value.Trim('.').Length > 0 && value.All(c => char.IsAsciiLetterOrDigit(c) || c is '.' or '_'),
Code => value.Length is > 0 and <= 64 && value.All(c => char.IsAsciiLetterOrDigit(c) || c is '_' or '-'),
HighlightId => value.Length is > 0 and <= 64 && value.All(c => char.IsAsciiLetterOrDigit(c) || c is '_' or '-' or ':'),
Cursor or SearchText => value.Length is > 0 and <= 4096 && !value.Any(char.IsControl),
_ => false,
};
}
}
///
/// One request to the site (issue #36, spec §6.0): which operation of the catalog, and the values of the case by name.
/// Nothing else: method, path, headers, body and constants come from richieste.<sito>.json, the session
/// values from the saved session. Callers never pass raw headers, bodies or addresses.
///
///
/// Its text () names the operation and the names of the values, never the values: user names,
/// identifiers, cursors and searched texts stay out of logs (rule 10).
///
public sealed class SiteRequest
{
/// Name of the catalog operation ().
/// Values of the case, by a name of .
/// An unknown value name, an empty value, or one without its form ().
public SiteRequest(string operation, IReadOnlyDictionary? values = null)
{
ArgumentException.ThrowIfNullOrWhiteSpace(operation);
var copy = new Dictionary(StringComparer.Ordinal);
foreach (var (name, value) in values ?? new Dictionary())
{
if (!CaseValues.All.Contains(name))
{
throw new ArgumentException($"Valore «{name}» sconosciuto: una richiesta porta solo {string.Join(", ", CaseValues.All.Order(StringComparer.Ordinal))}.", nameof(values));
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentException($"Il valore «{name}» è vuoto.", nameof(values));
}
if (!CaseValues.HasForm(name, value))
{
// Never the value in the message: it may be a user name or a searched text (rule 10).
throw new ArgumentException($"Il valore «{name}» non ha la forma attesa (cifre per gli identificativi; mai un percorso o una query).", nameof(values));
}
copy[name] = value;
}
Operation = operation.Trim();
Values = copy.ToFrozenDictionary(StringComparer.Ordinal);
}
/// Name of the catalog operation.
public string Operation { get; }
/// Values of the case, by name (never logged).
public IReadOnlyDictionary Values { get; }
/// A request for with the given values.
public static SiteRequest For(string operation, params (string Name, string Value)[] values) =>
new(operation, values.ToDictionary(v => v.Name, v => v.Value, StringComparer.Ordinal));
/// The operation and the names of the values, never the values.
public override string ToString() =>
Values.Count == 0
? $"Richiesta «{Operation}»"
: $"Richiesta «{Operation}» (valori: {string.Join(", ", Values.Keys.Order(StringComparer.Ordinal))})";
}