using System.Text.Json;
using Tiku.Domain.Common;

namespace Tiku.Domain.Content;

public sealed class ContentAssetAccessEvent : Entity, ITenantOwned
{
    public Guid TenantId { get; set; }
    public Guid? AssetId { get; set; }
    public Guid? UserId { get; set; }
    public AssetAccessActorRole ActorRole { get; set; } = AssetAccessActorRole.Anonymous;
    public AssetAccessType AccessType { get; set; } = AssetAccessType.Preview;
    public string? Visibility { get; set; }
    public string? AssetType { get; set; }
    public string? StorageProvider { get; set; }
    public AssetAccessDisposition? Disposition { get; set; }
    public int? ExpiresInSeconds { get; set; }
    public string? SignatureMode { get; set; }
    public AssetAccessResult Result { get; set; } = AssetAccessResult.Granted;
    public string? DenyCode { get; set; }
    public string? IpAddress { get; set; }
    public string? UserAgent { get; set; }
    public JsonElement Metadata { get; set; } = JsonDefaults.Object();
    public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
}

public sealed class ContentAssetSecurityScanEvent : Entity, ITenantOwned
{
    public Guid TenantId { get; set; }
    public Guid? AssetId { get; set; }
    public string Provider { get; set; } = string.Empty;
    public AssetSecurityScanStatus ScanStatus { get; set; } = AssetSecurityScanStatus.Pending;
    public AssetSecurityRiskLevel RiskLevel { get; set; } = AssetSecurityRiskLevel.None;
    public string[] IssueCodes { get; set; } = [];
    public JsonElement Details { get; set; } = JsonDefaults.Object();
    public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
}

public sealed class TenantQuestionBankPreference : AuditableTenantEntity
{
    public Guid QuestionBankOwnerTenantId { get; set; }
    public Guid QuestionBankId { get; set; }
    public bool IsVisible { get; set; } = true;
    public string? Alias { get; set; }
    public int SortOrder { get; set; }
    public string? NavigationLocation { get; set; }
    public Guid? CreatedBy { get; set; }
    public Guid? UpdatedBy { get; set; }
    public JsonElement Metadata { get; set; } = JsonDefaults.Object();
}

public sealed class TenantQuestionReference : AuditableTenantEntity
{
    public Guid QuestionOwnerTenantId { get; set; }
    public Guid QuestionId { get; set; }
    public QuestionSource Source { get; set; } = QuestionSource.Tenant;
    public Guid? CreatedBy { get; set; }
}

public sealed class AiRecommendationReport : AuditableTenantEntity
{
    public Guid UserId { get; set; }
    public Guid? RegionId { get; set; }
    public AiRecommendationReportStatus Status { get; set; } = AiRecommendationReportStatus.Generated;
    public string Provider { get; set; } = "local_rules";
    public string? Model { get; set; }
    public string PromptVersion { get; set; } = "school-recommendation-v1";
    public JsonElement InputPayload { get; set; } = JsonDefaults.Object();
    public JsonElement ContextPayload { get; set; } = JsonDefaults.Object();
    public JsonElement ResultPayload { get; set; } = JsonDefaults.Object();
    public string? ErrorMessage { get; set; }
    public DateTimeOffset? GeneratedAt { get; set; }
}

public enum AssetAccessActorRole { Anonymous, Student, TenantAdmin, TenantContentEditor, System }
public enum AssetAccessType { Download, Preview, AdminDownload, AdminPreview, UploadSign, UploadConfirm }
public enum AssetAccessDisposition { Attachment, Inline }
public enum AssetAccessResult { Granted, Denied }
public enum AssetSecurityRiskLevel { None, Low, Medium, High, Critical }
public enum QuestionSource { Platform, Tenant }
public enum AiRecommendationReportStatus { Draft, Generated, Failed }
