using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Tiku.Domain.Catalog;
using Tiku.Domain.Commerce;
using Tiku.Domain.Content;
using Tiku.Domain.Growth;
using Tiku.Domain.Identity;
using Tiku.Domain.Import;
using Tiku.Domain.Learning;
using Tiku.Domain.Operations;
using Tiku.Domain.Platform;
using Tiku.Domain.QuestionBanks;
using Tiku.Domain.Tenancy;

namespace Tiku.Infrastructure.Persistence;

/// <summary>
/// Technical unit-of-work surface shared by the module persistence capabilities.
/// Business services must consume one or more owned module capabilities instead of TikuDbContext.
/// </summary>
public interface IModulePersistence
{
    long SaveVersion { get; }
    DatabaseFacade Database { get; }
    ChangeTracker ChangeTracker { get; }
    DbSet<TEntity> Set<TEntity>() where TEntity : class;
    EntityEntry Entry(object entity);
    EntityEntry<TEntity> Entry<TEntity>(TEntity entity) where TEntity : class;
    int SaveChanges();
    Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
}

public interface IIdentityPersistence : IModulePersistence
{
    DbSet<User> Users { get; }
    DbSet<UserIdentity> UserIdentities { get; }
    DbSet<TenantMembership> TenantMemberships { get; }
    DbSet<SmsVerificationCode> SmsVerificationCodes { get; }
    DbSet<SmsChannel> SmsChannels { get; }
    DbSet<SmsTemplate> SmsTemplates { get; }
    DbSet<SmsSendLog> SmsSendLogs { get; }
    DbSet<AuthLoginEvent> AuthLoginEvents { get; }
    DbSet<AuthSession> AuthSessions { get; }
    DbSet<AuthChallenge> AuthChallenges { get; }
    DbSet<SmsSendRateLimit> SmsSendRateLimits { get; }
}

public interface ITenancyPersistence : IModulePersistence
{
    DbSet<Tenant> Tenants { get; }
    DbSet<TenantDomain> TenantDomains { get; }
    DbSet<TenantBranding> TenantBrandings { get; }
    DbSet<TenantSettings> TenantSettings { get; }
    DbSet<TenantAuthPolicy> TenantAuthPolicies { get; }
    DbSet<TenantFrontendConfig> TenantFrontendConfigs { get; }
    DbSet<TenantExternalProvider> TenantExternalProviders { get; }
    DbSet<TenantSecret> TenantSecrets { get; }
}

public interface ITenantAdministrationPersistence : IModulePersistence
{
    DbSet<TenantClass> TenantClasses { get; }
    DbSet<TenantClassMember> TenantClassMembers { get; }
    DbSet<TenantStudentNote> TenantStudentNotes { get; }
    DbSet<TenantStudentFollowup> TenantStudentFollowups { get; }
    DbSet<StudentProfile> StudentProfiles { get; }
    DbSet<Badge> Badges { get; }
    DbSet<UserBadge> UserBadges { get; }
    DbSet<TenantContentNotification> TenantContentNotifications { get; }
    DbSet<TenantThemeTemplate> TenantThemeTemplates { get; }
    DbSet<TenantThemeConfig> TenantThemeConfigs { get; }
    DbSet<TenantBillingProfile> TenantBillingProfiles { get; }
    DbSet<TenantBillingPolicy> TenantBillingPolicies { get; }
    DbSet<TenantOwnerActivationGrant> TenantOwnerActivationGrants { get; }
}

public interface ICatalogPersistence : IModulePersistence
{
    DbSet<Region> Regions { get; }
    DbSet<RegionModule> RegionModules { get; }
    DbSet<ModuleNode> ModuleNodes { get; }
    DbSet<School> Schools { get; }
    DbSet<Major> Majors { get; }
    DbSet<Subject> Subjects { get; }
    DbSet<Category> Categories { get; }
    DbSet<TaxonomyNode> TaxonomyNodes { get; }
    DbSet<QuestionTaxonomyAssignment> QuestionTaxonomyAssignments { get; }
    DbSet<ScorelineField> ScorelineFields { get; }
    DbSet<ScorelineRecord> ScorelineRecords { get; }
}

