using AIStudio.Settings;
using AIStudio.Tools.PluginSystem;
namespace AIStudio.Tools.Mail;
///
/// Which mail servers AI Studio may connect to, and whose mailboxes the AI may read.
///
///
/// Read anew for every decision rather than kept: both parts come from configuration plugins, which
/// may change while AI Studio runs.
///
/// Whether the organization allows only its own mail servers, see DataMailboxes.AllowOnlyOrganizationMailServers.
/// The hosts of the mail servers which the configuration plugins offer.
public sealed record MailServerPolicy(bool AllowsOnlyOrganizationMailServers, IReadOnlyList OrganizationHosts)
{
///
/// Every server: what holds as long as no organization rules otherwise.
///
public static readonly MailServerPolicy ANY_SERVER = new(false, []);
///
/// Reads the policy which holds right now.
///
/// The settings, which hold the switch of the organization.
/// The policy.
public static MailServerPolicy Read(SettingsManager settingsManager) => new(
settingsManager.ConfigurationData.MailboxSettings.AllowOnlyOrganizationMailServers,
PluginFactory.GetMailboxProviders().Select(provider => provider.Host).ToList());
///
/// Whether AI Studio may connect to a server, and the AI read the mailboxes on it.
///
///
/// Port and encryption make no difference: a mailbox on the right host but another port is
/// still on the server of the organization.
///
/// The host of the server.
/// True when the server is allowed.
public bool IsAllowed(string host) => !this.AllowsOnlyOrganizationMailServers || this.OrganizationHosts.Any(organizationHost => MailServerHosts.AreSame(organizationHost, host));
}