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("Domain")]
[Table("Domains", Schema = "Domain")]
[Index(nameof(Name), IsUnique = true)]
[Index(nameof(Guid), IsUnique = true)]
public class Domain : IGeneralId, ISoftDeletable
{
    [Key]
    public long Id { get; set; }

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

    [AuditName]
    [Required]
    public string Name { get; set; } = string.Empty;

    [MaxLength(16)] //Taken from Domain.Hostname in sunrise
    public string SunriseHostname { get; set; } = string.Empty;

    [MaxLength(128)] //Taken from PublicApiKeys.AppId in sunrise
    public string SunriseAppId { get; set; } = string.Empty;

    [MaxLength(16)] //Taken from Category.Id in sunrise
    public string SunriseCategoryId { get; set; } = string.Empty;

    public long? SigmaId { get; set; }

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


    [DefaultValue(null)]
    public long? SsoProviderId { get; set; } = null!;

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

    [AuditParent]
    [ForeignKey(nameof(SsoProviderId))]
    public virtual SsoProvider? SsoProvider { get; set; } = null!;

    public static void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Domain>().HasData(new Domain
        {
            Id = 1,
            Guid = new Guid("{4EB1B0C7-E81B-4C9D-BA91-384ED47A1CF9}"),
            Name = "Sun-Strategy",
            Deleted = false,
        });
    }
}