using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using e_suite.Database.Audit.Attributes;
using e_suite.Database.Audit.Models;
using e_suite.Database.Core.Tables.CustomFields;
using Microsoft.EntityFrameworkCore;

namespace e_suite.Database.Core.Tables.Glossaries;

[DisplayName("Glossary Custom Field")]
[Table("GlossaryCustomFields", Schema = "Glossaries")]
public class GlossaryCustomField : IId
{
    [Key]
    public long Id { get; set; }

    [Required]
    public long GlossaryId { get; set; }

    [Required]
    public long CustomFieldId { get; set; }

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

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

    [ForeignKey(nameof(CustomFieldId))]
    public virtual CustomField CustomField { get; set; } = null!;

    public static void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<GlossaryCustomField>()
            .HasAlternateKey(gc => new { gc.GlossaryId, gc.CustomFieldId });

        modelBuilder.Entity<GlossaryCustomField>()
            .HasOne(gc => gc.Glossary)
            .WithMany(gc => gc.CustomFieldDefinitions)
            .HasForeignKey(gc => gc.GlossaryId);


        modelBuilder.Entity<GlossaryCustomField>()
            .HasData(new GlossaryCustomField
            {
                Id = 1,
                GlossaryId = 3,
                CustomFieldId = 1
            });
    }
}