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

namespace Domain.Entities.Page.Faq
{
    public class FaqItem
    {
        [ForeignKey(nameof(CategoryID))]
        public virtual FaqCategory FaqCategory { get; private set; } = null!;

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

        public int CategoryID { get; private set; }

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

        public string? Answer { get; private set; }

        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 FaqItem() { }

        private FaqItem(int categoryID, string question, string? answer, short order, bool isActive)
        {
            if (categoryID <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(categoryID));
            }

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

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

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

            CategoryID = categoryID;
            Question = question;
            Answer = answer;
            Order = order;
            IsActive = isActive;
        }

        public static FaqItem Create(int categoryID, string question, string? answer = null, short order = 0, bool isActive = false)
        {
            return new(categoryID, question, answer, order, isActive);
        }

        public void Update(int categoryID, string question, string? answer, short order, bool isActive)
        {
            if (categoryID <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(categoryID));
            }

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

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

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

            CategoryID = categoryID;
            Question = question;
            Answer = answer;
            Order = order;
            IsActive = isActive;
            UpdatedAt = DateTime.UtcNow;
        }

        public void SetContent(string? answer)
        {
            Answer = answer;
            UpdatedAt = DateTime.UtcNow;
        }
    }
}