# -*- coding: utf-8 -*-
import sys, io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
from sqlalchemy import create_engine, text

pg = create_engine('postgresql://admin:trajet6640@192.168.0.87:5432/apdb_master').connect()

# 약국 제품 → PostgreSQL 매칭 (체중/용량 포함)
mappings = [
    # (약국제품명, 검색키워드)
    ('제스타제(10정)', '제스타제', '10'),
    ('파라캅L(5kg이상)', '파라캅', 'L'),
    ('파라캅S(5kg이하)', '파라캅', 'S'),
    ('하트캅츄어블(11kg이하)', '하트캅', '11'),
    ('넥스가드L(15~30kg)', '넥스가드', '15'),
    ('넥스가드xs(2~3.5kg)', '넥스가드', '2'),
    ('다이로하트정M(12~22kg)', '다이로하트', '12'),
    ('다이로하트정S(5.6~11kg)', '다이로하트', '5.6'),
    ('다이로하트정SS(5.6kg이하)', '다이로하트', 'SS'),
    ('세레니아정16mg(개멀미약)', '세레니아', '16'),
    ('세레니아정24mg(개멀미약)', '세레니아', '24'),
    ('하트세이버츄어블M(12~22kg)', '하트세이버', '12'),
    ('하트세이버츄어블S(5.6~11kg)', '하트세이버', '5.6'),
    ('하트웜솔루션츄어블M(12~22kg)', '하트웜', '12'),
    ('하트웜솔루션츄어블S(11kg이하)', '하트웜', '11'),
]

print('=== 상세 매칭 검색 ===\n')

for pharm_name, keyword, size in mappings:
    result = pg.execute(text("""
        SELECT apc, product_name, packaging,
               llm_pharm->>'사용가능 동물' as target
        FROM apc 
        WHERE product_name ILIKE :kw
        ORDER BY product_name
        LIMIT 10
    """), {'kw': f'%{keyword}%'})
    
    print(f'\n📦 {pharm_name} (검색: {keyword}, 사이즈: {size})')
    for r in result:
        mark = '⭐' if size.lower() in r.product_name.lower() else '  '
        print(f'{mark} {r.apc}: {r.product_name[:50]}')

pg.close()
