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

/// <summary>
/// 사용자단 일반 회원 정보
/// </summary>
namespace bitforum.Models.Account
{
    [Table("Member")]
    [Comment("회원 정보")]
    [Index(nameof(SID), Name = "IX_Member_SID", IsUnique = true)]
    [Index(nameof(Email), Name = "IX_Member_Email", IsUnique = true)]
    [Index(nameof(Name), Name = "IX_Member_Name", IsUnique = true)]
    [Index(nameof(FullName), Name = "IX_Member_FullName")]
    [Index(nameof(Gender), Name = "IX_Member_Gender")]
    [Index(nameof(Phone), Name = "IX_Member_Phone")]
    [Index(nameof(IsEmailVerified), Name = "IX_Member_IsEmailVerified")]
    [Index(nameof(IsAuthCertified), Name = "IX_Member_IsAuthCertified")]
    [Index(nameof(IsDenied), Name = "IX_Member_IsDenied")]
    [Index(nameof(IsAdmin), Name = "IX_Member_IsAdmin")]
    [Index(nameof(IsWithdraw), Name = "IX_Member_IsWithdraw")]
    [Index(nameof(CreatedAt), Name = "IX_Member_CreatedAt")]
    [Index(nameof(DeletedAt), Name = "IX_Member_DeletedAt")]
    public class Member
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Comment("PK")]
        public int ID { get; set; }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        // 1:1 관계
        [ForeignKey("GradeID")]
        public virtual MemberGrade? MemberGrade { get; set; } // 회원 등급 정보
        public virtual MemberApprove? MemberApprove { get; set; } // 알림/약관 동의 여부

        // 1:N 관계
        public virtual ICollection<LoginLog>? LoginLog { get; set; } // 로그인 기록
        public virtual ICollection<EmailLog>? EmailLog { get; set; } // 이메일 발송 기록
        public virtual ICollection<NameChangeLog>? NameChangeLog { get; set; } // 별명 변경 기록
        public virtual ICollection<EmailChangeLog>? EmailChangeLog { get; set; } // 이메일 변경 기록

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        [DisplayName("회원등급 ID")]
        [Comment("회원등급 ID")]
        public int? GradeID { get; set; } = null;

        [Required]
        [DisplayName("SID")]
        [Comment("SID")]
        [StringLength(20)]
        public string SID { get; set; } = Guid.NewGuid().ToString().Substring(0, 20); // 무작위 문자열

        [Required]
        [DisplayName("이메일")]
        [Comment("이메일")]
        [DataType(DataType.EmailAddress)]
        [StringLength(60)]
        public string Email { get; set; } = null!;

        [DisplayName("별명")]
        [Comment("별명")]
        [StringLength(20)]
        public string? Name { get; set; } = null;

        [DisplayName("본명")]
        [Comment("본명")]
        [StringLength(40)]
        public string? FullName { get; set; } = null;

        [DisplayName("본명(성)")]
        [Comment("본명(성)")]
        [StringLength(20)]
        public string? FirstName { get; set; } = null;

        [DisplayName("본명(이름)")]
        [Comment("본명(이름)")]
        [StringLength(40)]
        public string? LastName { get; set; } = null;

        [DisplayName("비밀번호")]
        [Comment("비밀번호")]
        [StringLength(255)]
        [DataType(DataType.Password)]
        public string? Password { get; set; } = string.Empty; // 관리자 수정 시 필요

        [DisplayName("자기소개")]
        [Comment("자기소개")]
        [StringLength(1000)]
        public string? Intro { get; set; } = null;

        [DisplayName("한마디")]
        [Comment("한마디")]
        [StringLength(50)]
        public string? Summary { get; set; } = null;

        [DisplayName("코인")]
        [Comment("코인")]
        public long Coin { get; set; } = 0;

        [DisplayName("경험치")]
        [Comment("경험치")]
        public int Exp { get; set; } = 0;

        [DisplayName("연락처")]
        [Comment("연락처")]
        [StringLength(15)]
        [RegularExpression(@"^010-\d{4}-\d{4}$", ErrorMessage = "전화번호 형식은 010-0000-0000 이어야 합니다.")]
        public string? Phone { get; set; } = null;

        [DisplayName("생년월일")]
        [Comment("생년월일")]
        public DateOnly? Birthday { get; set; } = null;

        [DisplayName("성별")]
        [Comment("성별")]
        [EnumDataType(typeof(Gender))]
        public Gender? Gender { get; set; } = null;

        [DisplayName("사진")]
        [Comment("사진")]
        [StringLength(255)]
        public string? Photo { get; set; } = null;

        [DisplayName("아이콘")]
        [Comment("아이콘")]
        [StringLength(255)]
        public string? Icon { get; set; } = null;

        [DisplayName("이메일 인증 여부")]
        [Comment("이메일 인증 여부")]
        public bool IsEmailVerified { get; set; } = false;

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

        [DisplayName("차단 여부")]
        [Comment("차단 여부")]
        public bool IsDenied { get; set; } = false;

        [DisplayName("운영진 여부")]
        [Comment("운영진 여부")]
        public bool IsAdmin { get; set; } = false;

        [DisplayName("탈퇴 여부")]
        [Comment("탈퇴 여부")]
        public bool IsWithdraw { get; set; } = false;

        [DisplayName("구독 중")]
        [Comment("구독 중")]
        public int Following { get; set; } = 0;

        [DisplayName("구독자")]
        [Comment("구독자")]
        public int Followed { get; set; } = 0;

        [DisplayName("로그인 단말기 정보")]
        [Comment("로그인 단말기 정보")]
        [StringLength(400)]
        public string? DeviceInfo { get; set; } = null;

        [DisplayName("회원가입 시 IP")]
        [Comment("회원가입 시 IP")]
        [StringLength(15)]
        public string SignupIP { get; set; } = string.Empty;

        [DisplayName("마지막 로그인 IP")]
        [Comment("마지막 로그인 IP")]
        [StringLength(15)]
        public string? LastLoginIp { get; set; } = null;

        [DisplayName("마지막 로그인 일시")]
        [Comment("마지막 로그인 일시")]
        public DateTime? LastLoginAt { get; set; } = null;

        [DisplayName("마지막 이메일 변경 일시")]
        [Comment("마지막 이메일 변경 일시")]
        public DateTime? LastEmailChangedAt { get; set; } = null;

        [DisplayName("마지막 별명 변경 일시")]
        [Comment("마지막 별명 변경 일시")]
        public DateTime? LastNameChangedAt { get; set; } = null;

        [DisplayName("이메일 인증 일시")]
        [Comment("이메일 인증 일시")]
        public DateTime? EmailVerifiedAt { get; set; } = null;

        [DisplayName("본인인증 일시")]
        [Comment("본인인증 일시")]
        public DateTime? AuthCertifiedAt { get; set; } = null;

        [DisplayName("비밀번호 변경 일시")]
        [Comment("비밀번호 변경 일시")]
        public DateTime PasswordUpdatedAt { get; set; }

        [DisplayName("가입 일시")]
        [Comment("가입 일시")]
        public DateTime CreatedAt { get; set; }

        [DisplayName("수정 일시")]
        [Comment("수정 일시")]
        public DateTime? UpdatedAt { get; set; } = null;

        [DisplayName("탈퇴 일시")]
        [Comment("탈퇴 일시")]
        public DateTime? DeletedAt { get; set; } = null;
    }
}
