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

namespace e_suite.Database.Core.Tables.Mail;

[DisplayName("Mail Template")]
[Table("MailTemplates", Schema = "Mail")]
[Index(nameof(DomainId), nameof(MailType), IsUnique = true)]
[Index(nameof(Guid), IsUnique = true)]
public class MailTemplate : IGeneralId, ISoftDeletable
{
    [Key]
    public long Id { get; set; }

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

    [Required]
    public MailType MailType { get; set; }

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

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

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

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

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

    public static void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<MailTemplate>().HasData(new MailTemplate
        {
            Id = 1,
            Guid = new Guid("{32B657AF-5E17-47F3-B8DC-59580516B141}"),
            DomainId = 1,
            MailType = MailType.ConfirmEmailAddress,
            Subject = "Confirmation e-mail",
            TemplateDefinition = "<h1>Please confirm your e-mail</h1> Welcome to .  <a href=\"$url\">Please click on this link to confirm your e-mail address is correct.</a>",
            Deleted = false,
        });

        modelBuilder.Entity<MailTemplate>().HasData(new MailTemplate
        {
            Id = 2,
            Guid = new Guid("{77FFF2B0-DD8C-43E6-A9FD-304C79850F81}"),
            DomainId = 1,
            MailType = MailType.PasswordReset,
            Subject = "Password reset",
            TemplateDefinition = "<h1>Please follow the link to reset your password</h1><br><a href=\"$url\">Reset Password</a>",
            Deleted = false,
        });

        modelBuilder.Entity<MailTemplate>().HasData(new MailTemplate
        {
            Id = 3,
            Guid = new Guid("{2E2D84A5-BE1C-4E3B-A86A-BDAFED42801B}"),
            DomainId = 1,
            MailType = MailType.DisableAuthenticator,
            Subject = "Disable two factor authentication",
            TemplateDefinition = "<h1>Please follow the link to reset two factor authentication</h1><br><a href=\"$url\">Disable two factor authentication</a>",
            Deleted = false,
        });

        modelBuilder.Entity<MailTemplate>().HasData(new MailTemplate
        {
            Id = 4,
            Guid = new Guid("{421B67C7-5F4B-4CAD-ADC5-528E2921790C}"),
            DomainId = 1,
            MailType = MailType.PasswordResetCompleted,
            Subject = "Password successfully reset",
            TemplateDefinition = "<h1>Your password has been reset. Please contact your admin if this wasn't you.</h1>",
            Deleted = false,
        });
    }
}