using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Domain.Entities.Members.Logs
{
    /// <summary>
    /// 자기소개 변경 내역
    /// </summary>
    public class MemberIntroChangeLog
    {
        [ForeignKey(nameof(MemberID))]
        public virtual Member Member { get; private set; } = null!;

        [Key]
        public int ID { get; private set; }

        public int MemberID { get; private set; }

        public string? BeforeIntro { get; private set; } = default;

        public string? AfterIntro { get; private set; } = default;

        public string? Referer { get; private set; } = default;

        public string? IpAddress { get; private set; } = default;

        public string? UserAgent { get; private set; } = default;

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

        private MemberIntroChangeLog() { }

        private MemberIntroChangeLog(int memberID, string? beforeIntro, string? afterIntro, string? referer, string? ipAddress, string? userAgent)
        {
            if (memberID <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(memberID));
            }

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

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

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

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

            MemberID = memberID;
            BeforeIntro = beforeIntro;
            AfterIntro = afterIntro;
            Referer = referer;
            IpAddress = ipAddress;
            UserAgent = userAgent;
            CreatedAt = DateTime.UtcNow;
        }

        public static MemberIntroChangeLog Create(int memberID, string? beforeIntro, string? afterIntro, string? referer = null, string? ipAddress = null, string? userAgent = null)
        {
            return new(memberID, beforeIntro, afterIntro, referer, ipAddress, userAgent);
        }
    }
}