namespace Febe.Web.Connection;
/// State of the link to Instagram shown at the bottom of the sidebar and on the Collegamento page (spec §4.1).
public enum ConnectionState
{
/// No session in the browser profile: «Non collegato».
NotConnected,
/// Session cookie present: «Collegato».
Connected,
/// The engine stopped for a challenge or a disconnection: «Verifica richiesta».
VerificationRequired,
/// The browser is not available (starting, crashed, demo without browser).
BrowserUnavailable,
///
/// The session cookie is in the browser, but the raccolta «richieste» has no readable saved session to send it with
/// (issue #36, S4b): «Serve «Ho finito»». The engine waits until a copy is made.
///
SessionCopyMissing,
}
///
/// The saved session of the raccolta «richieste» as the Collegamento page shows it (issue #36, S4b): counts and names
/// only, never a value (rule 10).
///
/// , or .
/// When the copy was made; null when there is none or it is not known.
/// How many cookies it holds.
/// Values of the page it holds.
/// Values of the page the copy reads.
/// Names of the values it lacks.
/// doc_id it holds.
/// doc_id the copy reads.
/// Operations whose doc_id it lacks.
/// How many of those a configured operation sends (its requests do not leave).
public sealed record SavedSessionStatus(
string State,
DateTimeOffset? CopiedAt,
int Cookies,
int ValuesPresent,
int ValuesExpected,
IReadOnlyList MissingValues,
int DocIdsPresent,
int DocIdsExpected,
IReadOnlyList MissingDocIds,
int NeededMissing)
{
/// A readable saved session.
public const string Present = "present";
/// No saved session.
public const string None = "none";
/// A file that cannot be read: counts as none.
public const string Unreadable = "unreadable";
}
/// What the interface knows about the link to Instagram.
/// Current state.
/// Connected account (from the data the page received, hypothesis H10), when known.
/// Display name, when known.
/// True while Alberto is using the browser (login page, remote view, browser window): the engine is still.
/// Address shown in the remote view's address bar, without query string.
/// The saved session (raccolta «richieste»); null when this start keeps none.
public sealed record ConnectionStatus(
ConnectionState State,
string? Username,
string? DisplayName,
bool UserIsDriving,
string? CurrentUrl,
SavedSessionStatus? SavedSession = null);
/// Result of «Ho finito» on the Collegamento page.
/// True when the session cookie now exists.
/// Italian message for the page (what happened, what Febe did, what Alberto should do).
public sealed record FinishResult(bool Connected, string Message);
///
/// The link to Instagram (spec §4.1, §10.3 «Collegamento» and «Browser» pages). Implemented by the server/desktop
/// composition over the real browser host, and by the demo mode with a fake driven by the scene.
/// Opening the login page here is not an engine action (rule 7): Alberto is using the browser and the engine is still.
///
public interface IConnectionService
{
Task GetStatusAsync(CancellationToken ct);
///
/// Alberto starts using the browser: the engine stops acting (UserIsDriving), and when
/// is true the browser loads the real Instagram login page. Windows: the browser window is brought to the front.
///
Task BeginUserDrivingAsync(bool openLogin, CancellationToken ct);
/// «Ho finito»: checks that the session cookie exists (value never read), hides the browser, ends user driving.
Task FinishAsync(CancellationToken ct);
/// «Annulla»: ends user driving and puts the browser on the empty rest page.
Task CancelAsync(CancellationToken ct);
///
/// «Apri il browser» on Windows (spec §10.3, issue #52): the window comes to the front and holds the engine still
/// while it is on screen. A browser resting on the empty page first opens the start page (the site's home when
/// connected, the login page otherwise); with the login page in any case (the red strip
/// of a disconnection).
///
Task ShowBrowserAsync(bool openLogin, CancellationToken ct);
///
/// The Febe page that took the browser was left (another page, a reload, the tab closed) while the browser is a
/// window of its own (Windows, issue #52): only the page's hold ends. The window stays as it is, on screen and with
/// its page, until Alberto closes it or presses «Ho finito».
///
Task ReleaseAsync(CancellationToken ct);
///
/// The Febe page that took the browser was left (another page, a reload, the tab closed) while the browser is the
/// remote view (Docker, answer of Alberto of 25 November 2026, spec §6.0 «Quando si copia»): like closing the window
/// «Browser di Febe» on Windows, not like «Annulla». If within a moment no view opens again and no «Accedi» / «Usa il
/// browser» comes (a reload is not leaving), the session is copied while the page is still his, and then the page goes
/// back to the engine. By default, for a service without a saved session to copy, it is «Annulla».
///
Task LeavePageAsync(CancellationToken ct) => CancelAsync(ct);
/// «Scollega» (after confirmation in the UI): stops the engine and deletes the browser profile.
Task DisconnectAsync(CancellationToken ct);
}