using Microsoft.EntityFrameworkCore;
using bitforum.Models.Account;

namespace bitforum.Repository
{
    public interface IMemberRepository
    {
        Task<bool> IsExistID(int id);
        Task<bool> IsExistEmail(string email);
        Task<bool> IsExistNickname(string name);
        Task<Member?> FindMemberByID(int id);
        Task<Member?> FindNonMemberByEmail(string email);
        Task<Member?> FindMemberByEmail(string email);
    }

    public class MemberRepository: IMemberRepository
    {
        private readonly DefaultDbContext _db;

        public MemberRepository(DefaultDbContext db)
        {
            _db = db;
        }

        // 회원 존재 여부
        public async Task<bool> IsExistID(int id)
        {
            return await _db.Member.AnyAsync(c => c.ID == id);
        }

        // 회원 존재 여부
        public async Task<bool> IsExistEmail(string email)
        {
            return await _db.Member.AnyAsync(c => c.Email == email);
        }

        // 별명 중복 여부
        public async Task<bool> IsExistNickname(string name)
        {
            return await _db.Member.AnyAsync(c => c.Name == name);
        }

        // 인증 회원 정보 조회(PK)
        public async Task<Member?> FindMemberByID(int id)
        {
            return await _db.Member.FirstOrDefaultAsync(c => c.ID == id && c.IsEmailVerified && !c.IsWithdraw);
        }

        // 비인증 회원 정보 조회(이메일)
        public async Task<Member?> FindNonMemberByEmail(string email)
        {
            return await _db.Member.FirstOrDefaultAsync(c => c.Email == email && !c.IsEmailVerified && !c.IsWithdraw);
        }

        // 인증 회원 정보 조회(이메일)
        public async Task<Member?> FindMemberByEmail(string email)
        {
            return await _db.Member.FirstOrDefaultAsync(c => c.Email == email && c.IsEmailVerified && !c.IsWithdraw);
        }
    }
}
