namespace Tiku.Application.Security;

public static class SaasFeatureCatalog
{
    public const string CoreBackoffice = "core.backoffice";
    public const string PrivateQuestionBank = "question_bank.private";
    public const string Practice = "learning.practice";
    public const string Assignment = "learning.assignment";
    public const string Exam = "learning.exam";
    public const string Vocabulary = "content.vocabulary";
    public const string Handbook = "content.handbook";
    public const string Video = "content.video";
    public const string Scoreline = "content.scoreline";
    public const string SiteContent = "marketing.site_content";
    public const string StudentManagement = "student.management";
    public const string StudentStore = "commerce.student_store";
    public const string Crm = "crm.followup";
    public const string ReferralCommission = "growth.referral_commission";
    public const string TeacherAi = "ai.teacher_assistant";

    public static readonly IReadOnlySet<string> All = new HashSet<string>(StringComparer.Ordinal)
    {
        CoreBackoffice,
        PrivateQuestionBank,
        Practice,
        Assignment,
        Exam,
        Vocabulary,
        Handbook,
        Video,
        Scoreline,
        SiteContent,
        StudentManagement,
        StudentStore,
        Crm,
        ReferralCommission,
        TeacherAi
    };

    public static string? ResolveContentImportFeature(string? importType)
    {
        var normalized = importType?.Trim().ToLowerInvariant().Replace('-', '_');
        return normalized switch
        {
            "question" or "questions" or "question_bank" or "question_banks" => PrivateQuestionBank,
            "vocabulary" or "vocabulary_unit" or "vocabulary_units" or "vocabulary_word"
                or "vocabulary_words" => Vocabulary,
            "handbook" or "handbook_subject" or "handbook_subjects" or "handbook_chapter" or "handbook_chapters" or
                "handbook_entry" or "handbook_entries" => Handbook,
            "scoreline" or "scorelines" => Scoreline,
            "video" or "videos" => Video,
            _ => null
        };
    }
}

public static class PermissionModuleCatalog
{
    public static readonly IReadOnlyDictionary<string, string?> RequiredFeatures =
        new Dictionary<string, string?>(StringComparer.Ordinal)
        {
            ["tenant_dashboard"] = null,
            ["tenant_staff"] = null,
            ["tenant_settings"] = null,
            ["tenant_provider"] = null,
            ["tenant_job"] = null,
            ["tenant_billing"] = null,
            ["tenant_student"] = SaasFeatureCatalog.StudentManagement,
            ["tenant_question_bank"] = SaasFeatureCatalog.PrivateQuestionBank,
            ["tenant_vocabulary"] = SaasFeatureCatalog.Vocabulary,
            ["tenant_handbook"] = SaasFeatureCatalog.Handbook,
            ["tenant_video"] = SaasFeatureCatalog.Video,
            ["tenant_scoreline"] = SaasFeatureCatalog.Scoreline,
            ["tenant_site_content"] = SaasFeatureCatalog.SiteContent,
            ["tenant_commerce"] = SaasFeatureCatalog.StudentStore,
            ["tenant_crm"] = SaasFeatureCatalog.Crm,
            ["tenant_commission"] = SaasFeatureCatalog.ReferralCommission,
            ["platform_dashboard"] = null,
            ["platform_tenant"] = null,
            ["platform_staff"] = null,
            ["platform_content"] = null,
            ["platform_audit"] = null,
            ["platform_billing"] = null,
            ["platform_crm"] = null,
            ["platform_sms"] = null,
            ["platform_payment"] = null,
            ["platform_operations"] = null,
            ["platform_governance"] = null,
            ["commerce"] = SaasFeatureCatalog.StudentStore
        };

    public static string ResolvePermissionModuleCode(string permissionCode)
    {
        return permissionCode switch
        {
            BackendPermissions.TenantDashboardView => "tenant_dashboard",
            BackendPermissions.TenantStaffManage or BackendPermissions.TenantRoleManage => "tenant_staff",
            BackendPermissions.TenantStudentManage => "tenant_student",
            BackendPermissions.TenantContentManage => "tenant_question_bank",
            BackendPermissions.TenantVocabularyManage => "tenant_vocabulary",
            BackendPermissions.TenantHandbookManage => "tenant_handbook",
            BackendPermissions.TenantVideoManage => "tenant_video",
            BackendPermissions.TenantScorelineManage => "tenant_scoreline",
            BackendPermissions.TenantSiteContentManage => "tenant_site_content",
            BackendPermissions.TenantSettingsManage => "tenant_settings",
            BackendPermissions.TenantProviderManage => "tenant_provider",
            BackendPermissions.TenantCommerceOperate => "tenant_commerce",
            BackendPermissions.TenantCrmManage => "tenant_crm",
            BackendPermissions.TenantCommissionManage => "tenant_commission",
            BackendPermissions.TenantJobManage => "tenant_job",
            BackendPermissions.TenantBillingManage => "tenant_billing",
            BackendPermissions.PlatformDashboardView => "platform_dashboard",
            BackendPermissions.PlatformTenantManage => "platform_tenant",
            BackendPermissions.PlatformStaffManage or BackendPermissions.PlatformRoleManage => "platform_staff",
            BackendPermissions.PlatformQuestionBankManage => "platform_content",
            BackendPermissions.PlatformAuditView => "platform_audit",
            BackendPermissions.PlatformBillingNotification => "platform_billing",
            BackendPermissions.PlatformSaasCatalogManage or BackendPermissions.PlatformSaasBillingManage =>
                "platform_billing",
            BackendPermissions.PlatformCrmRead or BackendPermissions.PlatformCrmWrite => "platform_crm",
            BackendPermissions.PlatformSmsRead or BackendPermissions.PlatformSmsWrite => "platform_sms",
            BackendPermissions.PlatformPaymentRead or BackendPermissions.PlatformPaymentWrite => "platform_payment",
            BackendPermissions.PlatformOperationsView or BackendPermissions.PlatformOperationsManage =>
                "platform_operations",
            BackendPermissions.PlatformApprovalView or BackendPermissions.PlatformApprovalDecide
                or BackendPermissions.PlatformApprovalPolicyManage => "platform_governance",
            BackendPermissions.PlatformConfigurationManage or BackendPermissions.PlatformNotificationManage =>
                "platform_governance",
            _ when permissionCode.StartsWith("commerce:", StringComparison.Ordinal) => "commerce",
            _ => throw new ArgumentOutOfRangeException(nameof(permissionCode), permissionCode,
                "Permission module mapping is missing.")
        };
    }
}

public static class SaasQuotaMetricCatalog
{
    public const string StaffCount = "staff.count";
    public const string StudentCount = "student.count";
    public const string PrivateQuestionCount = "private_question.count";
    public const string StorageBytes = "storage.bytes";
    public const string ImportCount = "import.count";
    public const string ExportCount = "export.count";
    public const string SmsCount = "sms.count";
    public const string AiCallCount = "ai.call.count";

    public static readonly IReadOnlySet<string> All = new HashSet<string>(StringComparer.Ordinal)
    {
        StaffCount,
        StudentCount,
        PrivateQuestionCount,
        StorageBytes,
        ImportCount,
        ExportCount,
        SmsCount,
        AiCallCount
    };
}