#!/usr/bin/env python3

from google.oauth2 import service_account
from googleapiclient.discovery import build

def check_overtime_sheet():
    """
    檢查加班時數總表的內容
    """
    SPREADSHEET_ID = '1FFRR9i5wFSNjNqLjVN7xWI74qV7SqjJncQHyEyzmtAA'
    SHEET_NAME = '加班時數總表'
    
    try:
        creds = service_account.Credentials.from_service_account_file(
            'service_account_key.json',
            scopes=['https://www.googleapis.com/auth/spreadsheets.readonly']
        )
        
        service = build('sheets', 'v4', credentials=creds)
        sheet = service.spreadsheets()
        
        # 讀取所有資料
        range_name = f'{SHEET_NAME}!A1:E20'
        result = sheet.values().get(
            spreadsheetId=SPREADSHEET_ID,
            range=range_name
        ).execute()
        
        values = result.get('values', [])
        
        if not values:
            print('沒有找到資料')
            return
        
        print(f"「{SHEET_NAME}」內容：")
        print("=" * 80)
        
        for i, row in enumerate(values, 1):
            # 補齊空白欄位
            while len(row) < 5:
                row.append('')
            print(f"第 {i:2d} 行: {row}")
            
            # 檢查日期格式問題
            if i > 1 and len(row) >= 3:  # 跳過標題行
                start_date = row[1]
                end_date = row[2]
                if '1899' in str(start_date) or '1899' in str(end_date):
                    print(f"    ⚠️  發現 1899 年日期問題：{start_date} / {end_date}")
        
        print(f"\n總共有 {len(values)} 行資料")
        
    except Exception as e:
        print(f"讀取試算表時發生錯誤：{e}")

if __name__ == '__main__':
    check_overtime_sheet()