-- ============================================================================
-- 92_signage_rag_setting.sql — ESN 최신 AI 기법(중앙 guardia-rag) 토글 설정 (멱등)
-- ----------------------------------------------------------------------------
-- 기존 signage_setting 은 전역(테넌트 무차원)이라 RAG 기법 토글의 *테넌트 격리* 요건을
-- 만족하지 못한다. 그래서 (tenant_code, setting_key) 복합 PK 의 전용 테이블을 신설한다.
-- sql.init.mode=always + continue-on-error 환경에서 재실행 안전(IF NOT EXISTS / ON CONFLICT).
-- 무거운 기법(rerank·graphrag·stream)은 서버 RAM 제약상 기본 OFF, retrieval_mode 기본 vector.
-- 자격증명/PII 미저장 — 운영 토글 값만.
-- ============================================================================

CREATE TABLE IF NOT EXISTS signage_rag_setting (
    tenant_code   VARCHAR(20)  NOT NULL,
    setting_key   VARCHAR(100) NOT NULL,
    setting_value TEXT,
    updated_at    TIMESTAMP DEFAULT NOW(),
    PRIMARY KEY (tenant_code, setting_key)
);

-- 테넌트별 기본 토글 시드 (멱등). 4개 테넌트 × 토글 키.
-- 무거운 기법 기본 off, 소형모델, vector 모드, top_k=5.
INSERT INTO signage_rag_setting (tenant_code, setting_key, setting_value) VALUES
  ('LGINNOTEK','rag.enabled','true'),       ('LGINNOTEK','rag.retrieval_mode','vector'),
  ('LGINNOTEK','rag.rerank','false'),       ('LGINNOTEK','rag.graphrag','false'),
  ('LGINNOTEK','rag.tool_use','false'),     ('LGINNOTEK','rag.structured','true'),
  ('LGINNOTEK','rag.stream','false'),       ('LGINNOTEK','rag.top_k','5'),
  ('LGINNOTEK','rag.agent_max_steps','4'),  ('LGINNOTEK','rag.temperature','0.1'),
  ('LGINNOTEK','rag.faithfulness_threshold','0.5'),
  ('LGINNOTEK','rag.generation_model','llama3.2:1b'),

  ('LGIT','rag.enabled','true'),            ('LGIT','rag.retrieval_mode','vector'),
  ('LGIT','rag.rerank','false'),            ('LGIT','rag.graphrag','false'),
  ('LGIT','rag.tool_use','false'),          ('LGIT','rag.structured','true'),
  ('LGIT','rag.stream','false'),            ('LGIT','rag.top_k','5'),
  ('LGIT','rag.agent_max_steps','4'),       ('LGIT','rag.temperature','0.1'),
  ('LGIT','rag.faithfulness_threshold','0.5'),
  ('LGIT','rag.generation_model','llama3.2:1b'),

  ('EMART','rag.enabled','true'),           ('EMART','rag.retrieval_mode','vector'),
  ('EMART','rag.rerank','false'),           ('EMART','rag.graphrag','false'),
  ('EMART','rag.tool_use','false'),         ('EMART','rag.structured','true'),
  ('EMART','rag.stream','false'),           ('EMART','rag.top_k','5'),
  ('EMART','rag.agent_max_steps','4'),      ('EMART','rag.temperature','0.1'),
  ('EMART','rag.faithfulness_threshold','0.5'),
  ('EMART','rag.generation_model','llama3.2:1b'),

  ('ZIOINFO','rag.enabled','true'),         ('ZIOINFO','rag.retrieval_mode','vector'),
  ('ZIOINFO','rag.rerank','false'),         ('ZIOINFO','rag.graphrag','false'),
  ('ZIOINFO','rag.tool_use','false'),       ('ZIOINFO','rag.structured','true'),
  ('ZIOINFO','rag.stream','false'),         ('ZIOINFO','rag.top_k','5'),
  ('ZIOINFO','rag.agent_max_steps','4'),    ('ZIOINFO','rag.temperature','0.1'),
  ('ZIOINFO','rag.faithfulness_threshold','0.5'),
  ('ZIOINFO','rag.generation_model','llama3.2:1b')
ON CONFLICT (tenant_code, setting_key) DO NOTHING;
