import csv
from pathlib import Path


def collect_first_tokens_from_full_table_files(directory: Path) -> list[str]:
    first_tokens: list[str] = []

    for file_path in directory.iterdir():
        # Match file names like "Full_Table_List_..." regardless of case.
        if file_path.is_file() and file_path.name.lower().startswith("full_table_list"):
            with file_path.open("r", encoding="utf-8", errors="ignore") as f:
                for line in f:
                    parts = line.strip().split()
                    if parts:
                        first_tokens.append(parts[0])

    return first_tokens


def main() -> None:
    current_directory = Path.cwd()
    tokens = collect_first_tokens_from_full_table_files(current_directory)
    tokens.sort()

    for token in tokens:
        print(token)

    output_file = current_directory / "full_table_list_tokens.csv"
    with output_file.open("w", newline="", encoding="utf-8") as csv_file:
        writer = csv.writer(csv_file)
        writer.writerow(["value"])
        for token in tokens:
            writer.writerow([token])

    print(f"\nTotal extracted values: {len(tokens)}")
    print(f"Saved CSV file: {output_file}")


if __name__ == "__main__":
    main()