public interface IQuestionBankPersistence : IModulePersistence
{
    DbSet<QuestionBank> QuestionBanks { get; }
    DbSet<Question> Questions { get; }
    DbSet<QuestionVersion> QuestionVersions { get; }
    DbSet<ContentEntry> ContentEntries { get; }
    DbSet<ContentNode> ContentNodes { get; }
    DbSet<QuestionCollection> QuestionCollections { get; }
    DbSet<QuestionCollectionItem> QuestionCollectionItems { get; }
    DbSet<PracticeBlueprint> PracticeBlueprints { get; }
    DbSet<QuestionTypeGroup> QuestionTypeGroups { get; }
    DbSet<SubjectShare> SubjectShares { get; }
    DbSet<ContentImportJob> ContentImportJobs { get; }
    DbSet<ContentImportItem> ContentImportItems { get; }
    DbSet<ContentImportIssue> ContentImportIssues { get; }
    DbSet<TenantQuestionBankPreference> TenantQuestionBankPreferences { get; }
    DbSet<TenantQuestionReference> TenantQuestionReferences { get; }
}

public interface IContentAssetPersistence : IModulePersistence
{
    DbSet<VocabularyUnit> VocabularyUnits { get; }
    DbSet<VocabularyWord> VocabularyWords { get; }
    DbSet<HandbookSubject> HandbookSubjects { get; }
    DbSet<HandbookChapter> HandbookChapters { get; }
    DbSet<HandbookEntry> HandbookEntries { get; }
    DbSet<ContentAsset> ContentAssets { get; }
    DbSet<ContentAssetAccessEvent> ContentAssetAccessEvents { get; }
    DbSet<ContentAssetSecurityScanEvent> ContentAssetSecurityScanEvents { get; }
    DbSet<ImageAsset> Images { get; }
    DbSet<AppAsset> AppAssets { get; }
    DbSet<VideoExplanation> VideoExplanations { get; }
    DbSet<QuestionVideo> QuestionVideos { get; }
    DbSet<VideoPlaybackProgress> VideoPlaybackProgress { get; }
}

public interface ILearningPersistence : IModulePersistence
{
    DbSet<UserWordProgress> UserWordProgress { get; }
    DbSet<UserWordFavorite> UserWordFavorites { get; }
    DbSet<AiRecommendationReport> AiRecommendationReports { get; }
    DbSet<PracticeSession> PracticeSessions { get; }
    DbSet<PracticeSessionQuestion> PracticeSessionQuestions { get; }
    DbSet<AnswerRecord> AnswerRecords { get; }
    DbSet<LearningOperationIdempotency> LearningOperationIdempotencies { get; }
    DbSet<FavoriteQuestion> FavoriteQuestions { get; }
    DbSet<WrongQuestion> WrongQuestions { get; }
    DbSet<RecentPractice> RecentPractices { get; }
    DbSet<ExamDate> ExamDates { get; }
    DbSet<Report> Reports { get; }
    DbSet<ReportStatusEvent> ReportStatusEvents { get; }
    DbSet<UserScoreEvent> UserScoreEvents { get; }
    DbSet<PracticeDailyUsage> PracticeDailyUsages { get; }
    DbSet<PracticeAccessEvent> PracticeAccessEvents { get; }
    DbSet<PracticeSessionReport> PracticeSessionReports { get; }
    DbSet<PracticeSessionReportSection> PracticeSessionReportSections { get; }
    DbSet<DashboardDailyStat> DashboardDailyStats { get; }
    DbSet<RevenueDailyStat> RevenueDailyStats { get; }
}

