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

namespace Domain.Entities.News
{
    public class RssNewsArticle
    {
        [ForeignKey(nameof(RssFeedSourceID))]
        public virtual RssFeedSource RssFeedSource { get; set; } = null!;

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

        public int RssFeedSourceID { get; private set; }

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

        public string? Link { get; private set; }

        public string? Guid { get; private set; }

        public string? Author { get; private set; }

        public string? Description { get; private set; }

        public string? Content { get; private set; }

        public string? ImageUrl { get; private set; }

        public string? SourceName { get; private set; }

        public string? Categories { get; private set; }

        public int CommentCount { get; private set; } = 0;

        public DateTime? PublishedAt { get; private set; }

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

        private RssNewsArticle() { }

        private RssNewsArticle(
            int rssFeedSourceID,
            string title,
            string? link,
            string? guid,
            string? author,
            string? description,
            string? content,
            string? imageUrl,
            string? sourceName,
            string? categories,
            int commentCount,
            DateTime? publishedAt
        ) {
            if (string.IsNullOrWhiteSpace(title))
            {
                throw new ArgumentException("Title is required.", nameof(title));
            }

            if (title.Length > 500)
            {
                title = title[..500];
            }

            RssFeedSourceID = rssFeedSourceID;
            Title = title;
            Link = link;
            Guid = guid;
            Author = author;
            Description = description;
            Content = content;
            ImageUrl = imageUrl;
            SourceName = sourceName;
            Categories = categories;
            CommentCount = commentCount;
            PublishedAt = publishedAt;
        }

        public static RssNewsArticle Create(
            int rssFeedSourceID,
            string title,
            string? link = null,
            string? guid = null,
            string? author = null,
            string? description = null,
            string? content = null,
            string? imageUrl = null,
            string? sourceName = null,
            string? categories = null,
            int commentCount = 0,
            DateTime? publishedAt = null
        ) => new(rssFeedSourceID, title, link, guid, author, description, content, imageUrl, sourceName, categories, commentCount, publishedAt);
    }
}