using System.Text.RegularExpressions;
using Tiku.Application.Content;
using Tiku.Application.QuestionBanks;
using Tiku.Application.Security;
using Tiku.Infrastructure.Persistence;

namespace Tiku.Infrastructure.Content;

internal sealed record DirectContentServiceDependencies(
    IQuestionBankPersistence QuestionBankPersistence,
    IContentAssetPersistence ContentAssetPersistence,
    ICatalogPersistence CatalogPersistence,
    ILearningPersistence LearningPersistence,
    ITenantAdministrationPersistence TenantAdministrationPersistence,
    IJobsOperationsPersistence JobsOperationsPersistence,
    IQuestionReferenceService QuestionReferenceService,
    ICurrentAccessContext CurrentAccessContext,
    IFeatureAccessService FeatureAccessService);

internal abstract partial class DirectContentServiceBase(DirectContentServiceDependencies dependencies)
{
    protected IQuestionBankPersistence questionBankPersistence { get; } = dependencies.QuestionBankPersistence;
    protected IContentAssetPersistence contentAssetPersistence { get; } = dependencies.ContentAssetPersistence;
    protected ICatalogPersistence catalogPersistence { get; } = dependencies.CatalogPersistence;
    protected ILearningPersistence learningPersistence { get; } = dependencies.LearningPersistence;

    protected ITenantAdministrationPersistence tenantAdministrationPersistence { get; } =
        dependencies.TenantAdministrationPersistence;

    protected IJobsOperationsPersistence jobsOperationsPersistence { get; } = dependencies.JobsOperationsPersistence;
    protected IModulePersistence unitOfWork { get; } = dependencies.QuestionBankPersistence;
    protected IQuestionReferenceService questionReferenceService { get; } = dependencies.QuestionReferenceService;
    protected ICurrentAccessContext currentAccessContext { get; } = dependencies.CurrentAccessContext;
    protected IFeatureAccessService featureAccessService { get; } = dependencies.FeatureAccessService;

    protected const int DefaultLimit = 100;
    protected const int MaxLimit = 1000;

    protected static readonly string[] ContentPermissions =
    [
        BackendPermissions.TenantContentManage,
        BackendPermissions.TenantVocabularyManage,
        BackendPermissions.TenantHandbookManage,
        BackendPermissions.TenantVideoManage,
        BackendPermissions.TenantScorelineManage,
        BackendPermissions.TenantSiteContentManage,
        BackendPermissions.TenantJobManage
    ];

    protected static readonly Regex ScorelineFieldKeyRegex = new("^[A-Za-z][A-Za-z0-9_]{0,63}$", RegexOptions.Compiled);

    protected static readonly HashSet<string> SupportedImportTypes = new(StringComparer.OrdinalIgnoreCase)
    {
        "questions",
        "vocabulary",
        "handbook",
        "scoreline",
        "videos"
    };
}
