<?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="dashboard">

    <!-- ═══════════════════════════════════════════════════════════════
         대시보드 = 사용자 메뉴 (MENU_INFO 단일 본체)
         - MENU_TYPE='1' AND MENU_URL ~ '^/\d+$' (회사별 시퀀스 숫자 URL) 인 row 가 대시보드
         - 개인 대시보드: USER_ID = 로그인 사용자
         - 공용 대시보드: USER_ID IS NULL (같은 COMPANY_CODE 공유)
         ═══════════════════════════════════════════════════════════════ -->

    <!-- ═══ 대시보드 CRUD ═══ -->

    <select id="getDashboardList" parameterType="map" resultType="map">
        SELECT OBJID
            , OBJID AS DASHBOARD_ID
            , MENU_NAME_KOR AS NAME
            , MENU_URL
            , MENU_ICON AS ICON
            , SEQ AS DISPLAY_ORDER
            , COMPANY_CODE
            , USER_ID
            , STATUS
            , WRITER AS CREATED_BY
            , CREATED_DATE
        FROM MENU_INFO
        WHERE MENU_TYPE = '1'
            AND MENU_URL ~ '^/\d+$'
            AND STATUS = 'active'
        <include refid="common.companyCodeFilter"/>
        AND (USER_ID = #{user_id} OR USER_ID IS NULL)
        <if test='keyword != null and keyword != ""'>
            AND MENU_NAME_KOR LIKE CONCAT('%', #{keyword}, '%')
        </if>
        ORDER BY CAST(COALESCE(NULLIF(SEQ, ''), '0') AS INTEGER) ASC, CREATED_DATE ASC
        <include refid="common.pagination"/>
    </select>

    <select id="getDashboardListCnt" parameterType="map" resultType="int">
        SELECT COUNT(*)
        FROM MENU_INFO
        WHERE MENU_TYPE = '1'
            AND MENU_URL ~ '^/\d+$'
            AND STATUS = 'active'
        <include refid="common.companyCodeFilter"/>
        AND (USER_ID = #{user_id} OR USER_ID IS NULL)
        <if test='keyword != null and keyword != ""'>
            AND MENU_NAME_KOR LIKE CONCAT('%', #{keyword}, '%')
        </if>
    </select>

    <select id="getDashboardInfo" parameterType="map" resultType="map">
        SELECT OBJID
            , OBJID AS DASHBOARD_ID
            , MENU_NAME_KOR AS NAME
            , MENU_URL
            , MENU_ICON AS ICON
            , SEQ AS DISPLAY_ORDER
            , COMPANY_CODE
            , USER_ID
            , STATUS
            , WRITER AS CREATED_BY
            , CREATED_DATE
        FROM MENU_INFO
        WHERE OBJID = #{objid}
            AND MENU_TYPE = '1'
            AND STATUS = 'active'
    </select>

    <!-- 회사별 다음 메뉴 시퀀스 (URL 식별자로 사용) -->
    <select id="getNextMenuSeq" parameterType="map" resultType="int">
        SELECT COALESCE(MAX(CAST(NULLIF(SEQ, '') AS INTEGER)), 0) + 1
        FROM MENU_INFO
        WHERE MENU_TYPE = '1'
            AND COMPANY_CODE = #{company_code}
            AND MENU_URL ~ '^/\d+$'
    </select>

    <insert id="insertDashboard" parameterType="map">
        INSERT INTO MENU_INFO (
            OBJID
            , MENU_TYPE
            , PARENT_OBJ_ID
            , MENU_NAME_KOR
            , MENU_URL
            , SEQ
            , WRITER
            , CREATED_DATE
            , STATUS
            , COMPANY_CODE
            , MENU_ICON
            , USER_ID
        ) VALUES (
            #{objid}
            , '1'
            , '0'
            , #{name}
            , #{menu_url}
            , #{seq}
            , #{writer}
            , NOW()
            , 'active'
            , #{company_code}
            , COALESCE(#{icon}, '📋')
            , #{user_id}
        )
    </insert>

    <update id="updateDashboard" parameterType="map">
        UPDATE MENU_INFO
        SET
            <if test='name != null'>MENU_NAME_KOR = #{name},</if>
            <if test='icon != null'>MENU_ICON = #{icon},</if>
            <if test='display_order != null'>SEQ = #{display_order},</if>
            OBJID = OBJID
        WHERE OBJID = #{objid}
            AND MENU_TYPE = '1'
    </update>

    <update id="deleteDashboard" parameterType="map">
        UPDATE MENU_INFO
        SET STATUS = 'inactive'
        WHERE OBJID = #{objid}
            AND MENU_TYPE = '1'
    </update>

    <!-- ═══ 대시보드 카드 ═══ -->

    <select id="getDashboardCardList" parameterType="map" resultType="map">
        SELECT DC.CARD_ID
            , DC.MENU_OBJID
            , DC.MENU_OBJID AS DASHBOARD_ID
            , DC.TEMPLATE_ID
            , DC.POSITION_X
            , DC.POSITION_Y
            , DC.WIDTH
            , DC.HEIGHT
            , DC.IS_COLLAPSED
            , DC.DISPLAY_ORDER
            , DC.IS_ACTIVE
            , DC.CREATED_DATE
            , DC.UPDATED_DATE
            , T.NAME AS TEMPLATE_NAME
            , T.CATEGORY AS TEMPLATE_CATEGORY
            , T.DESCRIPTION AS TEMPLATE_DESCRIPTION
            , T.PRIMARY_TABLE
            , T.STATUS AS TEMPLATE_STATUS
        FROM DASHBOARD_CARDS DC
        LEFT JOIN TEMPLATES T ON DC.TEMPLATE_ID = T.TEMPLATE_ID AND T.IS_ACTIVE = 'Y'
        WHERE DC.MENU_OBJID = #{menu_objid}
            AND DC.IS_ACTIVE = 'Y'
        ORDER BY DC.DISPLAY_ORDER ASC, DC.CREATED_DATE ASC
    </select>

    <insert id="insertDashboardCard" parameterType="map">
        INSERT INTO DASHBOARD_CARDS (
            CARD_ID
            , MENU_OBJID
            , TEMPLATE_ID
            , POSITION_X
            , POSITION_Y
            , WIDTH
            , HEIGHT
            , IS_COLLAPSED
            , DISPLAY_ORDER
            , IS_ACTIVE
            , CREATED_DATE
            , UPDATED_DATE
        ) VALUES (
            #{card_id}
            , #{menu_objid}
            , #{template_id}
            , #{position_x}
            , #{position_y}
            , #{width}
            , #{height}
            , FALSE
            , #{display_order}
            , 'Y'
            , CURRENT_TIMESTAMP
            , CURRENT_TIMESTAMP
        )
    </insert>

    <update id="updateDashboardCard" parameterType="map">
        UPDATE DASHBOARD_CARDS
        SET UPDATED_DATE = CURRENT_TIMESTAMP
            <if test='position_x != null'>
                , POSITION_X = #{position_x}
            </if>
            <if test='position_y != null'>
                , POSITION_Y = #{position_y}
            </if>
            <if test='width != null'>
                , WIDTH = #{width}
            </if>
            <if test='height != null'>
                , HEIGHT = #{height}
            </if>
            <if test='is_collapsed != null'>
                , IS_COLLAPSED = #{is_collapsed}
            </if>
            <if test='display_order != null'>
                , DISPLAY_ORDER = #{display_order}
            </if>
        WHERE CARD_ID = #{card_id}
            AND IS_ACTIVE = 'Y'
    </update>

    <update id="updateCardPosition" parameterType="map">
        UPDATE DASHBOARD_CARDS
        SET POSITION_X = #{position_x}
            , POSITION_Y = #{position_y}
            , WIDTH = #{width}
            , HEIGHT = #{height}
            , IS_COLLAPSED = #{is_collapsed}
            , UPDATED_DATE = CURRENT_TIMESTAMP
        WHERE CARD_ID = #{card_id}
            AND IS_ACTIVE = 'Y'
    </update>

    <update id="deleteDashboardCard" parameterType="map">
        UPDATE DASHBOARD_CARDS
        SET IS_ACTIVE = 'D'
            , UPDATED_DATE = CURRENT_TIMESTAMP
        WHERE CARD_ID = #{card_id}
    </update>

</mapper>