public interface ICommercePersistence : IModulePersistence
{
    DbSet<Product> Products { get; }
    DbSet<SvipPlan> SvipPlans { get; }
    DbSet<Order> Orders { get; }
    DbSet<OrderItem> OrderItems { get; }
    DbSet<Payment> Payments { get; }
    DbSet<PaymentEvent> PaymentEvents { get; }
    DbSet<Entitlement> Entitlements { get; }
    DbSet<CodeBatch> CodeBatches { get; }
    DbSet<ActivationCode> ActivationCodes { get; }
    DbSet<Coupon> Coupons { get; }
    DbSet<CouponRedemption> CouponRedemptions { get; }
    DbSet<CommerceRefundRequest> CommerceRefundRequests { get; }
    DbSet<CommerceRefundEvent> CommerceRefundEvents { get; }
    DbSet<CommerceReconciliationBatch> CommerceReconciliationBatches { get; }
    DbSet<CommerceReconciliationItem> CommerceReconciliationItems { get; }
    DbSet<CommerceReconciliationIssue> CommerceReconciliationIssues { get; }
    DbSet<CommerceReconciliationIssueEvent> CommerceReconciliationIssueEvents { get; }
    DbSet<CommerceAdjustmentVoucher> CommerceAdjustmentVouchers { get; }
    DbSet<CommerceAdjustmentVoucherEvent> CommerceAdjustmentVoucherEvents { get; }
}

public interface IPointsPersistence : IModulePersistence
{
    DbSet<PointActivityTask> PointActivityTasks { get; }
    DbSet<PointActivityClaim> PointActivityClaims { get; }
    DbSet<PointExchangeItem> PointExchangeItems { get; }
    DbSet<PointExchangeOrder> PointExchangeOrders { get; }
}

public interface IGrowthPersistence : IModulePersistence
{
    DbSet<ReferralTrack> ReferralTracks { get; }
    DbSet<ReferralCode> ReferralCodes { get; }
    DbSet<ReferralLead> ReferralLeads { get; }
    DbSet<ReferralTeamEdge> ReferralTeamEdges { get; }
    DbSet<ReferralQrcode> ReferralQrcodes { get; }
    DbSet<CrmConfig> CrmConfigs { get; }
    DbSet<CrmWebhookQueueItem> CrmWebhookQueue { get; }
    DbSet<CrmWebhookLog> CrmWebhookLogs { get; }
    DbSet<TenantCommissionSetting> TenantCommissionSettings { get; }
    DbSet<CommissionSettlement> CommissionSettlements { get; }
    DbSet<CommissionSettlementItem> CommissionSettlementItems { get; }
    DbSet<CommissionSettlementProof> CommissionSettlementProofs { get; }
    DbSet<CommissionSettlementExportEvent> CommissionSettlementExportEvents { get; }
}

public interface IJobsOperationsPersistence : IModulePersistence
{
    DbSet<Banner> Banners { get; }
    DbSet<Faq> Faqs { get; }
    DbSet<Announcement> Announcements { get; }
    DbSet<AuditLog> AuditLogs { get; }
    DbSet<BackendPermission> BackendPermissions { get; }
    DbSet<BackendMenu> BackendMenus { get; }
    DbSet<TenantBackendRole> TenantBackendRoles { get; }
    DbSet<TenantBackendRolePermission> TenantBackendRolePermissions { get; }
    DbSet<TenantBackendRoleMenu> TenantBackendRoleMenus { get; }
    DbSet<TenantBackendUserRole> TenantBackendUserRoles { get; }
    DbSet<PlatformBackendRole> PlatformBackendRoles { get; }
    DbSet<PlatformBackendRolePermission> PlatformBackendRolePermissions { get; }
    DbSet<PlatformBackendRoleMenu> PlatformBackendRoleMenus { get; }
    DbSet<PlatformBackendUserRole> PlatformBackendUserRoles { get; }
    DbSet<AuthorizationScopeVersion> AuthorizationScopeVersions { get; }
    DbSet<AuthorizationCacheInvalidation> AuthorizationCacheInvalidations { get; }
    DbSet<BackgroundJob> BackgroundJobs { get; }
    DbSet<TenantLifecycleOperation> TenantLifecycleOperations { get; }
    DbSet<WorkerHeartbeat> WorkerHeartbeats { get; }
    DbSet<UserNotification> UserNotifications { get; }
}

