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

namespace Domain.Entities.Members.Logs
{
    /// <summary>
    /// 사용자 별명 변경 내역
    /// </summary>
    public class MemberNameChangeLog
    {
        [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? BeforeName { get; private set; } = default;

        public string? AfterName { 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 MemberNameChangeLog() { }

        private MemberNameChangeLog(int memberID, string? beforeName, string? afterName, string? referer, string? ipAddress, string? userAgent)
        {
            if (memberID <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(memberID));
            }

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

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

            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;
            BeforeName = beforeName;
            AfterName = afterName;
            Referer = referer;
            IpAddress = ipAddress;
            UserAgent = userAgent;
            CreatedAt = DateTime.UtcNow;
        }

        public static MemberNameChangeLog Create(int memberID, string? beforeName, string? afterName, string? referer = null, string? ipAddress = null, string? userAgent = null)
        {
            return new(memberID, beforeName, afterName, referer, ipAddress, userAgent);
        }
    }
}