import moment from "moment";
import csv from "csvtojson";
import * as fs from "fs";

const start = moment().set("year", 2019).set("month", 8).set("date", 1);

const end = moment().set("year", 2023).set("month", 2).set("date", 1);

let current = start.clone();

do {
	const lastYearString = current.clone().startOf("month").format("YYYYMMDD");
	const nextYearString = current
		.clone()
		.subtract(1, "month")
		.add(1, "year")
		.endOf("month")
		.format("YYYYMMDD");

	const filename = `KF_${lastYearString}_${nextYearString}.csv`;

	const url =
		"https://opendata.dwd.de/climate_environment/CDC/derived_germany/techn/monthly/climate_correction_factor/recent/";

	const response = await fetch(`${url}${filename}`);

	if (response.status !== 200) {
		console.error(`Could not fetch ${url}${filename}`);
		process.exit(1);
	}

	const parser = csv({
		noheader: false,
		output: "json",
		delimiter: ";",
	});

	const data: {
		DatAnf: string;
		DatEnd: string;
		PLZ: string;
		KF: string;
	}[] = await parser.fromString(await response.text());

	const year = current.year();
	const month = current.month();

	const json = JSON.parse(fs.readFileSync("./test.json", "utf-8"));

	fs.writeFileSync(
		"./test.json",
		JSON.stringify(
			[
				...json,
				...data.map((row) => [
					year,
					month,
					parseFloat(row.KF),
					row.PLZ,
				]),
			],
			null,
			2
		)
	);

	console.log(`Wrote ${year}-${month} to file.`);
} while (current.add(1, "month").isSameOrBefore(end));
