#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
直接讀取 Excel 檔案，查看實際內容
"""

import pandas as pd
import sys
import os

def read_excel_directly():
    """直接讀取 Excel"""

    # 尋找 Excel 檔案
    possible_paths = [
        '/Users/ogt/momo_pro_system/缺貨測試.xlsx',
        '/Users/ogt/缺貨測試.xlsx',
        '/Users/ogt/Downloads/缺貨測試.xlsx',
    ]

    excel_path = None
    for path in possible_paths:
        if os.path.exists(path):
            excel_path = path
            break

    if not excel_path:
        print("找不到 Excel 檔案，請提供完整路徑")
        print("使用方式: python3 direct_read_excel.py <Excel檔案路徑>")
        return

    print("=" * 80)
    print(f"讀取 Excel: {excel_path}")
    print("=" * 80)

    try:
        # 讀取 Excel
        df = pd.read_excel(excel_path)

        print(f"\n📊 資料概況：")
        print(f"   總行數: {len(df)}")
        print(f"   總欄位數: {len(df.columns)}")

        print(f"\n📋 所有欄位名稱：")
        for i, col in enumerate(df.columns, 1):
            print(f"   {i:2d}. {col}")

        print(f"\n📄 第一行完整數據：")
        print("=" * 80)
        for col in df.columns:
            value = df[col].iloc[0]
            value_type = type(value).__name__
            print(f"{col:20s}: {value} (類型: {value_type})")

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

        # 檢查是否有18個欄位
        if len(df.columns) == 18:
            print("✅ 欄位數量正確：18 個")
        else:
            print(f"⚠️  欄位數量不對：{len(df.columns)} 個（預期 18 個）")

    except Exception as e:
        print(f"\n❌ 錯誤: {e}")
        import traceback
        traceback.print_exc()

if __name__ == '__main__':
    if len(sys.argv) > 1:
        # 使用提供的路徑
        excel_path = sys.argv[1]
        if os.path.exists(excel_path):
            print("=" * 80)
            print(f"讀取 Excel: {excel_path}")
            print("=" * 80)
            df = pd.read_excel(excel_path)
            print(f"\n總行數: {len(df)}, 總欄位數: {len(df.columns)}")
            print(f"\n欄位清單：")
            for i, col in enumerate(df.columns, 1):
                print(f"   {i}. {col}")
            print(f"\n第一行數據：")
            for col in df.columns:
                print(f"   {col}: {df[col].iloc[0]}")
        else:
            print(f"檔案不存在: {excel_path}")
    else:
        read_excel_directly()
