#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
檢查 import_date 欄位的數據類型和值
"""

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

from database.vendor_manager import VendorDatabaseManager
from database.vendor_models import VendorStockout

def check_import_date():
    """檢查 import_date 欄位"""

    db = VendorDatabaseManager()
    session = db.get_session()

    try:
        # 查詢最近 5 筆記錄
        records = session.query(VendorStockout).order_by(
            VendorStockout.id.desc()
        ).limit(5).all()

        print("=" * 80)
        print("檢查 import_date 欄位")
        print("=" * 80)

        for record in records:
            print(f"\nID: {record.id}")
            print(f"  import_date 原始值: {record.import_date}")
            print(f"  import_date 類型: {type(record.import_date)}")

            if record.import_date:
                print(f"  strftime 轉換: {record.import_date.strftime('%Y-%m-%d')}")
            else:
                print(f"  ⚠️  import_date 是 None")

            print(f"  product_code: {record.product_code}")
            print(f"  product_name: {record.product_name}")

        print("\n" + "=" * 80)

    except Exception as e:
        print(f"❌ 錯誤: {e}")
        import traceback
        traceback.print_exc()
    finally:
        session.close()

if __name__ == '__main__':
    check_import_date()
