using System.ComponentModel.DataAnnotations;

namespace Domain.Entities.Page
{
    public class Document
    {
        [Key]
        public int ID { get; private set; }

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

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

        public string? Content { get; private set; }

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

        public int Views { get; private set; } = 0;

        public DateTime? UpdatedAt { get; private set; }

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

        private Document() { }

        private Document(string code, string subject, string? content, bool isActive)
        {
            if (string.IsNullOrWhiteSpace(code))
            {
                throw new ArgumentException("Code is required.", nameof(code));
            }

            if (code.Length > 30)
            {
                throw new ArgumentOutOfRangeException(nameof(code));
            }

            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new ArgumentException("Subject is required.", nameof(subject));
            }

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

            Code = code;
            Subject = subject;
            Content = content;
            IsActive = isActive;
        }

        public static Document Create(string code, string subject, string? content = null, bool isActive = false)
        {
            return new(code, subject, content, isActive);
        }

        public void Update(string code, string subject, string? content, bool isActive)
        {
            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new ArgumentException("Subject is required.", nameof(subject));
            }

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

            Code = code;
            Subject = subject;
            Content = content;
            IsActive = isActive;
            UpdatedAt = DateTime.UtcNow;
        }

        public void SetContent(string? content)
        {
            Content = content;
            UpdatedAt = DateTime.UtcNow;
        }

        public void IncreaseViews()
        {
            Views++;
        }
    }
}