namespace MemberCenter.Application.Constants;

public static class AdminPermissions
{
    public const string AdminRole = "admin";
    public const string SuperuserRole = "superuser";
    public const string TenantManagerRole = "tenant_manager";

    public const string Home = "admin.home";
    public const string Accounts = "admin.accounts";
    public const string AccountsIndex = "admin.accounts.index";
    public const string AccountsSetAdmin = "admin.accounts.set_admin";
    public const string AccountsSetDisabled = "admin.accounts.set_disabled";
    public const string AccountsResetPassword = "admin.accounts.reset_password";
    public const string Tenants = "admin.tenants";
    public const string TenantsIndex = "admin.tenants.index";
    public const string TenantsCreate = "admin.tenants.create";
    public const string TenantsEdit = "admin.tenants.edit";
    public const string TenantsDelete = "admin.tenants.delete";
    public const string NewsletterLists = "admin.newsletter_lists";
    public const string NewsletterListsIndex = "admin.newsletter_lists.index";
    public const string NewsletterListsCreate = "admin.newsletter_lists.create";
    public const string NewsletterListsEdit = "admin.newsletter_lists.edit";
    public const string NewsletterListsDelete = "admin.newsletter_lists.delete";
    public const string Subscriptions = "admin.subscriptions";
    public const string SubscriptionsIndex = "admin.subscriptions.index";
    public const string SubscriptionsExport = "admin.subscriptions.export";
    public const string OAuthClients = "admin.oauth_clients";
    public const string OAuthClientsIndex = "admin.oauth_clients.index";
    public const string OAuthClientsCreate = "admin.oauth_clients.create";
    public const string OAuthClientsEdit = "admin.oauth_clients.edit";
    public const string OAuthClientsDelete = "admin.oauth_clients.delete";
    public const string OAuthClientsRotateSecret = "admin.oauth_clients.rotate_secret";
    public const string AuditLogs = "admin.audit_logs";
    public const string AuditLogsIndex = "admin.audit_logs.index";
    public const string Security = "admin.security";
    public const string SecurityIndex = "admin.security.index";
    public const string SecuritySave = "admin.security.save";
    public const string SecurityTestEmail = "admin.security.test_email";
    public const string Blacklist = "admin.blacklist";
    public const string BlacklistIndex = "admin.blacklist.index";
    public const string BlacklistCreate = "admin.blacklist.create";

    public static readonly IReadOnlyList<AdminPermissionDefinition> All =
    [
        new(Home, "Overview", "Open the admin overview."),

        new(Accounts, "Accounts", "Open account governance."),
        new(AccountsIndex, "Accounts / View", "View account governance."),
        new(AccountsSetAdmin, "Accounts / Grant admin", "Grant or remove admin role.", SuperuserOnly: true),
        new(AccountsSetDisabled, "Accounts / Disable account", "Disable or enable member accounts."),
        new(AccountsResetPassword, "Accounts / Reset password", "Reset member passwords."),

        new(Tenants, "Tenants", "Open tenant management."),
        new(TenantsIndex, "Tenants / View", "View tenants."),
        new(TenantsCreate, "Tenants / Create", "Create tenants."),
        new(TenantsEdit, "Tenants / Edit", "Edit tenants."),
        new(TenantsDelete, "Tenants / Delete", "Delete tenants."),

        new(NewsletterLists, "Newsletter lists", "Open newsletter list management."),
        new(NewsletterListsIndex, "Newsletter lists / View", "View newsletter lists."),
        new(NewsletterListsCreate, "Newsletter lists / Create", "Create newsletter lists."),
        new(NewsletterListsEdit, "Newsletter lists / Edit", "Edit newsletter lists."),
        new(NewsletterListsDelete, "Newsletter lists / Delete", "Delete newsletter lists."),

        new(Subscriptions, "Subscriptions", "Open subscription management."),
        new(SubscriptionsIndex, "Subscriptions / View", "View subscriptions."),
        new(SubscriptionsExport, "Subscriptions / Export", "Export subscriptions."),

        new(OAuthClients, "OAuth clients", "Open OAuth client management."),
        new(OAuthClientsIndex, "OAuth clients / View", "View OAuth clients."),
        new(OAuthClientsCreate, "OAuth clients / Create", "Create OAuth clients."),
        new(OAuthClientsEdit, "OAuth clients / Edit", "Edit OAuth clients."),
        new(OAuthClientsDelete, "OAuth clients / Delete", "Delete OAuth clients."),
        new(OAuthClientsRotateSecret, "OAuth clients / Rotate secret", "Rotate OAuth client secrets."),

        new(AuditLogs, "Audit logs", "Open audit logs."),
        new(AuditLogsIndex, "Audit logs / View", "View audit logs."),

        new(Security, "Security", "Open security settings."),
        new(SecurityIndex, "Security / View", "View security settings."),
        new(SecuritySave, "Security / Save", "Save security settings."),
        new(SecurityTestEmail, "Security / Test email", "Send security settings test email."),

        new(Blacklist, "Blacklist", "Open email blacklist."),
        new(BlacklistIndex, "Blacklist / View", "View email blacklist."),
        new(BlacklistCreate, "Blacklist / Create", "Add or update email blacklist entries.")
    ];

    public static readonly IReadOnlySet<string> TenantManagerPermissions = new HashSet<string>(StringComparer.Ordinal)
    {
        Home,
        Tenants,
        TenantsIndex,
        NewsletterLists,
        NewsletterListsIndex,
        Subscriptions,
        SubscriptionsIndex
    };
}

public sealed record AdminPermissionDefinition(
    string Key,
    string Name,
    string Description,
    bool SuperuserOnly = false);
