package com.pandoli365.bibimbap.mapper;

import com.pandoli365.bibimbap.data.UserData;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface UsersMapper {

    @Select("""
            SELECT
                id,
                display_name AS displayName,
                canonical_email AS canonicalEmail,
                avatar_url AS avatarUrl,
                role,
                status,
                last_login_at AS lastLoginAt,
                created_at AS createdAt,
                updated_at AS updatedAt
            FROM users
            WHERE id = #{id}
              AND is_delete IS NOT TRUE
            """)
    UserData getUser(long id);

    @Insert("""
            INSERT INTO users (
                display_name,
                canonical_email,
                avatar_url,
                role,
                status,
                last_login_at
            ) VALUES (
                #{displayName},
                #{canonicalEmail},
                #{avatarUrl},
                #{role},
                #{status},
                #{lastLoginAt}
            )
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int addUser(UserData user);

    @Update("""
            UPDATE users
            SET
                display_name = #{displayName},
                canonical_email = #{canonicalEmail},
                avatar_url = #{avatarUrl},
                role = #{role},
                status = #{status},
                last_login_at = #{lastLoginAt},
                updated_at = now()
            WHERE id = #{id}
              AND is_delete IS NOT TRUE
            """)
    int updateUser(UserData user);
}
