"""Generate 100 TLS flow CSV files (10000 rows each) matching TlsDB.csv column spec.

Usage:
    runtime\\python\\python.exe scripts\\gen_massive_test.py [--files 100] [--rows 10000] [--output-dir data/test_csvs]
"""
import sys
import argparse
from pathlib import Path

# Allow importing from project root
sys.path.insert(0, str(Path(__file__).parent.parent))

from scripts.gen_test_data import generate_row, COLUMNS, COVERAGE
import random
import csv
from datetime import datetime


def main():
    parser = argparse.ArgumentParser(
        description='Generate 100 massive TLS flow CSV files (10,000 rows each)'
    )
    parser.add_argument('--files', type=int, default=100,
                        help='Number of CSV files (default: 100)')
    parser.add_argument('--rows', type=int, default=10000,
                        help='Rows per file (default: 10000)')
    parser.add_argument('--output-dir', type=str, default='data/test_csvs',
                        help='Output directory (default: data/test_csvs)')
    parser.add_argument('--seed', type=int, default=42,
                        help='Random seed for reproducibility (default: 42)')
    args = parser.parse_args()

    random.seed(args.seed)
    out_dir = Path(args.output_dir)
    out_dir.mkdir(parents=True, exist_ok=True)
    base_time = datetime(2026, 6, 1, 0, 0, 0)

    total_rows = 0
    for file_idx in range(args.files):
        file_seed = args.seed + file_idx
        file_rng = random.Random(file_seed)

        # Generate all rows for this file
        rows = []
        for row_i in range(args.rows):
            row = generate_row(total_rows + row_i, base_time)
            rows.append(row)

        # Auto-drop columns that are ALL blank
        non_blank_cols = set()
        for row in rows:
            for col in COLUMNS:
                val = row.get(col, '')
                if val != '' and val != '+':
                    non_blank_cols.add(col)
        file_columns = [c for c in COLUMNS if c in non_blank_cols]

        # Randomize column order per file
        shuffled = file_columns[:]
        file_rng.shuffle(shuffled)

        # Write: test_0000.csv .. test_0099.csv
        out_path = out_dir / f'test_{file_idx:04d}.csv'
        with open(out_path, 'w', newline='', encoding='utf-8') as f:
            writer = csv.DictWriter(f, fieldnames=shuffled, extrasaction='ignore')
            writer.writeheader()
            for row in rows:
                writer.writerow(row)

        total_rows += args.rows

        if (file_idx + 1) % 10 == 0 or file_idx == 0 or file_idx == args.files - 1:
            print(f'  [{file_idx + 1}/{args.files}] {total_rows} rows, '
                  f'{len(shuffled)} cols, seed={file_seed}')

    print(f'\nDone: {args.files} files x {args.rows} rows = {total_rows} total rows')
    print(f'Output: {out_dir.resolve()}')


if __name__ == '__main__':
    main()
