# -*- coding: utf-8 -*-
"""API 직접 테스트 - 디버그용"""
import requests
import json

# 지오영에서 실제 품목 검색해서 internal_code 얻기
import sys; sys.path.insert(0, '.'); import wholesale_path
from wholesale import GeoYoungSession

g = GeoYoungSession()
g.login()

# 재고 있는 품목 검색
r = g.search_products('라식스')
if r.get('items'):
    item = r['items'][0]
    print("="*60)
    print("검색된 품목:")
    print(f"  name: {item['name']}")
    print(f"  internal_code: {item['internal_code']}")
    print(f"  stock: {item['stock']}")
    print(f"  price: {item['price']}")
    print("="*60)
    
    # API 호출
    payload = {
        "wholesaler_id": "geoyoung",
        "items": [{
            "drug_code": "652100200",
            "kd_code": "라식스",
            "internal_code": item['internal_code'],  # 검색된 internal_code 사용
            "product_name": item['name'],
            "manufacturer": "한독",
            "specification": item.get('spec', ''),
            "order_qty": 1,
            "usage_qty": 100,
            "current_stock": 0
        }],
        "reference_period": "2026-02-01~2026-03-07",
        "dry_run": False,
        "cart_only": False
    }
    
    print("\n" + "="*60)
    print("API 요청:")
    print(json.dumps(payload, ensure_ascii=False, indent=2))
    print("="*60)
    
    response = requests.post(
        'http://localhost:7001/api/order/quick-submit',
        json=payload,
        timeout=60
    )
    
    print("\n" + "="*60)
    print(f"응답 (status: {response.status_code}):")
    print(json.dumps(response.json(), ensure_ascii=False, indent=2))
    print("="*60)
else:
    print("품목을 찾을 수 없습니다")
