using System.ComponentModel.DataAnnotations;

namespace Domain.Entities.Page.Faq
{
    public class FaqCategory
    {
        public virtual List<FaqItem> FaqItems { get; private set; } = [];

        [Key]
        public int ID { get; private set; }

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

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

        public short Order { get; private set; } = 0;

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

        public DateTime? UpdatedAt { get; private set; }

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

        private FaqCategory() { }

        private FaqCategory(string code, string subject, short order, 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 > 255)
            {
                throw new ArgumentOutOfRangeException(nameof(subject));
            }

            if (order < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(order));
            }

            Code = code;
            Subject = subject;
            Order = order;
            IsActive = isActive;
        }

        public static FaqCategory Create(string code, string subject, short order = 0, bool isActive = false)
        {
            return new(code, subject, order, isActive);
        }

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

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

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

            if (order < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(order));
            }

            Code = code;
            Subject = subject;
            Order = order;
            IsActive = isActive;
            UpdatedAt = DateTime.UtcNow;
        }
    }
}