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

namespace Tiku.Domain.Tenancy;

public sealed class TenantDomain : AuditableTenantEntity
{
    public string Host { get; set; } = string.Empty;
    public TenantDomainType DomainType { get; set; } = TenantDomainType.Custom;
    public TenantDomainStatus Status { get; set; } = TenantDomainStatus.Pending;
    public bool IsPrimary { get; set; }
    public string? VerificationToken { get; set; }
    public DateTimeOffset? VerifiedAt { get; set; }
    public DateTimeOffset? LastCheckedAt { get; set; }
    public DateTimeOffset? DnsVerifiedAt { get; set; }
    public DateTimeOffset? TlsReadyAt { get; set; }
    public string? LastFailureReason { get; set; }
}

public enum TenantDomainType
{
    System,
    Custom,
    Miniapp
}

public enum TenantDomainStatus
{
    Pending,
    Active,
    Failed,
    Disabled
}

public sealed class TenantBranding : IHasTimestamps, ITenantOwned
{
    public string BrandName { get; set; } = string.Empty;
    public string? ShortName { get; set; }
    public string? Slogan { get; set; }
    public string? OrganizationName { get; set; }
    public string? LogoUrl { get; set; }
    public string? FaviconUrl { get; set; }
    public string? ServiceWechat { get; set; }
    public string? ServiceAccountName { get; set; }
    public JsonElement Theme { get; set; } = JsonDefaults.Object();
    public JsonElement PublicAssets { get; set; } = JsonDefaults.Object();
    public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
    public DateTimeOffset UpdatedAt { get; set; } = DateTimeOffset.UtcNow;
    public Guid TenantId { get; set; }
}

public sealed class TenantSettings : IHasTimestamps, ITenantOwned
{
    public JsonElement FeatureFlags { get; set; } = JsonDefaults.Object();
    public JsonElement AdminFeatureFlags { get; set; } = JsonDefaults.Object();
    public JsonElement PublicConfig { get; set; } = JsonDefaults.Object();
    public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
    public DateTimeOffset UpdatedAt { get; set; } = DateTimeOffset.UtcNow;
    public Guid TenantId { get; set; }
}

public sealed class TenantAuthPolicy : IHasTimestamps, ITenantOwned
{
    public bool AllowExternalStudentSelfRegistration { get; set; }
    public string[] AllowedStudentLoginMethods { get; set; } = ["password"];
    public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
    public DateTimeOffset UpdatedAt { get; set; } = DateTimeOffset.UtcNow;
    public Guid TenantId { get; set; }
}

public sealed class TenantFrontendConfig : AuditableTenantEntity
{
    public int SchemaVersion { get; set; } = 1;
    public int ConfigVersion { get; set; } = 1;
    public JsonElement PublishedBranding { get; set; } = JsonDefaults.Object();
    public JsonElement PublishedTheme { get; set; } = JsonDefaults.Object();
    public JsonElement PublishedFeatures { get; set; } = JsonDefaults.Object();
    public JsonElement PublishedNavigation { get; set; } = JsonDefaults.Array();
    public JsonElement PublishedHomeModules { get; set; } = JsonDefaults.Array();
    public JsonElement DraftBranding { get; set; } = JsonDefaults.Object();
    public JsonElement DraftTheme { get; set; } = JsonDefaults.Object();
    public JsonElement DraftFeatures { get; set; } = JsonDefaults.Object();
    public JsonElement DraftNavigation { get; set; } = JsonDefaults.Array();
    public JsonElement DraftHomeModules { get; set; } = JsonDefaults.Array();
    public DateTimeOffset? PublishedAt { get; set; }
}