#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ETL Script Quick Test
=======================================
功能：测试日志解析是否正常工作
"""

import sys
import os
from pathlib import Path
from logs_etl import ETLPipeline

def main():
    # 设置控制台编码
    if sys.platform == 'win32':
        os.system('chcp 65001 >nul 2>&1')

    print("="*60)
    print("ETL Script Test")
    print("="*60)

    # 获取当前目录
    base_dir = Path(__file__).parent
    output_dir = base_dir / 'report' / 'output_test'

    print(f"\n[*] Working Directory: {base_dir}")
    print(f"[*] Output Directory: {output_dir}")

    # 检查各系统目录是否存在
    print("\n[*] Checking System Directories:")
    systems = ['MDP', 'Hub', 'POTS', 'wdts']
    for system in systems:
        system_path = base_dir / system
        if system_path.exists():
            # 统计文件数
            log_count = len(list(system_path.glob('*.log')))
            csv_count = len(list(system_path.glob('*.csv')))
            print(f"  [OK] {system:10} - {log_count} log files, {csv_count} CSV files")
        else:
            print(f"  [X]  {system:10} - Directory not found")

    # 运行ETL
    print("\n[*] Running ETL...")
    try:
        pipeline = ETLPipeline(base_dir, output_dir)
        pipeline.run()

        # 检查输出文件
        print("\n[*] Checking Output Files:")
        output_files = [
            'Unified_Termination_Logs.csv',
            'Daily_Summary.csv',
            'Error_Details.csv'
        ]

        for filename in output_files:
            file_path = output_dir / filename
            if file_path.exists():
                # 统计行数
                with open(file_path, 'r', encoding='utf-8-sig') as f:
                    lines = sum(1 for _ in f)
                print(f"  [OK] {filename:30} ({lines} lines)")
            else:
                print(f"  [X]  {filename:30} - Not generated")

        print("\n" + "="*60)
        print("[SUCCESS] Test Complete!")
        print("="*60)
        print(f"\n[TIP] For production use: 'python logs_etl.py'")
        print(f"[DIR] Output files: {output_dir}")

    except Exception as e:
        print(f"\n[ERROR] Test failed: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)


if __name__ == '__main__':
    main()
