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

  <!-- ══════════════════════════════════════════════════════════════
       Screen Embedding
       ══════════════════════════════════════════════════════════════ -->

  <select id="getScreenEmbeddingList" parameterType="map" resultType="map">
    SELECT
      SE.*,
      PS.SCREEN_NAME AS PARENT_SCREEN_NAME,
      CS.SCREEN_NAME AS CHILD_SCREEN_NAME
    FROM SCREEN_EMBEDDING SE
    LEFT JOIN SCREEN_DEFINITIONS PS ON SE.PARENT_SCREEN_ID = PS.SCREEN_ID
    LEFT JOIN SCREEN_DEFINITIONS CS ON SE.CHILD_SCREEN_ID  = CS.SCREEN_ID
    WHERE SE.PARENT_SCREEN_ID = #{parent_screen_id}
    <include refid="common.companyCodeFilter"/>
    ORDER BY SE.POSITION, SE.CREATED_DATE
  </select>

  <select id="getScreenEmbeddingInfo" parameterType="map" resultType="map">
    SELECT
      SE.*,
      PS.SCREEN_NAME AS PARENT_SCREEN_NAME,
      CS.SCREEN_NAME AS CHILD_SCREEN_NAME
    FROM SCREEN_EMBEDDING SE
    LEFT JOIN SCREEN_DEFINITIONS PS ON SE.PARENT_SCREEN_ID = PS.SCREEN_ID
    LEFT JOIN SCREEN_DEFINITIONS CS ON SE.CHILD_SCREEN_ID  = CS.SCREEN_ID
    WHERE SE.ID = #{id}
    <include refid="common.companyCodeFilter"/>
  </select>

  <insert id="insertScreenEmbedding" parameterType="map"
          useGeneratedKeys="true" keyProperty="id" keyColumn="id">
    INSERT INTO SCREEN_EMBEDDING (
      PARENT_SCREEN_ID, CHILD_SCREEN_ID, POSITION, MODE,
      CONFIG, COMPANY_CODE, CREATED_BY, CREATED_DATE, UPDATED_DATE
    ) VALUES (
      #{parent_screen_id}, #{child_screen_id}, #{position}, #{mode},
      #{config}::JSONB, #{company_code}, #{user_id}, NOW(), NOW()
    )
  </insert>

  <update id="updateScreenEmbedding" parameterType="map">
    UPDATE SCREEN_EMBEDDING
    <set>
      <if test="position != null">POSITION  = #{position},</if>
      <if test="mode != null">MODE       = #{mode},</if>
      <if test="config != null">CONFIG     = #{config}::JSONB,</if>
      UPDATED_DATE = NOW()
    </set>
    WHERE ID = #{id}
    <include refid="common.companyCodeFilter"/>
  </update>

  <delete id="deleteScreenEmbedding" parameterType="map">
    DELETE FROM SCREEN_EMBEDDING
    WHERE ID = #{id}
    <include refid="common.companyCodeFilter"/>
  </delete>

  <!-- ══════════════════════════════════════════════════════════════
       Screen Data Transfer
       ══════════════════════════════════════════════════════════════ -->

  <select id="getScreenDataTransferInfo" parameterType="map" resultType="map">
    SELECT
      SDT.*,
      SS.SCREEN_NAME AS SOURCE_SCREEN_NAME,
      TS.SCREEN_NAME AS TARGET_SCREEN_NAME
    FROM SCREEN_DATA_TRANSFER SDT
    LEFT JOIN SCREEN_DEFINITIONS SS ON SDT.SOURCE_SCREEN_ID = SS.SCREEN_ID
    LEFT JOIN SCREEN_DEFINITIONS TS ON SDT.TARGET_SCREEN_ID = TS.SCREEN_ID
    WHERE SDT.SOURCE_SCREEN_ID = #{source_screen_id}
      AND SDT.TARGET_SCREEN_ID = #{target_screen_id}
    <include refid="common.companyCodeFilter"/>
  </select>

  <select id="getScreenDataTransferInfoById" parameterType="map" resultType="map">
    SELECT *
    FROM SCREEN_DATA_TRANSFER
    WHERE ID = #{id}
    <include refid="common.companyCodeFilter"/>
  </select>

  <insert id="insertScreenDataTransfer" parameterType="map"
          useGeneratedKeys="true" keyProperty="id" keyColumn="id">
    INSERT INTO SCREEN_DATA_TRANSFER (
      SOURCE_SCREEN_ID, TARGET_SCREEN_ID, SOURCE_COMPONENT_ID, SOURCE_COMPONENT_TYPE,
      DATA_RECEIVERS, BUTTON_CONFIG, COMPANY_CODE, CREATED_BY, CREATED_DATE, UPDATED_DATE
    ) VALUES (
      #{source_screen_id}, #{target_screen_id}, #{source_component_id}, #{source_component_type},
      #{data_receivers}::JSONB, #{button_config}::JSONB, #{company_code}, #{user_id}, NOW(), NOW()
    )
  </insert>

  <update id="updateScreenDataTransfer" parameterType="map">
    UPDATE SCREEN_DATA_TRANSFER
    <set>
      <if test="data_receivers != null">DATA_RECEIVERS = #{data_receivers}::JSONB,</if>
      <if test="button_config != null">BUTTON_CONFIG   = #{button_config}::JSONB,</if>
      UPDATED_DATE = NOW()
    </set>
    WHERE ID = #{id}
    <include refid="common.companyCodeFilter"/>
  </update>

  <delete id="deleteScreenDataTransfer" parameterType="map">
    DELETE FROM SCREEN_DATA_TRANSFER
    WHERE ID = #{id}
    <include refid="common.companyCodeFilter"/>
  </delete>

  <!-- ══════════════════════════════════════════════════════════════
       Screen Split Panel
       companyCodeFilter 없이 명시적 alias 필터 사용:
       SCREEN_EMBEDDING(LE, RE)도 COMPANY_CODE를 가지므로 SSP.COMPANY_CODE 로 한정
       ══════════════════════════════════════════════════════════════ -->

  <select id="getScreenSplitPanelInfo" parameterType="map" resultType="map">
    SELECT
      SSP.ID,
      SSP.SCREEN_ID,
      SSP.LEFT_EMBEDDING_ID,
      SSP.RIGHT_EMBEDDING_ID,
      SSP.DATA_TRANSFER_ID,
      SSP.LAYOUT_CONFIG,
      SSP.COMPANY_CODE,
      SSP.CREATED_DATE,
      SSP.UPDATED_DATE,
      LE.PARENT_SCREEN_ID    AS LE_PARENT_SCREEN_ID,
      LE.CHILD_SCREEN_ID     AS LE_CHILD_SCREEN_ID,
      LE.POSITION            AS LE_POSITION,
      LE.MODE                AS LE_MODE,
      LE.CONFIG              AS LE_CONFIG,
      RE.PARENT_SCREEN_ID    AS RE_PARENT_SCREEN_ID,
      RE.CHILD_SCREEN_ID     AS RE_CHILD_SCREEN_ID,
      RE.POSITION            AS RE_POSITION,
      RE.MODE                AS RE_MODE,
      RE.CONFIG              AS RE_CONFIG,
      SDT.SOURCE_SCREEN_ID,
      SDT.TARGET_SCREEN_ID,
      SDT.SOURCE_COMPONENT_ID,
      SDT.SOURCE_COMPONENT_TYPE,
      SDT.DATA_RECEIVERS,
      SDT.BUTTON_CONFIG
    FROM SCREEN_SPLIT_PANEL SSP
    LEFT JOIN SCREEN_EMBEDDING     LE  ON SSP.LEFT_EMBEDDING_ID  = LE.ID
    LEFT JOIN SCREEN_EMBEDDING     RE  ON SSP.RIGHT_EMBEDDING_ID = RE.ID
    LEFT JOIN SCREEN_DATA_TRANSFER SDT ON SSP.DATA_TRANSFER_ID   = SDT.ID
    WHERE SSP.SCREEN_ID = #{screen_id}
    <if test='company_code != null and company_code != "*"'>
      AND (SSP.COMPANY_CODE = #{company_code} OR SSP.COMPANY_CODE = '*')
      AND SSP.COMPANY_CODE != '*'
    </if>
  </select>

  <!-- 분할 패널 단건 조회 (by PK id): 삭제용 자식 ID 조회 및 생성 응답용 -->
  <select id="getScreenSplitPanelInfoById" parameterType="map" resultType="map">
    SELECT *
    FROM SCREEN_SPLIT_PANEL
    WHERE ID = #{id}
    <if test='company_code != null and company_code != "*"'>
      AND (COMPANY_CODE = #{company_code} OR COMPANY_CODE = '*')
      AND COMPANY_CODE != '*'
    </if>
  </select>

  <insert id="insertScreenSplitPanel" parameterType="map"
          useGeneratedKeys="true" keyProperty="id" keyColumn="id">
    INSERT INTO SCREEN_SPLIT_PANEL (
      SCREEN_ID, LEFT_EMBEDDING_ID, RIGHT_EMBEDDING_ID, DATA_TRANSFER_ID,
      LAYOUT_CONFIG, COMPANY_CODE, CREATED_DATE, UPDATED_DATE
    ) VALUES (
      #{screen_id}, #{left_embedding_id}, #{right_embedding_id}, #{data_transfer_id},
      #{layout_config}::JSONB, #{company_code}, NOW(), NOW()
    )
  </insert>

  <update id="updateScreenSplitPanel" parameterType="map">
    UPDATE SCREEN_SPLIT_PANEL
    SET LAYOUT_CONFIG = #{layout_config}::JSONB,
        UPDATED_DATE    = NOW()
    WHERE ID = #{id}
    <if test='company_code != null and company_code != "*"'>
      AND (COMPANY_CODE = #{company_code} OR COMPANY_CODE = '*')
      AND COMPANY_CODE != '*'
    </if>
  </update>

  <delete id="deleteScreenSplitPanel" parameterType="map">
    DELETE FROM SCREEN_SPLIT_PANEL
    WHERE ID = #{id}
    <if test='company_code != null and company_code != "*"'>
      AND (COMPANY_CODE = #{company_code} OR COMPANY_CODE = '*')
      AND COMPANY_CODE != '*'
    </if>
  </delete>

</mapper>
