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 e_suite.Database.Core.Tables.UserManager;
using Microsoft.EntityFrameworkCore;

namespace e_suite.Database.Core.Tables.Domain;

[DisplayName("UserRoles")]
[Table("UserRoles", Schema = "Domain")]
public class UserRole : ISoftDeletable
{
    [Key]
    public long Id { get; set; }

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

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

    [AuditSoftDelete(true)]
    [Required]
    [DefaultValue(false)]
    public bool Deleted { get; set; } = false;

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

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

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

    public static void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<UserRole>()
            .HasAlternateKey(gc => new { gc.UserId , gc.RoleId });

        modelBuilder.Entity<UserRole>().HasData(new UserRole
        {
            Id = 1,
            UserId = 1,
            RoleId = 1
        });
    }
}