package com.zioinfo.kintex.system.authpolicy;

import org.apache.ibatis.annotations.*;

import java.util.Map;

/** 인증 정책 매퍼(sys_auth_policy). 테넌트 싱글턴 1행. */
@Mapper
public interface AuthPolicyMapper {

    @Select("SELECT pw_min_length AS \"pwMinLength\", pw_require_upper AS \"pwRequireUpper\", "
            + "pw_require_number AS \"pwRequireNumber\", pw_require_special AS \"pwRequireSpecial\", "
            + "pw_expire_days AS \"pwExpireDays\", login_max_fail AS \"loginMaxFail\", "
            + "lock_minutes AS \"lockMinutes\", session_timeout_min AS \"sessionTimeoutMin\", "
            + "to_char(updated_at AT TIME ZONE 'UTC','YYYY-MM-DD\"T\"HH24:MI:SS\"Z\"') AS \"updatedAt\", "
            + "updated_by AS \"updatedBy\" "
            + "FROM sys_auth_policy WHERE tenant_id = #{tenantId}")
    Map<String, Object> find(@Param("tenantId") String tenantId);

    @Insert("INSERT INTO sys_auth_policy (tenant_id, pw_min_length, pw_require_upper, pw_require_number, "
            + "pw_require_special, pw_expire_days, login_max_fail, lock_minutes, session_timeout_min, updated_by) "
            + "VALUES (#{tenantId}, #{pwMinLength}, #{pwRequireUpper}, #{pwRequireNumber}, #{pwRequireSpecial}, "
            + "#{pwExpireDays}, #{loginMaxFail}, #{lockMinutes}, #{sessionTimeoutMin}, #{updatedBy}) "
            + "ON CONFLICT (tenant_id) DO UPDATE SET pw_min_length=EXCLUDED.pw_min_length, "
            + "pw_require_upper=EXCLUDED.pw_require_upper, pw_require_number=EXCLUDED.pw_require_number, "
            + "pw_require_special=EXCLUDED.pw_require_special, pw_expire_days=EXCLUDED.pw_expire_days, "
            + "login_max_fail=EXCLUDED.login_max_fail, lock_minutes=EXCLUDED.lock_minutes, "
            + "session_timeout_min=EXCLUDED.session_timeout_min, updated_at=now(), updated_by=EXCLUDED.updated_by")
    int upsert(Map<String, Object> p);
}
