# cSpell:ignore momo bottomicon
"""
修正錯誤圖片腳本
清理資料庫中的錯誤圖片（bottomIcon 等），並標記為需要重新抓取
"""
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
from database.edm_models import PromoProduct

# 資料庫路徑
DB_PATH = os.path.join(BASE_DIR, 'data', 'momo_database.db')

def fix_wrong_images():
    """清理錯誤的圖片 URL"""
    print("🔧 開始修正錯誤圖片...\n")

    if not os.path.exists(DB_PATH):
        print(f"❌ 資料庫檔案不存在: {DB_PATH}")
        return

    try:
        engine = create_engine(f"sqlite:///{DB_PATH}")
        Session = sessionmaker(bind=engine)
        session = Session()

        # 定義錯誤圖片的關鍵字
        wrong_patterns = [
            '%bottomIcon%',
            '%bottomicon%',
            '%loader.gif%',
            '%blank.png%'
        ]

        print("=" * 60)
        print("📦 修正一般商品 (Product)")
        print("=" * 60)

        fixed_count = 0
        for pattern in wrong_patterns:
            products = session.query(Product).filter(
                Product.image_url.like(pattern)
            ).all()

            for p in products:
                print(f"修正: [{p.i_code}] {p.name[:50]}...")
                print(f"  錯誤圖片: {p.image_url}")
                p.image_url = None  # 清空錯誤的圖片 URL
                fixed_count += 1

        session.commit()
        print(f"\n✅ 已修正 {fixed_count} 個商品的錯誤圖片")

        # 檢查促銷商品
        print("\n" + "=" * 60)
        print("🎁 檢查促銷商品 (PromoProduct)")
        print("=" * 60)

        promo_fixed = 0
        for pattern in wrong_patterns:
            promos = session.query(PromoProduct).filter(
                PromoProduct.image_url.like(pattern)
            ).all()

            for p in promos:
                print(f"修正: [{p.i_code}] {p.name[:50]}...")
                print(f"  錯誤圖片: {p.image_url}")
                p.image_url = None
                promo_fixed += 1

        session.commit()
        print(f"\n✅ 已修正 {promo_fixed} 個促銷商品的錯誤圖片")

        print("\n" + "=" * 60)
        print("🎉 修正完成！")
        print("=" * 60)
        print(f"總計修正: {fixed_count + promo_fixed} 個商品")
        print("\n💡 提示: 下次執行爬蟲時，這些商品會重新抓取圖片。")

        session.close()

    except Exception as e:
        print(f"❌ 修正失敗: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    fix_wrong_images()
