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

namespace Domain.Entities.Members.Logs
{
    /// <summary>
    /// 한마디 변경 내역
    /// </summary>
    public class MemberSummaryChangeLog
    {
        [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? BeforeSummary { get; private set; } = default;

        public string? AfterSummary { 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 MemberSummaryChangeLog() { }

        private MemberSummaryChangeLog(int memberID, string? beforeSummary, string? afterSummary, string? referer, string? ipAddress, string? userAgent)
        {
            if (memberID <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(memberID));
            }

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

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

            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;
            BeforeSummary = beforeSummary;
            AfterSummary = afterSummary;
            Referer = referer;
            IpAddress = ipAddress;
            UserAgent = userAgent;
            CreatedAt = DateTime.UtcNow;
        }

        public static MemberSummaryChangeLog Create(int memberID, string? beforeSummary, string? afterSummary, string? referer = null, string? ipAddress = null, string? userAgent = null)
        {
            return new(memberID, beforeSummary, afterSummary, referer, ipAddress, userAgent);
        }
    }
}