using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using bitforum.Constants;

namespace bitforum.Models.Account
{
    [Table("EmailVerifyToken")]
    [Comment("이메일 인증 토큰들")]
    [Index(nameof(Type), Name = "IX_EmailVerifyToken_Type")]
    [Index(nameof(Email), Name = "IX_EmailVerifyToken_Email")]
    [Index(nameof(Token), Name = "IX_EmailVerifyToken_Token")]
    [Index(nameof(IsVerified), Name = "IX_EmailVerifyToken_IsVerified")]
    [Index(nameof(Expiration), Name = "IX_EmailVerifyToken_Expiration")]
    public class EmailVerifyToken
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Comment("PK")]
        public int ID { get; set; }

        [Comment("인증 유형 (이메일 인증 / 비밀번호 재설정)")]
        public required VerificationType Type { get; set; }

        [Comment("이메일")]
        [MaxLength(60)]
        public required string Email { get; set; }

        [Comment("Token")]
        [MaxLength(256)]
        public required string Token { get; set; }

        [Comment("인증 여부")]
        public bool IsVerified { get; set; } = false;

        [Comment("만료 일시")]
        public required DateTime Expiration { get; set; }

        [Comment("추가 정보(JSON)")]
        public string? Additional { get; set; } = null;

        [Comment("등록 일시")]
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

        [NotMapped]
        public AdditionalData? AdditionalData
        {
            get => Additional == null ? null : System.Text.Json.JsonSerializer.Deserialize<AdditionalData>(Additional);
            set => Additional = value == null ? null : System.Text.Json.JsonSerializer.Serialize(value);
        }
    }
}
