import pandas as pd
import glob
import argparse
import os


def combine_csv_files(input_folder: str):
    all_files = glob.glob(f"{input_folder}/*.csv")

    if not all_files:
        print("Error: No CSV files found in the specified folder.")
        return

    dataframes = []

    for file in all_files:
        if os.path.getsize(file) == 0:
            print(f"Skipping empty file: {file}")
            continue

        try:
            df = pd.read_csv(file, skiprows=1, header=None)  # Read without headers
            dataframes.append(df)
        except pd.errors.EmptyDataError:
            print(f"Skipping invalid or empty file: {file}")

    if not dataframes:
        print("Error: No valid data to combine.")
        return

    # Combine without aligning columns
    combined_df = pd.concat(dataframes, ignore_index=True, axis=0)

    output_file = os.path.join(input_folder, "output.csv")
    combined_df.to_csv(output_file, index=False, header=False)  # Remove headers
    print(f"Combined CSV saved as {output_file}")


def call_combiner(input_folder: str):
    """Function to call CSV combiner programmatically."""
    combine_csv_files(input_folder)


def main():
    parser = argparse.ArgumentParser(description="Combine multiple CSV files into one, ignoring the first row in each.")
    parser.add_argument("input_folder", type=str, help="Folder containing CSV files")

    args = parser.parse_args()
    combine_csv_files(args.input_folder)


if __name__ == "__main__":
    main()