import csv
from pathlib import Path

files = sorted(Path(".").glob("chunk_*.csv"))
if not files:
    raise ValueError("No chunk CSV files found")

output = Path("final.csv")
partial = Path("final.csv.partial")
if output.exists() or partial.exists():
    raise FileExistsError("Use a fresh output path")

header = None
count = 0
with partial.open("x", encoding="utf-8", newline="") as target:
    writer = csv.writer(target)
    for path in files:
        with path.open(encoding="utf-8-sig", newline="") as source:
            reader = csv.reader(source, strict=True)
            current = next(reader, None)
            if not current or len(set(current)) != len(current):
                raise ValueError(f"Missing or duplicate header: {path}")
            if header is None:
                header = current
                writer.writerow(header)
            elif current != header:
                raise ValueError(f"Column order or schema differs: {path}")
            for record in reader:
                if len(record) != len(header):
                    raise ValueError(f"Wrong field count: {path}")
                writer.writerow(record)
                count += 1
partial.rename(output)
print(f"Merged {count} data records")