public interface IPlatformControlPlanePersistence : IModulePersistence
{
    DbSet<PlatformOperationIdempotency> PlatformOperationIdempotencies { get; }
    DbSet<PlatformBillingInvoiceReminder> PlatformBillingInvoiceReminders { get; }
    DbSet<PlatformAuditAlertRule> PlatformAuditAlertRules { get; }
    DbSet<PlatformAuditAlert> PlatformAuditAlerts { get; }
    DbSet<PlatformBillingDunningNotificationChannel> PlatformBillingDunningNotificationChannels { get; }
    DbSet<PlatformBillingDunningNotificationEvent> PlatformBillingDunningNotificationEvents { get; }
    DbSet<PlatformPaymentApp> PlatformPaymentApps { get; }
    DbSet<PlatformPaymentChannel> PlatformPaymentChannels { get; }
    DbSet<PlatformApprovalPolicy> PlatformApprovalPolicies { get; }
    DbSet<PlatformApprovalRequest> PlatformApprovalRequests { get; }
    DbSet<PlatformConfigurationDefinition> PlatformConfigurationDefinitions { get; }
    DbSet<PlatformConfigurationVersion> PlatformConfigurationVersions { get; }
    DbSet<PlatformNotificationTemplate> PlatformNotificationTemplates { get; }
    DbSet<PlatformNotificationDelivery> PlatformNotificationDeliveries { get; }
    DbSet<SaasFeature> SaasFeatures { get; }
    DbSet<PermissionModule> PermissionModules { get; }
    DbSet<SaasOffering> SaasOfferings { get; }
    DbSet<SaasOfferingVersion> SaasOfferingVersions { get; }
    DbSet<SaasOfferingVersionFeature> SaasOfferingVersionFeatures { get; }
    DbSet<SaasFeatureLimitDefinition> SaasFeatureLimitDefinitions { get; }
    DbSet<SaasOfferingVersionLimit> SaasOfferingVersionLimits { get; }
    DbSet<TenantSaasSubscription> TenantSaasSubscriptions { get; }
    DbSet<TenantSaasSubscriptionItem> TenantSaasSubscriptionItems { get; }
    DbSet<TenantFeatureOverride> TenantFeatureOverrides { get; }
    DbSet<TenantFeatureUsage> TenantFeatureUsages { get; }
    DbSet<PlatformBillingQuote> PlatformBillingQuotes { get; }
    DbSet<PlatformBillingQuoteItem> PlatformBillingQuoteItems { get; }
    DbSet<PlatformBillingOrder> PlatformBillingOrders { get; }
    DbSet<PlatformBillingOrderItem> PlatformBillingOrderItems { get; }
    DbSet<PlatformBillingPayment> PlatformBillingPayments { get; }
    DbSet<PlatformBillingPaymentEvent> PlatformBillingPaymentEvents { get; }
    DbSet<PlatformBillingRefund> PlatformBillingRefunds { get; }
    DbSet<PlatformBillingInvoice> PlatformBillingInvoices { get; }
    DbSet<PocketBaseImportRun> PocketBaseImportRuns { get; }
    DbSet<PocketBaseRawRecord> PocketBaseRawRecords { get; }
    DbSet<PocketBaseImportIssue> PocketBaseImportIssues { get; }
}

// Explicit orchestration capabilities. These are intentionally limited to control-plane and bootstrap workflows;
// feature modules must consume the owned interfaces above directly.
public interface IPlatformAdministrationPersistence : IIdentityPersistence, ITenancyPersistence,
    ITenantAdministrationPersistence, IJobsOperationsPersistence, IPlatformControlPlanePersistence
{
}

public interface IPlatformQuestionBankAdministrationPersistence : IQuestionBankPersistence,
    IContentAssetPersistence, ITenancyPersistence, IJobsOperationsPersistence
{
}

public interface IPlatformTenantCapabilitiesPersistence : IIdentityPersistence, ITenancyPersistence,
    ICommercePersistence, IGrowthPersistence, IJobsOperationsPersistence, IPlatformControlPlanePersistence
{
}

public interface IPlatformBillingPersistence : ITenancyPersistence, ITenantAdministrationPersistence,
    IJobsOperationsPersistence, IPlatformControlPlanePersistence
{
}

public interface IOwnerActivationPersistence : IIdentityPersistence, ITenancyPersistence,
    ITenantAdministrationPersistence, IJobsOperationsPersistence
{
}

public interface IBootstrapPersistence : IIdentityPersistence, ITenancyPersistence,
    ITenantAdministrationPersistence, ICommercePersistence, IGrowthPersistence, IJobsOperationsPersistence,
    IPlatformControlPlanePersistence
{
}
