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

  <!--
    서브도메인으로 회사 DB 이름 조회.
    - DB_STATUS='active' 인 회사만 접근 허용.
    - SUBDOMAIN NULL 이거나 DB_NAME NULL 이면 null 리턴 → META fallback.
  -->
  <select id="resolveDbNameBySubdomain" parameterType="map" resultType="string">
    SELECT DB_NAME
    FROM COMPANY_MNG
    WHERE SUBDOMAIN = #{subdomain}
      AND DB_NAME IS NOT NULL
      AND DB_STATUS = 'active'
    LIMIT 1
  </select>

  <!--
    서브도메인으로 (db_name, company_code) 동시 조회.
    TenantConsistencyGuardFilter 가 JWT.company_code 와 대조하는 용도.
  -->
  <select id="resolveTenantBySubdomain" parameterType="map" resultType="map">
    SELECT DB_NAME       AS db_name
         , COMPANY_CODE  AS company_code
    FROM COMPANY_MNG
    WHERE SUBDOMAIN = #{subdomain}
      AND DB_NAME IS NOT NULL
      AND DB_STATUS = 'active'
    LIMIT 1
  </select>

</mapper>
