using System.ComponentModel.DataAnnotations;

namespace Domain.Entities.Director
{
    /// <summary>
    /// 관리자 로그인 기록
    /// </summary>
    public class AdminLoginLog
    {
        [Key]
        public int ID { get; private set; }

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

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

        public string? Reason { get; private set; }

        public string? IpAddress { get; private set; }

        public string? UserAgent { get; private set; }

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

        private AdminLoginLog() { }

        private AdminLoginLog(bool success, string account, string? reason, string? ipAddress, string? userAgent)
        {
            if (string.IsNullOrWhiteSpace(account))
            {
                throw new ArgumentException("Account is required.", nameof(account));
            }

            if (account.Length > 120)
            {
                throw new ArgumentOutOfRangeException(nameof(account));
            }

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

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

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

            Success = success;
            Account = account;
            Reason = reason;
            IpAddress = ipAddress;
            UserAgent = userAgent;
            CreatedAt = DateTime.UtcNow;
        }

        public static AdminLoginLog Create(bool success, string account, string? reason = null, string? ipAddress = null, string? userAgent = null)
        {
            return new(success, account, reason, ipAddress, userAgent);
        }
    }
}