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

    <resultMap id="userResultMap" type="com.tasteby.domain.UserInfo">
        <id property="id" column="id"/>
        <result property="email" column="email"/>
        <result property="nickname" column="nickname"/>
        <result property="avatarUrl" column="avatar_url"/>
        <result property="admin" column="is_admin" javaType="boolean"/>
        <result property="provider" column="provider"/>
        <result property="createdAt" column="created_at"/>
        <result property="favoriteCount" column="favorite_count"/>
        <result property="reviewCount" column="review_count"/>
        <result property="memoCount" column="memo_count"/>
    </resultMap>

    <select id="findByProviderAndProviderId" resultMap="userResultMap">
        SELECT id, email, nickname, avatar_url, is_admin, provider, created_at
        FROM tasteby_users
        WHERE provider = #{provider} AND provider_id = #{providerId}
    </select>

    <update id="updateLastLogin">
        UPDATE tasteby_users SET last_login_at = SYSTIMESTAMP WHERE id = #{id}
    </update>

    <insert id="insert">
        INSERT INTO tasteby_users (id, provider, provider_id, email, nickname, avatar_url)
        VALUES (#{id}, #{provider}, #{providerId}, #{email}, #{nickname}, #{avatarUrl})
    </insert>

    <select id="findById" resultMap="userResultMap">
        SELECT id, email, nickname, avatar_url, is_admin, provider, created_at
        FROM tasteby_users
        WHERE id = #{id}
    </select>

    <select id="findAllWithCounts" resultMap="userResultMap">
        SELECT u.id, u.email, u.nickname, u.avatar_url, u.provider, u.created_at,
               NVL(fav.cnt, 0) AS favorite_count,
               NVL(rev.cnt, 0) AS review_count,
               NVL(memo.cnt, 0) AS memo_count
        FROM tasteby_users u
        LEFT JOIN (SELECT user_id, COUNT(*) AS cnt FROM user_favorites GROUP BY user_id) fav ON fav.user_id = u.id
        LEFT JOIN (SELECT user_id, COUNT(*) AS cnt FROM user_reviews GROUP BY user_id) rev ON rev.user_id = u.id
        LEFT JOIN (SELECT user_id, COUNT(*) AS cnt FROM user_memos GROUP BY user_id) memo ON memo.user_id = u.id
        ORDER BY u.created_at DESC
        OFFSET #{offset} ROWS FETCH NEXT #{limit} ROWS ONLY
    </select>

    <select id="countAll" resultType="int">
        SELECT COUNT(*) FROM tasteby_users
    </select>

</mapper>
