#!/usr/bin/env python3
"""
쌍화탕 처방 및 당귀 약재 확인
"""

import sqlite3

def check_ssanghwatang():
    conn = sqlite3.connect('database/kdrug.db')
    cursor = conn.cursor()

    # 쌍화탕 처방 찾기
    print("🔍 쌍화탕 처방 검색...")
    print("="*60)

    cursor.execute("""
        SELECT formula_id, formula_code, formula_name
        FROM formulas
        WHERE formula_name LIKE '%쌍화%'
    """)

    formulas = cursor.fetchall()

    if not formulas:
        print("❌ 쌍화탕 처방을 찾을 수 없습니다.")
    else:
        for formula_id, code, name in formulas:
            print(f"\n📋 {name} ({code})")

            # 처방 구성 약재 확인
            cursor.execute("""
                SELECT hm.herb_name, hm.ingredient_code, fi.grams_per_cheop
                FROM formula_ingredients fi
                JOIN herb_masters hm ON fi.ingredient_code = hm.ingredient_code
                WHERE fi.formula_id = ?
                ORDER BY fi.sort_order
            """, (formula_id,))

            ingredients = cursor.fetchall()
            print("  구성 약재:")
            for herb_name, code, amount in ingredients:
                if '당귀' in herb_name:
                    print(f"  ⚠️ {herb_name} ({code}): {amount}g  <-- 당귀 발견!")
                else:
                    print(f"  - {herb_name} ({code}): {amount}g")

    # 당귀 관련 약재 검색
    print("\n\n🌿 당귀 관련 약재 검색...")
    print("="*60)

    cursor.execute("""
        SELECT ingredient_code, herb_name, herb_name_hanja
        FROM herb_masters
        WHERE herb_name LIKE '%당귀%'
        ORDER BY herb_name
    """)

    danggui_herbs = cursor.fetchall()
    for code, name, hanja in danggui_herbs:
        print(f"{code}: {name} ({hanja})")

    # 일당귀 확인
    print("\n✅ 일당귀 검색:")
    cursor.execute("""
        SELECT ingredient_code, herb_name, herb_name_hanja
        FROM herb_masters
        WHERE herb_name = '일당귀'
           OR herb_name LIKE '%일당귀%'
    """)

    result = cursor.fetchall()
    if result:
        for code, name, hanja in result:
            print(f"  {code}: {name} ({hanja})")
    else:
        print("  ❌ 일당귀를 찾을 수 없음")

    conn.close()

if __name__ == "__main__":
    check_ssanghwatang()