<?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.ga.common.menu.MenuMapper">

    <resultMap id="MenuMap" type="com.ga.common.menu.MenuVO"/>

    <sql id="MenuColumns">
        menu_id, parent_menu_id, menu_code, menu_name, menu_type, menu_path, menu_icon,
        component_path, menu_level, sort_order, is_visible, is_active, description
    </sql>

    <select id="selectAllActive" resultMap="MenuMap">
        SELECT <include refid="MenuColumns"/>
        FROM menu
        WHERE is_active = 'Y' AND is_visible = 'Y'
        ORDER BY menu_level, sort_order, menu_id
    </select>

    <!-- 사용자가 접근 가능한 메뉴: 사용자 → 역할 → role_menu_permission → 메뉴 -->
    <select id="selectAccessibleMenusByUserId" resultMap="MenuMap">
        SELECT DISTINCT
            m.menu_id, m.parent_menu_id, m.menu_code, m.menu_name, m.menu_type,
            m.menu_path, m.menu_icon, m.component_path, m.menu_level, m.sort_order,
            m.is_visible, m.is_active, m.description
        FROM menu m
        JOIN role_menu_permission rmp ON rmp.menu_id = m.menu_id AND rmp.is_granted = 'Y'
        JOIN user_role ur              ON ur.role_id = rmp.role_id
        WHERE ur.user_id = #{userId}
          AND m.is_active = 'Y'
          AND m.is_visible = 'Y'
          AND rmp.perm_code = 'READ'
        ORDER BY m.menu_level, m.sort_order, m.menu_id
    </select>

    <!-- 사용자가 보유한 모든 (menu_id, perm_code) -->
    <select id="selectUserPermissions" resultType="map">
        SELECT rmp.menu_id  AS "menuId",
               rmp.perm_code AS "permCode"
        FROM role_menu_permission rmp
        JOIN user_role ur ON ur.role_id = rmp.role_id
        WHERE ur.user_id = #{userId} AND rmp.is_granted = 'Y'
    </select>

    <select id="hasPermission" resultType="int">
        SELECT COUNT(*)
        FROM role_menu_permission rmp
        JOIN user_role ur ON ur.role_id = rmp.role_id
        JOIN menu m       ON m.menu_id = rmp.menu_id
        WHERE ur.user_id = #{userId}
          AND m.menu_code = #{menuCode}
          AND rmp.perm_code = #{permCode}
          AND rmp.is_granted = 'Y'
    </select>

    <select id="selectById" resultMap="MenuMap">
        SELECT <include refid="MenuColumns"/> FROM menu WHERE menu_id = #{menuId}
    </select>

    <select id="selectByCode" resultMap="MenuMap">
        SELECT <include refid="MenuColumns"/> FROM menu WHERE menu_code = #{menuCode}
    </select>

    <insert id="insert" parameterType="com.ga.common.menu.MenuVO" useGeneratedKeys="true" keyProperty="menuId">
        INSERT INTO menu (
            parent_menu_id, menu_code, menu_name, menu_type, menu_path, menu_icon,
            component_path, menu_level, sort_order, is_visible, is_active, description
        ) VALUES (
            #{parentMenuId}, #{menuCode}, #{menuName}, #{menuType}, #{menuPath}, #{menuIcon},
            #{componentPath}, #{menuLevel}, COALESCE(#{sortOrder},0),
            COALESCE(#{isVisible},'Y'), COALESCE(#{isActive},'Y'), #{description}
        )
    </insert>

    <update id="update" parameterType="com.ga.common.menu.MenuVO">
        UPDATE menu SET
            parent_menu_id = #{parentMenuId},
            menu_name      = #{menuName},
            menu_type      = #{menuType},
            menu_path      = #{menuPath},
            menu_icon      = #{menuIcon},
            component_path = #{componentPath},
            menu_level     = #{menuLevel},
            sort_order     = #{sortOrder},
            is_visible     = #{isVisible},
            is_active      = #{isActive},
            description    = #{description}
        WHERE menu_id = #{menuId}
    </update>

    <delete id="deleteById">
        DELETE FROM menu WHERE menu_id = #{menuId}
    </delete>

    <select id="countChildren" resultType="int">
        SELECT COUNT(*) FROM menu WHERE parent_menu_id = #{menuId}
    </select>

</mapper>
