<?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.datamate.common.auth.infrastructure.persistence.mapper.AuthMapper">

    <select id="findUserByUsername" resultType="com.datamate.common.auth.domain.model.AuthUserAccount">
        SELECT id,
               username,
               email,
               password_hash AS passwordHash,
               full_name     AS fullName,
               avatar_url    AS avatarUrl,
               organization,
               enabled,
               last_login_at AS lastLoginAt
        FROM users
        WHERE username = #{username}
        LIMIT 1
    </select>

    <select id="findUserById" resultType="com.datamate.common.auth.domain.model.AuthUserAccount">
        SELECT id,
               username,
               email,
               password_hash AS passwordHash,
               full_name     AS fullName,
               avatar_url    AS avatarUrl,
               organization,
               enabled,
               last_login_at AS lastLoginAt
        FROM users
        WHERE id = #{userId}
        LIMIT 1
    </select>

    <update id="updateLastLoginAt">
        UPDATE users
        SET last_login_at = NOW()
        WHERE id = #{userId}
    </update>

    <select id="findRolesByUserId" resultType="com.datamate.common.auth.domain.model.AuthRoleInfo">
        SELECT r.id,
               r.role_code AS roleCode,
               r.role_name AS roleName,
               r.description,
               r.enabled
        FROM t_auth_roles r
                 INNER JOIN t_auth_user_roles ur ON ur.role_id = r.id
        WHERE ur.user_id = #{userId}
        ORDER BY r.role_code
    </select>

    <select id="findPermissionCodesByUserId" resultType="string">
        SELECT DISTINCT p.permission_code
        FROM t_auth_permissions p
                 INNER JOIN t_auth_role_permissions rp ON rp.permission_id = p.id
                 INNER JOIN t_auth_user_roles ur ON ur.role_id = rp.role_id
        WHERE ur.user_id = #{userId}
          AND p.enabled = 1
        ORDER BY p.permission_code
    </select>

    <select id="listUsers" resultType="com.datamate.common.auth.domain.model.AuthUserSummary">
        SELECT id,
               username,
               email,
               full_name AS fullName,
               enabled
        FROM users
        ORDER BY id ASC
    </select>

    <select id="listRoles" resultType="com.datamate.common.auth.domain.model.AuthRoleInfo">
        SELECT id,
               role_code AS roleCode,
               role_name AS roleName,
               description,
               enabled
        FROM t_auth_roles
        ORDER BY role_code ASC
    </select>

    <select id="listPermissions" resultType="com.datamate.common.auth.domain.model.AuthPermissionInfo">
        SELECT id,
               permission_code AS permissionCode,
               permission_name AS permissionName,
               module,
               action,
               path_pattern AS pathPattern,
               method,
               enabled
        FROM t_auth_permissions
        ORDER BY module ASC, action ASC
    </select>

    <select id="countRolesByIds" resultType="int">
        SELECT COUNT(1)
        FROM t_auth_roles
        WHERE id IN
        <foreach collection="roleIds" item="roleId" open="(" separator="," close=")">
            #{roleId}
        </foreach>
    </select>

    <delete id="deleteUserRoles">
        DELETE
        FROM t_auth_user_roles
        WHERE user_id = #{userId}
    </delete>

    <insert id="insertUserRoles">
        INSERT INTO t_auth_user_roles (user_id, role_id)
        VALUES
        <foreach collection="roleIds" item="roleId" separator=",">
            (#{userId}, #{roleId})
        </foreach>
    </insert>
</mapper>

