using System.ComponentModel.DataAnnotations;

namespace Domain.Entities.Director;

/// <summary>
/// 관리자 접근 기록
/// </summary>
public class AdminAccessLog
{
    [Key]
    public long ID { get; private set; }

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

    public string? UserName { get; private set; }

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

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

    public string? QueryString { get; private set; }

    public int StatusCode { get; private set; }

    public long ElapsedMs { get; private set; }

    public string? MenuName { get; private set; }

    public string? IpAddress { get; private set; }

    public string? UserAgent { get; private set; }

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

    private AdminAccessLog() { }

    public static AdminAccessLog Create(
        string userId,
        string? userName,
        string method,
        string path,
        string? queryString,
        int statusCode,
        long elapsedMs,
        string? menuName,
        string? ipAddress,
        string? userAgent
    ) {
        return new AdminAccessLog
        {
            UserID = userId.Length > 64 ? userId[..64] : userId,
            UserName = userName?.Length > 100 ? userName[..100] : userName,
            Method = method.Length > 10 ? method[..10] : method,
            Path = path.Length > 2048 ? path[..2048] : path,
            QueryString = queryString?.Length > 2048 ? queryString[..2048] : queryString,
            StatusCode = statusCode,
            ElapsedMs = elapsedMs,
            MenuName = menuName?.Length > 200 ? menuName[..200] : menuName,
            IpAddress = ipAddress?.Length > 45 ? ipAddress[..45] : ipAddress,
            UserAgent = userAgent?.Length > 512 ? userAgent[..512] : userAgent,
            CreatedAt = DateTime.UtcNow
        };
    }
}