<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zioinfo.mall.auth.mapper.UserMapper">

  <resultMap id="userMap" type="com.zioinfo.mall.auth.MallUser">
    <id property="id" column="id"/>
    <result property="username" column="username"/>
    <result property="passwordHash" column="password_hash"/>
    <result property="role" column="role"/>
    <result property="displayName" column="display_name"/>
    <result property="active" column="is_active"/>
    <result property="createdAt" column="created_at"/>
    <!-- UIWS 2FA 이식 컬럼 (mall_account ALTER, db/91_uiws_port.sql) -->
    <result property="email" column="email"/>
    <result property="emailVerifyCode" column="email_verify_code"/>
    <result property="emailVerifyExpire" column="email_verify_expire"/>
    <result property="loginFailCount" column="login_fail_count"/>
    <result property="locked" column="locked"/>
    <result property="otpSecret" column="otp_secret"/>
    <result property="otpEnabled" column="otp_enabled"/>
    <!-- 로그인 보조 이식: 회원가입 승인 게이트 -->
    <result property="approved" column="approved"/>
  </resultMap>

  <select id="findByUsername" resultMap="userMap">
    SELECT id, username, password_hash, role, display_name, is_active, created_at,
           email, email_verify_code, email_verify_expire, login_fail_count, locked, otp_secret, otp_enabled, approved
    FROM mall_account
    WHERE username = #{username}
  </select>

  <insert id="insert" parameterType="com.zioinfo.mall.auth.MallUser"
          useGeneratedKeys="true" keyProperty="id">
    INSERT INTO mall_account (username, password_hash, role, display_name, is_active)
    VALUES (#{username}, #{passwordHash}, #{role}, #{displayName}, #{active})
  </insert>

  <select id="countByUsername" resultType="int">
    SELECT COUNT(*) FROM mall_account WHERE username = #{username}
  </select>

  <!-- ── 로그인 보조 이식: 회원가입 / 아이디찾기 / 비밀번호 초기화 ─────────────── -->

  <select id="countByEmail" resultType="int">
    SELECT COUNT(*) FROM mall_account WHERE email = #{email}
  </select>

  <!-- 회원가입(승인 대기): 운영자 신청 → role=MANAGER·is_active=true·approved=false·실패카운트0·미잠금 고정 -->
  <insert id="signup" parameterType="com.zioinfo.mall.auth.MallUser"
          useGeneratedKeys="true" keyProperty="id">
    INSERT INTO mall_account (username, password_hash, display_name, role, email,
                              is_active, approved, login_fail_count, locked)
    VALUES (#{username}, #{passwordHash}, #{displayName}, 'MANAGER', #{email},
            true, false, 0, false)
  </insert>

  <select id="findByDisplayNameAndEmail" resultMap="userMap">
    SELECT id, username, password_hash, role, display_name, is_active, created_at,
           email, email_verify_code, email_verify_expire, login_fail_count, locked, otp_secret, otp_enabled, approved
    FROM mall_account
    WHERE display_name = #{displayName} AND email = #{email}
    ORDER BY id
    LIMIT 1
  </select>

  <select id="findByUsernameAndEmail" resultMap="userMap">
    SELECT id, username, password_hash, role, display_name, is_active, created_at,
           email, email_verify_code, email_verify_expire, login_fail_count, locked, otp_secret, otp_enabled, approved
    FROM mall_account
    WHERE username = #{username} AND email = #{email}
  </select>

  <!-- 임시비번 적용 + 잠금/실패카운트 해제(초기화 시) -->
  <update id="updatePasswordHash">
    UPDATE mall_account
    SET password_hash = #{passwordHash}, locked = false, login_fail_count = 0
    WHERE username = #{username}
  </update>

</mapper>
