using System.Diagnostics.Metrics;
using Microsoft.Extensions.Logging;
using Tiku.Application.Learning;
using Tiku.Application.QuestionBanks;
using Tiku.Application.Security;
using Tiku.Infrastructure.Persistence;

namespace Tiku.Infrastructure.Learning;

internal sealed record LearningServiceDependencies(
    ILearningPersistence LearningPersistence,
    IQuestionBankPersistence QuestionBankPersistence,
    IContentAssetPersistence ContentAssetPersistence,
    IIdentityPersistence IdentityPersistence,
    IQuestionReferenceService QuestionReferenceService,
    IPublicQuestionAccessPolicy PublicQuestionAccessPolicy,
    ITenantExecutionScope TenantExecutionScope,
    ILogger<LearningActivityServiceBase> Logger);

internal abstract partial class LearningActivityServiceBase(LearningServiceDependencies dependencies)
{
    protected ILearningPersistence learningPersistence { get; } = dependencies.LearningPersistence;
    protected IQuestionBankPersistence questionBankPersistence { get; } = dependencies.QuestionBankPersistence;
    protected IContentAssetPersistence contentAssetPersistence { get; } = dependencies.ContentAssetPersistence;
    protected IIdentityPersistence identityPersistence { get; } = dependencies.IdentityPersistence;
    protected IModulePersistence unitOfWork { get; } = dependencies.LearningPersistence;
    protected IQuestionReferenceService questionReferenceService { get; } = dependencies.QuestionReferenceService;
    protected IPublicQuestionAccessPolicy publicQuestionAccessPolicy { get; } = dependencies.PublicQuestionAccessPolicy;
    protected ITenantExecutionScope tenantExecutionScope { get; } = dependencies.TenantExecutionScope;
    protected ILogger<LearningActivityServiceBase> logger { get; } = dependencies.Logger;

    protected const int DefaultLimit = 100;
    protected const int MaxLimit = 500;
    protected static readonly Meter LearningMeter = new("Tiku.Learning");

    protected static readonly Counter<long> IdempotencyReplays =
        LearningMeter.CreateCounter<long>("tiku.learning.idempotency.replays");

    protected static readonly Counter<long> AnswerConflicts =
        LearningMeter.CreateCounter<long>("tiku.learning.answer.conflicts");

    protected static readonly Counter<long> SubmissionConflicts =
        LearningMeter.CreateCounter<long>("tiku.learning.submission.conflicts");

    protected static readonly Counter<long> ScoringFailures =
        LearningMeter.CreateCounter<long>("tiku.learning.scoring.failures");
}
