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

namespace Domain.Entities.Page.Banner
{
    public class BannerItem
    {
        [ForeignKey(nameof(PositionID))]
        public virtual BannerPosition BannerPosition { 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? DesktopImage { get; private set; }

        public string? MobileImage { get; private set; }

        public string? Link { get; private set; }

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

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

        public DateTime? StartAt { get; private set; }

        public DateTime? EndAt { get; private set; }

        public DateTime? UpdatedAt { get; private set; }

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

        private BannerItem() { }

        private BannerItem(
            int positionID,
            string subject,
            string? desktopImage,
            string? mobileImage,
            string? link,
            short order,
            bool isActive,
            DateTime? startAt,
            DateTime? endAt
        ) {
            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;
            DesktopImage = desktopImage;
            MobileImage = mobileImage;
            Link = link;
            Order = order;
            IsActive = isActive;
            StartAt = startAt;
            EndAt = endAt;
        }

        public static BannerItem Create(
            int positionID,
            string subject,
            string? desktopImage = null,
            string? mobileImage = null,
            string? link = null,
            short order = 0,
            bool isActive = false,
            DateTime? startAt = null,
            DateTime? endAt = null
        ) => new(positionID, subject, desktopImage, mobileImage, link, order, isActive, startAt, endAt);

        public void Update(
            string subject,
            string? desktopImage,
            string? mobileImage,
            string? link,
            short order,
            bool isActive,
            DateTime? startAt,
            DateTime? endAt
        ) {
            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));
            }

            Subject = subject;
            DesktopImage = desktopImage;
            MobileImage = mobileImage;
            Link = link;
            Order = order;
            IsActive = isActive;
            StartAt = startAt;
            EndAt = endAt;
            UpdatedAt = DateTime.UtcNow;
        }

        public void SetDesktopImage(string? desktopImage)
        {
            DesktopImage = desktopImage;
            UpdatedAt = DateTime.UtcNow;
        }

        public void SetMobileImage(string? mobileImage)
        {
            MobileImage = mobileImage;
            UpdatedAt = DateTime.UtcNow;
        }
    }
}