namespace Domain.Entities.Members
{
    public class Channel
    {
        public virtual Member Member { get; private set; } = null!;

        public int ID { get; private set; }

        public int MemberID { get; private set; }

        public string SID { get; private set; } = default!;

        public string Name { get; private set; } = default!;

        public string? Handle { get; private set; }

        public string YouTubeUrl { get; private set; } = default!;

        public decimal PlatformFeeRate { get; private set; } = 0;

        public bool IsVerified { get; private set; } = false;

        public bool IsActive { get; private set; } = false;

        public DateTime? UpdatedAt { get; private set; }

        public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;

        private Channel() { }

        private Channel(int memberID, string sid, string name, string youtubeUrl)
        {
            if (memberID <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(memberID));
            }

            if (string.IsNullOrWhiteSpace(sid))
            {
                throw new ArgumentException("SID is required.", nameof(sid));
            }

            if (sid.Length != 24)
            {
                throw new ArgumentOutOfRangeException(nameof(sid));
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Name is required.", nameof(name));
            }

            if (name.Length > 200)
            {
                throw new ArgumentOutOfRangeException(nameof(name));
            }

            if (string.IsNullOrWhiteSpace(youtubeUrl))
            {
                throw new ArgumentException("YouTubeUrl is required.", nameof(youtubeUrl));
            }

            if (youtubeUrl.Length > 255)
            {
                throw new ArgumentOutOfRangeException(nameof(youtubeUrl));
            }

            MemberID = memberID;
            SID = sid;
            Name = name;
            YouTubeUrl = youtubeUrl;
        }

        public static Channel Create(int memberID, string sid, string name, string youtubeUrl)
        {
            return new(memberID, sid, name, youtubeUrl);
        }

        public void Update(string name, string? handle, string youtubeUrl, decimal platformFeeRate, bool isVerified, bool isActive)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Name is required.", nameof(name));
            }

            if (name.Length > 200)
            {
                throw new ArgumentOutOfRangeException(nameof(name));
            }

            if (handle is not null && handle.Length > 30)
            {
                throw new ArgumentOutOfRangeException(nameof(handle));
            }

            if (string.IsNullOrWhiteSpace(youtubeUrl))
            {
                throw new ArgumentException("YouTubeUrl is required.", nameof(youtubeUrl));
            }

            if (youtubeUrl.Length > 255)
            {
                throw new ArgumentOutOfRangeException(nameof(youtubeUrl));
            }

            if (platformFeeRate < 0 || platformFeeRate > 100)
            {
                throw new ArgumentOutOfRangeException(nameof(platformFeeRate));
            }

            Name = name;
            Handle = handle;
            YouTubeUrl = youtubeUrl;
            PlatformFeeRate = platformFeeRate;
            IsVerified = isVerified;
            IsActive = isActive;
            UpdatedAt = DateTime.UtcNow;
        }
    }
}