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

  <!-- ═══ 테이블 목록 (public 스키마, 시스템 테이블 제외) ═══ -->
  <select id="getMetaTableList" parameterType="map" resultType="map">
    SELECT
        T.TABLE_NAME
        , COALESCE(TL.TABLE_LABEL, T.TABLE_NAME) AS TABLE_LABEL
        , (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS C
           WHERE C.TABLE_SCHEMA = 'public' AND C.TABLE_NAME = T.TABLE_NAME) AS COLUMN_COUNT
        , CASE WHEN EXISTS(
            SELECT 1 FROM TABLE_TYPE_COLUMNS TTC
            WHERE TTC.TABLE_NAME = T.TABLE_NAME
            <if test='company_code != null and company_code != "*"'>
              AND (TTC.COMPANY_CODE = #{company_code} OR TTC.COMPANY_CODE = '*')
            </if>
          ) THEN true ELSE false END AS HAS_CUSTOM_META
    FROM INFORMATION_SCHEMA.TABLES T
    LEFT JOIN TABLE_LABELS TL
      ON T.TABLE_NAME = TL.TABLE_NAME
    WHERE T.TABLE_SCHEMA = 'public'
      AND T.TABLE_TYPE = 'BASE TABLE'
      AND T.TABLE_NAME NOT LIKE 'pg_%'
      AND T.TABLE_NAME NOT IN ('spatial_ref_sys')
    ORDER BY T.TABLE_NAME
  </select>

  <!-- ═══ 특정 테이블의 컬럼 정보 (information_schema) ═══ -->
  <select id="getSchemaColumns" parameterType="map" resultType="map">
    SELECT
        COLUMN_NAME
        , DATA_TYPE
        , IS_NULLABLE
        , COLUMN_DEFAULT
        , CHARACTER_MAXIMUM_LENGTH
        , ORDINAL_POSITION
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = 'public'
      AND TABLE_NAME = #{table_name}
    ORDER BY ORDINAL_POSITION
  </select>

  <!-- ═══ PK 컬럼 목록 ═══ -->
  <select id="getPrimaryKeys" parameterType="map" resultType="string">
    SELECT KCU.COLUMN_NAME
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
    JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU
        ON TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME
        AND TC.TABLE_SCHEMA = KCU.TABLE_SCHEMA
    WHERE TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
      AND TC.TABLE_SCHEMA = 'public'
      AND TC.TABLE_NAME = #{table_name}
  </select>

  <!-- ═══ TABLE_TYPE_COLUMNS 커스텀 메타 (회사 우선, '*' 폴백) ═══ -->
  <select id="getCustomMeta" parameterType="map" resultType="map">
    SELECT DISTINCT ON (COLUMN_NAME)
        COLUMN_NAME
        , COLUMN_LABEL
        , INPUT_TYPE
        , COALESCE(DETAIL_SETTINGS::TEXT, '') AS DETAIL_SETTINGS
        , DISPLAY_ORDER
        , IS_NULLABLE
        , IS_VISIBLE
        , REFERENCE_TABLE
        , REFERENCE_COLUMN
        , DISPLAY_COLUMN
        , CODE_INFO
        , CODE_VALUE
        , COMPANY_CODE
    FROM TABLE_TYPE_COLUMNS
    WHERE TABLE_NAME = #{table_name}
    <if test='company_code != null and company_code != "*"'>
      AND (COMPANY_CODE = #{company_code} OR COMPANY_CODE = '*')
    </if>
    ORDER BY COLUMN_NAME
        , CASE WHEN COMPANY_CODE = #{company_code} THEN 0 ELSE 1 END
  </select>

  <!-- ═══ 테이블 라벨 단건 조회 ═══ -->
  <select id="getTableLabel" parameterType="map" resultType="string">
    SELECT TABLE_LABEL
    FROM TABLE_LABELS
    WHERE TABLE_NAME = #{table_name}
  </select>

  <!-- ═══ 테이블 간 업무 관계 (table_relationships 기반) ═══ -->
  <select id="getMetaRelations" parameterType="map" resultType="map">
    SELECT
        SOURCE_TABLE
        , TARGET_TABLE
        , RELATION_TYPE
        , COALESCE(LABEL, '') AS LABEL
        , COALESCE(DESCRIPTION, '') AS DESCRIPTION
        , SOURCE_COLUMN
        , TARGET_COLUMN
    FROM TABLE_RELATIONSHIPS
    WHERE (SOURCE_TABLE = #{table_name} OR TARGET_TABLE = #{table_name})
      AND IS_ACTIVE = 'Y'
    <if test='company_code != null and company_code != "*"'>
      AND (COMPANY_CODE = #{company_code} OR COMPANY_CODE = '*')
    </if>
    ORDER BY SOURCE_TABLE, TARGET_TABLE
  </select>

</mapper>
