using JetBrains.Annotations;
using NPin.Framework.SqlSugarCore.Abstractions;
using SqlSugar;
using Volo.Abp.Auditing;
using Volo.Abp.Data;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.TenantManagement;
using Check = Volo.Abp.Check;

namespace NPin.Framework.TenantManagement.Domain;

[SugarTable("NPinTenant")]
[MasterTenant]
public class TenantAggregateRoot : FullAuditedAggregateRoot<Guid>, IHasEntityVersion
{
    [SugarColumn(IsPrimaryKey = true)] public override Guid Id { get; protected set; }
    public virtual string Name { get; protected set; }
    public int EntityVersion { get; protected set; }

    public string TenantConnectionString { get; protected set; }

    public DbType DbType { get; protected set; }

    [SugarColumn(IsIgnore = true)]
    public override ExtraPropertyDictionary ExtraProperties
    {
        get => base.ExtraProperties;
        protected set => base.ExtraProperties = value;
    }

    public virtual void SetConnectionString(DbType dbType, string connectionString)
    {
        DbType = dbType;
        TenantConnectionString = connectionString;
    }

    protected internal virtual void SetName([NotNull] string name)
    {
        Name = Check.NotNullOrWhiteSpace(name, nameof(name), TenantConsts.MaxNameLength);
    }

    public TenantAggregateRoot()
    {
    }

    protected internal TenantAggregateRoot(Guid id, [NotNull] string name) : base(id)
    {
        SetName(name);
    }
}