using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using e_suite.Database.Audit.Attributes;
using e_suite.Database.Core.Models;
using Microsoft.EntityFrameworkCore;

namespace e_suite.Database.Core.Tables.Glossaries;

[DisplayName("Glossary")]
[Table("Glossaries", Schema = "Glossaries")]
[Index(nameof(Id), nameof(ParentId), IsUnique = true)]
[Index(nameof(Name), IsUnique = true)]
[Index(nameof(Guid), IsUnique = true)]
public class Glossary : IGeneralId, ISoftDeletable
{
    [Key]
    public long Id { get; set; }

    public long? ParentId { get; set; }

    [AuditName]
    [Required]
    public string Name { get; set; } = string.Empty;
    
    [AuditSoftDelete(true)]
    [Required]
    [DefaultValue(false)]
    public bool Deleted { get; set; } = false;

    [Required]
    public Guid Guid { get; set; } = Guid.NewGuid();

    [AuditLastUpdated]
    public DateTimeOffset LastUpdated { get; set; }

    [ForeignKey(nameof(ParentId))]
    [AuditParent]
    public virtual Glossary Parent { get; set; } = null!;

    public Collection<GlossaryCustomField> CustomFieldDefinitions { get; set; } = [];

    public Collection<GlossaryCustomFieldValue> CustomFieldValues { get; set; } = [];

    public static void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Glossary>().HasData(new Glossary
        {
            Id = 1,
            Guid = new Guid("{FA6566F8-B4B0-48C5-9985-336C9284796E}"),
            Name = "System",
            Deleted = false,
            ParentId = null
        });

        modelBuilder.Entity<Glossary>().HasData(new Glossary
        {
            Id = 2,
            Guid = new Guid("{90C48CF1-1A2B-4A76-B30B-260ECB149CCC}"),
            Name = "Domain",
            Deleted = false,
            ParentId = null
        });

        modelBuilder.Entity<Glossary>().HasData(new Glossary
        {
            Id = 3,
            Guid = new Guid("{35EB8C23-4528-49A7-A798-B8BAE3B06DAF}"),
            Name = "Print Specifications",
            Deleted = false,
            ParentId = 1
        });
    }
}