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.Nuget.PasswordHasher;
using eSuite.Core.MailService;
using Microsoft.EntityFrameworkCore;

namespace e_suite.Database.Core.Tables.UserManager;

[DisplayName("User")]
[Table("Users", Schema = "UserManager")]
[Index(nameof(Email), IsUnique = true)]
[Index(nameof(Guid), IsUnique = true)]
public class User : IPassword, IEmailAddress, IGeneralId
{
    [Key]
    public long Id { get; set; }

    [Required]
    [DefaultValue("")]
    public string FirstName { get; set; } = string.Empty;

    [Required]
    [DefaultValue("")]
    public string LastName { get; set; } = string.Empty;

    [Required]
    [DefaultValue("")]
    public string MiddleNames { get; set; } = string.Empty;

    [AuditName]
    [NotMapped]
    public string DisplayName => ((FirstName.Trim() + " " + MiddleNames).Trim() + " " + LastName).Trim();

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

    [RedactAudit]
    [Required]
    public string Password { get; set; } = string.Empty;

    [Required]
    public bool EmailConfirmed { get; set; }

    [Required]
    public bool UsingTwoFactorAuthentication { get; set; }

    [RedactAudit]
    [Required]
    public string TwoFactorAuthenticationKey { get; set; } = string.Empty;

    [Required]
    public DateTimeOffset Created { get; set; }

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

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

    [Required]
    [DefaultValue(1)]
    public long DomainId { get; set; }

    [ForeignKey(nameof(DomainId))]
    public virtual Domain.Domain Domain { get; set; } = null!;

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

    [RedactAudit]
    [Required]
    [DefaultValue("")]
    public string SsoSubject { get; set; } = string.Empty;

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

    [Required]
    [MaxLength(20)]
    [DefaultValue("en-GB")]
    public string PreferredLocale { get; set; } = "en-GB";

    public static void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>().HasData(new User
        {
            Id = 1,
            FirstName = "Test1",
            MiddleNames = string.Empty,
            LastName = "User",
            Active = true,
            Email = "testuser1@sun-strategy.com",
            Password = "AgAAAAIAACcQAAAAAAAAABDGmF9Hre4l8k9lkZAxBRJk0zNHD2b6xfKz4C7h6UjuirkoyoP2fVR6d9b7riT03UnL5yAgFh2pSSVQDx+nQ5PBjZRB9UG4u5FrY8W7ouA/+w==",
            EmailConfirmed = true,
            UsingTwoFactorAuthentication = false,
            TwoFactorAuthenticationKey = "4EHHG42OWCN3L72TSRYSHTV6MRJXVOY3",
            Created = new DateTimeOffset(2022, 06, 09, 12, 00, 00, TimeSpan.Zero),
            LastUpdated = new DateTimeOffset(2022, 06, 09, 12, 00, 00, TimeSpan.Zero),
            DomainId = 1,
            SsoProviderId = null,
            SsoSubject = string.Empty,
            Guid = new Guid("{30cfcd5b-3385-43f1-b59a-fd35236f3d92}"), 
            PreferredLocale = "en-GB"
        });
    }

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