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

namespace Domain.Entities.Page.Popup
{
    public class Popup
    {
        [ForeignKey(nameof(PositionID))]
        public virtual PopupPosition PopupPosition { get; private set; } = null!;

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

        public int PositionID { get; private set; }

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

        public string? Content { get; private set; }

        public string? Link { get; private set; }

        public DateTime? StartAt { get; private set; }

        public DateTime? EndAt { 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 Popup() { }

        private Popup(int positionID, string subject, string? content, string? link, DateTime? startAt, DateTime? endAt, short order, bool isActive)
        {
            if (positionID <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(positionID));
            }

            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));
            }

            PositionID = positionID;
            Subject = subject;
            Content = content;
            Link = link;
            StartAt = startAt;
            EndAt = endAt;
            Order = order;
            IsActive = isActive;
        }

        public static Popup Create(int positionID, string subject, string? content = null, string? link = null, DateTime? startAt = null, DateTime? endAt = null, short order = 0, bool isActive = false)
        {
            return new(positionID, subject, content, link, startAt, endAt, order, isActive);
        }

        public void Update(int positionID, string subject, string? content, string? link, DateTime? startAt, DateTime? endAt, short order, bool isActive)
        {
            if (positionID <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(positionID));
            }

            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));
            }

            PositionID = positionID;
            Subject = subject;
            Content = content;
            Link = link;
            StartAt = startAt;
            EndAt = endAt;
            Order = order;
            IsActive = isActive;
            UpdatedAt = DateTime.UtcNow;
        }

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