import os
import sys
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, BASE_DIR)

from database.models import Product

DB_PATH = os.path.join(BASE_DIR, 'data', 'momo_database.db')
engine = create_engine(f'sqlite:///{DB_PATH}')
Session = sessionmaker(bind=engine)
session = Session()

i_codes = ['5939587', '14713987', '14237189', '13759520', '13330513', '13731019', 
           '14049583', '14695038', '14373672', '13646926', '4633943', '6271461', '13559833']

print("🔍 驗證第二批商品的圖片 URL:\n")
print("=" * 100)

correct = 0
incorrect = 0

for i_code in i_codes:
    product = session.query(Product).filter(Product.i_code == i_code).first()
    if product:
        print(f"\n[{i_code}] {product.name[:50]}...")
        print(f"  圖片: {product.image_url}")
        
        if product.image_url and i_code in product.image_url:
            print(f"  ✅ 正確: URL 包含商品編號")
            correct += 1
        else:
            print(f"  ❌ 錯誤: URL 不包含商品編號")
            incorrect += 1

print("\n" + "=" * 100)
print(f"\n📊 驗證結果:")
print(f"  ✅ 正確: {correct}/{len(i_codes)}")
print(f"  ❌ 錯誤: {incorrect}/{len(i_codes)}")

session.close()
