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

namespace ContactManagementSystem.Models
{
    /// <summary>
    /// ContactNote entity representing notes attached to a contact
    /// Demonstrates: One-to-Many relationship, Foreign keys, Navigation properties
    /// </summary>
    [Table("ContactNotes")] // CONTACT_NOTES
    public class ContactNote
    {

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Required(ErrorMessage = "Note content is required")]
        [MaxLength(1000)]
        [Column("Content", TypeName = "text")]
        public string Content { get; set; }

        [Required]
        [Column("DateCreated")]
        public DateTime DateCreated { get; set; }

        [MaxLength(50)]
        [Column("Category", TypeName = "varchar(50)")]
        public string Category { get; set; }

        [Required]
        [Column("ContactId")]
        public int ContactId { get; set; }

        [ForeignKey("ContactId")]
        public Contact Contact { get; set; }


        #region Constructors

        /// <summary>
        /// Default constructor (required for EF Core)
        /// </summary>
        public ContactNote()
        {
            DateCreated = DateTime.Now;
        }

        /// <summary>
        /// Constructor with content
        /// </summary>
        /// <param name="content">Note content</param>
        /// <param name="contactId">Contact this note belongs to</param>
        public ContactNote(string content, int contactId)
        {
            Content = content ?? throw new ArgumentNullException(nameof(content));
            ContactId = contactId;
            DateCreated = DateTime.Now;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Returns a string representation of the note
        /// </summary>
        public override string ToString()
        {
            return $"[{DateCreated:yyyy-MM-dd}] {Content}";
        }

        #endregion
    }
}













/* INSTRUCTOR NOTES:
 * 
 * One-to-Many Relationship Explained:
 * - One Contact can have MANY ContactNotes
 * - Each ContactNote belongs to ONE Contact
 * - ContactId is the foreign key that creates this relationship
 * 
 * Key Components:
 * 1. Foreign Key Property: ContactId (int)
 * 2. Navigation Property: Contact (Contact object)
 * 3. [ForeignKey] attribute links them together
 * 
 * In the Contact class, we add:
 * - Navigation property: ICollection<ContactNote> Notes
 * 
 * EF Core Configuration (in DbContext):
 * modelBuilder.Entity<ContactNote>()
 *     .HasOne(n => n.Contact)      // Each note has one contact
 *     .WithMany(c => c.Notes)      // Each contact has many notes
 *     .HasForeignKey(n => n.ContactId)
 *     .OnDelete(DeleteBehavior.Cascade); // Delete notes when contact deleted
 * 
 * Usage Examples:
 * 
 * // Adding a note to contact
 * var note = new ContactNote("Important client", contactId: 1);
 * await _context.ContactNotes.AddAsync(note);
 * await _context.SaveChangesAsync();
 * 
 * // Query contact with notes (eager loading)
 * var contact = await _context.Contacts
 *     .Include(c => c.Notes)
 *     .FirstOrDefaultAsync(c => c.Id == 1);
 * 
 * foreach (var note in contact.Notes)
 * {
 *     Console.WriteLine(note.Content);
 * }
 * 
 * // Query notes for a contact
 * var notes = await _context.ContactNotes
 *     .Where(n => n.ContactId == 1)
 *     .OrderByDescending(n => n.DateCreated)
 *     .ToListAsync();
 * 
 * Benefits of Relationships:
 * - Data integrity enforced by database
 * - Easy to query related data
 * - Cascade deletes handled automatically
 * - Type-safe navigation between entities
 */
