import time
import os
import sys
from app.data.fast_parser import FastLogParser
from app.core.reference_data import ref_data
import polars as pl

def test_parser():
    # Setup
    input_file = "d:/py_train/mmi/sample/20250723_180942_13_HCR_TC2_edit.dat"
    if not os.path.exists(input_file):
        print(f"Input file not found: {input_file}")
        return


    # Reference Data는 자동으로 SQLite에서 로드됨
    print(f"Reference Data loaded. Version: {ref_data.get_version()}")


    # Run Fast Parser
    print("Running FastLogParser...")
    start_time = time.time()
    output_parquet = FastLogParser.parse_to_parquet(input_file)
    end_time = time.time()

    if output_parquet:
        print(f"Success! Saved to: {output_parquet}")
        print(f"Time taken: {end_time - start_time:.4f} seconds")
        
        # Verify Data
        df = pl.read_parquet(output_parquet)
        print(f"Rows: {len(df)}")
        
        # Check System ID
        if "system_id" in df.columns:
            print("System ID Counts:")
            print(df["system_id"].value_counts())
        else:
            print("ERROR: 'system_id' column NOT found!")
            print("Columns found:", df.columns[:10])

        # Check VDI/VDO fields
        print("\nVDI/VDO Sample:")
        vdi_cols = [c for c in df.columns if "vdi" in c or "vdo" in c]
        if vdi_cols:
            print(f"Found {len(vdi_cols)} VDI/VDO columns.")
            print(df.select(vdi_cols[:5]).head())
        else:
            print("WARNING: No VDI/VDO columns found!")
    else:
        print("Parsing Failed.")

if __name__ == "__main__":
    test_parser()
