using System.ComponentModel.DataAnnotations;

namespace Domain.Entities.News
{
    public class RssFeedSource
    {
        public virtual List<RssNewsArticle> RssNewsArticle { get; private set; } = [];

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

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

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

        public string? Description { get; private set; }

        public int IntervalMinutes { get; private set; } = 10;

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

        public DateTime? LastFetchedAt { get; private set; }

        public DateTime? UpdatedAt { get; private set; }

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

        private RssFeedSource() { }

        private RssFeedSource(string name, string url, string? description, int intervalMinutes)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Name is required.", nameof(name));
            }

            if (name.Length > 200)
            {
                throw new ArgumentOutOfRangeException(nameof(name));
            }

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

            if (url.Length > 1000)
            {
                throw new ArgumentOutOfRangeException(nameof(url));
            }

            if (intervalMinutes < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(intervalMinutes));
            }

            Name = name;
            Url = url;
            Description = description;
            IntervalMinutes = intervalMinutes;
        }

        public static RssFeedSource Create(string name, string url, string? description = null, int intervalMinutes = 10)
            => new(name, url, description, intervalMinutes);

        public void Update(string name, string url, string? description, int intervalMinutes, bool isActive)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Name is required.", nameof(name));
            }

            if (name.Length > 200)
            {
                throw new ArgumentOutOfRangeException(nameof(name));
            }

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

            if (url.Length > 1000)
            {
                throw new ArgumentOutOfRangeException(nameof(url));
            }

            if (intervalMinutes < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(intervalMinutes));
            }

            Name = name;
            Url = url;
            Description = description;
            IntervalMinutes = intervalMinutes;
            IsActive = isActive;
            UpdatedAt = DateTime.UtcNow;
        }

        public void MarkFetched()
        {
            LastFetchedAt = DateTime.UtcNow;
        }
    }
}