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

  <!--
    component_standards 테이블
    Node.js componentStandardService.ts 포팅

    company_code 필터:
      - SUPER_ADMIN("*") → 전체 조회
      - 일반 사용자 → is_public='Y' OR company_code=? (공개 + 자사)
  -->

  <!-- ─────────────────────────────────────────────────────── -->
  <!-- 공통 WHERE 조건 fragments                              -->
  <!-- ─────────────────────────────────────────────────────── -->

  <!-- 목록 조회용 필터 (active, category, is_public, company, search) -->
  <sql id="componentFilter">
    <if test="active != null">
      AND is_active = #{active}
    </if>
    <if test='category != null and category != "all"'>
      AND category = #{category}
    </if>
    <if test="is_public != null">
      AND is_public = #{is_public}
    </if>
    <if test='company_code != null and company_code != "*"'>
      AND (is_public = 'Y' OR (company_code = #{company_code} OR company_code = '*'))
    </if>
    <if test="search_pattern != null">
      AND (component_name   ILIKE #{search_pattern}
        OR component_name_eng ILIKE #{search_pattern}
        OR description        ILIKE #{search_pattern})
    </if>
  </sql>

  <!-- 카테고리/통계용 필터 (active + company) -->
  <sql id="publicCompanyFilter">
    AND is_active = 'Y'
    <if test='company_code != null and company_code != "*"'>
      AND (is_public = 'Y' OR (company_code = #{company_code} OR company_code = '*'))
    </if>
  </sql>

  <!-- ─────────────────────────────────────────────────────── -->
  <!-- 목록 조회                                               -->
  <!-- ─────────────────────────────────────────────────────── -->

  <select id="selectComponentList" parameterType="map" resultType="map">
    SELECT *

    FROM component_standards

    WHERE 1=1
    <include refid="componentFilter"/>

    ORDER BY ${sortColumn} ${sortOrder}
    <include refid="common.pagination"/>
  </select>

  <select id="countComponents" parameterType="map" resultType="int">
    SELECT COUNT(*)

    FROM component_standards

    WHERE 1=1
    <include refid="componentFilter"/>
  </select>

  <!-- ─────────────────────────────────────────────────────── -->
  <!-- 단건 조회                                               -->
  <!-- ─────────────────────────────────────────────────────── -->

  <select id="selectComponent" parameterType="map" resultType="map">
    SELECT *

    FROM component_standards

    WHERE component_code = #{component_code}
  </select>

  <!-- ─────────────────────────────────────────────────────── -->
  <!-- 생성                                                    -->
  <!-- ─────────────────────────────────────────────────────── -->

  <insert id="insertComponent" parameterType="map">
    INSERT INTO component_standards (
      component_code,
      component_name,
      component_name_eng,
      description,
      category,
      icon_name,
      default_size,
      component_config,
      preview_image,
      sort_order,
      is_active,
      is_public,
      company_code,
      created_by,
      updated_by,
      created_date,
      updated_date
    ) VALUES (
      #{component_code},
      #{component_name},
      #{component_name_eng, jdbcType=VARCHAR},
      #{description, jdbcType=VARCHAR},
      #{category},
      #{icon_name, jdbcType=VARCHAR},
      #{default_size, jdbcType=OTHER}::jsonb,
      #{component_config, jdbcType=OTHER}::jsonb,
      #{preview_image, jdbcType=VARCHAR},
      #{sort_order, jdbcType=INTEGER},
      #{is_active},
      #{is_public},
      #{company_code},
      #{created_by, jdbcType=VARCHAR},
      #{updated_by, jdbcType=VARCHAR},
      NOW(),
      NOW()
    )
  </insert>

  <!-- ─────────────────────────────────────────────────────── -->
  <!-- 수정 (동적 UPDATE)                                      -->
  <!-- ─────────────────────────────────────────────────────── -->

  <update id="updateComponent" parameterType="map">
    UPDATE component_standards
    <set>
      <if test="component_name != null">component_name = #{component_name},</if>
      <if test="component_name_eng != null">component_name_eng = #{component_name_eng},</if>
      <if test="description != null">description = #{description},</if>
      <if test="category != null">category = #{category},</if>
      <if test="icon_name != null">icon_name = #{icon_name},</if>
      <if test="default_size != null">default_size = #{default_size, jdbcType=OTHER}::jsonb,</if>
      <if test="component_config != null">component_config = #{component_config, jdbcType=OTHER}::jsonb,</if>
      <if test="preview_image != null">preview_image = #{preview_image},</if>
      <if test="sort_order != null">sort_order = #{sort_order},</if>
      <if test="is_active != null">is_active = #{is_active},</if>
      <if test="is_public != null">is_public = #{is_public},</if>
      <if test="updated_by != null">updated_by = #{updated_by},</if>
      updated_date = NOW()
    </set>

    WHERE component_code = #{component_code}
  </update>

  <!-- ─────────────────────────────────────────────────────── -->
  <!-- 삭제                                                    -->
  <!-- ─────────────────────────────────────────────────────── -->

  <delete id="deleteComponent" parameterType="map">
    DELETE FROM component_standards

    WHERE component_code = #{component_code}
  </delete>

  <!-- ─────────────────────────────────────────────────────── -->
  <!-- 정렬 순서 업데이트 (단건, 서비스에서 반복 호출)         -->
  <!-- ─────────────────────────────────────────────────────── -->

  <update id="updateSortOrder" parameterType="map">
    UPDATE component_standards
    SET sort_order   = #{sort_order},
        updated_date = NOW()

    WHERE component_code = #{component_code}
  </update>

  <!-- ─────────────────────────────────────────────────────── -->
  <!-- 카테고리 목록                                           -->
  <!-- ─────────────────────────────────────────────────────── -->

  <select id="selectCategories" parameterType="map" resultType="map">
    SELECT DISTINCT category

    FROM component_standards

    WHERE 1=1
    <include refid="publicCompanyFilter"/>

    ORDER BY category
  </select>

  <!-- ─────────────────────────────────────────────────────── -->
  <!-- 통계                                                    -->
  <!-- ─────────────────────────────────────────────────────── -->

  <select id="countStatisticsTotal" parameterType="map" resultType="int">
    SELECT COUNT(*)

    FROM component_standards

    WHERE 1=1
    <include refid="publicCompanyFilter"/>
  </select>

  <select id="selectStatisticsByCategory" parameterType="map" resultType="map">
    SELECT category, COUNT(*) AS count

    FROM component_standards

    WHERE 1=1
    <include refid="publicCompanyFilter"/>

    GROUP BY category
  </select>

  <!-- 상태별 통계: Node.js 원본과 동일하게 company_code 필터 없음 -->
  <select id="selectStatisticsByStatus" resultType="map">
    SELECT is_active, COUNT(*) AS count

    FROM component_standards

    GROUP BY is_active
  </select>

  <!-- ─────────────────────────────────────────────────────── -->
  <!-- 중복 코드 체크                                          -->
  <!-- ─────────────────────────────────────────────────────── -->

  <select id="checkDuplicate" parameterType="map" resultType="map">
    SELECT component_code

    FROM component_standards

    WHERE component_code = #{component_code}
    <if test='company_code != null and company_code != "*"'>
      AND (company_code = #{company_code} OR company_code = '*')
    </if>
    LIMIT 1
  </select>

</mapper>
