#!/usr/bin/env python3
"""
테스트용 당귀 로트 추가 - 복합 로트 테스트를 위함
"""
import sqlite3
from datetime import datetime, timedelta

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

    try:
        # 휴먼일당귀 herb_item_id 확인
        cursor.execute("SELECT herb_item_id FROM herb_items WHERE herb_name = '휴먼일당귀'")
        herb_item_id = cursor.fetchone()[0]

        # 공급업체 ID 확인
        cursor.execute("SELECT supplier_id FROM suppliers WHERE name = '한의사랑' LIMIT 1")
        supplier_id = cursor.fetchone()[0]

        # 기존 로트의 receipt_line_id 복사
        cursor.execute("""
            SELECT receipt_line_id
            FROM inventory_lots
            WHERE herb_item_id = ?
            LIMIT 1
        """, (herb_item_id,))
        receipt_line_id = cursor.fetchone()[0]

        # 새 로트 추가 (한국산, 다른 가격)
        cursor.execute("""
            INSERT INTO inventory_lots (
                herb_item_id, supplier_id, receipt_line_id, received_date, origin_country,
                unit_price_per_g, quantity_received, quantity_onhand,
                expiry_date, lot_number, is_depleted, display_name
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
        """, (
            herb_item_id,                    # herb_item_id (휴먼일당귀)
            supplier_id,                     # supplier_id
            receipt_line_id,                 # receipt_line_id (기존 로트에서 복사)
            datetime.now().strftime('%Y-%m-%d'),  # received_date
            '한국',                          # origin_country (기존은 중국)
            18.5,                            # unit_price_per_g (기존은 12.9)
            3000.0,                          # quantity_received
            3000.0,                          # quantity_onhand
            (datetime.now() + timedelta(days=365)).strftime('%Y-%m-%d'),  # expiry_date
            'TEST-DG-2024-001',              # lot_number
            0,                               # is_depleted
            '일당귀(한국산)'                  # display_name
        ))

        new_lot_id = cursor.lastrowid

        conn.commit()
        print(f"✅ 테스트용 당귀 로트 추가 완료!")
        print(f"   - Lot ID: {new_lot_id}")
        print(f"   - 약재: 휴먼일당귀")
        print(f"   - 원산지: 한국")
        print(f"   - 재고: 3000g")
        print(f"   - 단가: 18.5원/g")

        # 현재 당귀 로트 현황 표시
        print("\n=== 현재 휴먼일당귀 로트 현황 ===")
        cursor.execute("""
            SELECT lot_id, origin_country, quantity_onhand, unit_price_per_g
            FROM inventory_lots
            WHERE herb_item_id = ? AND is_depleted = 0
            ORDER BY lot_id
        """, (herb_item_id,))

        for row in cursor.fetchall():
            print(f"Lot #{row[0]}: {row[1]}산, 재고 {row[2]}g, 단가 {row[3]}원/g")

    except Exception as e:
        conn.rollback()
        print(f"❌ 오류: {e}")
    finally:
        conn.close()

if __name__ == "__main__":
    add_test_lot()