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

namespace e_suite.Database.Audit.Tables.Audit;

[NoAudit]
[Table("AuditDrillHierarchies", Schema = "Audit")]
public class AuditDrillHierarchy : IId
{
    [Key]
    public long Id { get; set; }

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

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

    [ForeignKey(nameof(ParentAuditDrillDownEntityId))]
    public virtual AuditEntry ParentAuditDrillDownEntity { get; set; } = null!;

    [ForeignKey(nameof(ChildAuditDrillDownEntityId))]
    public virtual AuditEntry ChildAuditDrillDownEntity { get; set; } = null!;

    public static void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<AuditDrillHierarchy>()
            .HasOne(x => x.ParentAuditDrillDownEntity)
            .WithMany()
            .OnDelete(DeleteBehavior.NoAction);

        modelBuilder.Entity<AuditDrillHierarchy>()
            .HasOne(x => x.ChildAuditDrillDownEntity)
            .WithMany()
            .OnDelete(DeleteBehavior.NoAction);
    }
}